banner

Surprisingly, not many software developers and scientists seem to know about it, which saddens me because it is a very generalized and powerful tool for combining information in the presence of uncertainty. Sometimes its ability to extract accurate information seems almost magical, and if you think I’m talking too much, take a look at this video in which I show how the Kalman filter determines the orientation of a free-floating body by looking at its velocity vector. Amazing!

What is it?

The Kalman filter can be used in any domain where there is uncertain information about some dynamic system, and you can make an educated guess about what the system will do next. Even if chaotic reality intervenes and affects the clear motion we assume, the Kalman filter often does a pretty good job of predicting what’s actually going to happen. And it takes advantage of correlations between crazy phenomena that you might not even think of using!

Kalman filters are ideal for continuously changing systems. They don’t take up too much memory (because they don’t need to store history other than the previous state) and are very fast, making them well suited for real-time and embedded system tasks.

In most of the articles you’ll find on Google, the math of the Kalman filter implementation looks pretty daunting. And that’s too bad, because in fact the Kalman filter is very easy
and easy to understand if you look at it from the right angle. So it makes a great topic for an article, and I’ll try to expose it with clear and understandable images and colors. You don’t have to do much, just know the basics of probability theory and matrices.

I will start with a vague example that can be solved with the Kalman filter, but if you want to go straight to pretty pictures and math, you can skip this section.

What can you do with a Kalman filter?

Let’s look at an example: you have created a little robot that can wander through the forest; in order to do the moving, it needs to know exactly where it is.

Our little robot.

Suppose our robot has a state $\vec{x_k}$, that is, just a position and a velocity vector.

Note that the state is just a list of numbers specifying the configuration of our system; it can be anything. In our example, it’s a position and speed vector, but it could also be the amount of liquid in the tank, the temperature of the car engine, the position of the user’s finger on the touchpad, or any number of objects you need to track.

Our robot also has a GPS sensor that has about 10 meters accuracy, and that’s good, but it needs to know its location more accurately in this 10 meter diameter. There are a lot of ravines and precipices in this forest, so if the robot gets a few meters wrong, it could fall off a cliff. So the GPS by itself is not enough.

We can also learn something about how a robot moves: it knows the commands given to its wheel motors, and it knows that if it’s going in one direction and nothing is in its way, in the next instant it’s very likely to move in the same direction. But, of course, it doesn’t know anything about its movement: it might get blown by the wind, its wheels might get a little boggy or roll over bumps; so the number of wheel revolutions might not accurately reflect the robot’s movement, and that prediction won’t be perfect.

The GPS sensor tells us status information, but only indirectly, with a degree of uncertainty or inaccuracy. Our prediction tells us something about how the robot is moving, but only indirectly, with a fraction of uncertainty or inaccuracy.

But if we use all the information available to us, can we get a more accurate answer than each of the approximations in isolation? Of course, the answer is yes, and that’s what the Kalman filter is for.