- Every sensor reports the truth plus noise. No reading is exactly right.
- The Kalman filter does data fusion: it is a recipe for combining several imperfect readings into one better guess that beats each of them.
- It works in two steps, over and over: predict where things went, then correct the guess with a new reading.
- To do this it needs a way to measure how big the noise is. That is what this lesson builds, one slide at a time.
A wall of equations
I first ran into the Kalman filter as a student. I was building a drone and needed its tilt angle from a few cheap, noisy sensors. I searched for "Kalman filter" and got a wall of matrix equations. I understood nothing.
It took years of projects before the idea clicked. This course is the gentle start I wish I had: the picture first, then the maths, one small step at a time, with code you can run and break. By the end you will build a Kalman filter yourself, line by line.
Sixty years of keeping things on track
The filter guided the Apollo spacecraft to the Moon. The craft had to follow a strict path, so it needed a good estimate of where it was, even though noise spoiled every sensor reading. The filter handled it.
More than sixty years later it is everywhere. It steadies drones, steers robots and self-driving cars, tracks satellites, and turns the jumpy GPS in your phone into the smooth blue dot on the map.
Truth plus noise
Back to the bathroom scale. Every real sensor works like it: GPS, a wheel encoder (it counts how far the wheels turn), a thermometer, an accelerometer (it senses speeding up and tilting). Each one gives a value close to the truth, but never exactly the truth. We write this as one line:
Say your true weight is 70.0 kg. Then the noise in each reading is what is left over:
The noise is the part we did not ask for: a small random error that changes every time we look. Here is the same thing for a robot that rolls 0.5 m forward each step while a GPS reads its position.
Many readings beat one
We can never remove the noise from a single reading. But the errors go up as often as down, so they partly cancel when we combine readings. Park the robot at 30 m and keep reading the GPS:
A small case: three readings of 31.2, 28.9 and 30.4 m.
To combine readings well, we need to know how big the noise tends to be. Knowing the size of the noise is all the filter needs to decide how much to trust each reading.
Predict, then correct
Now let the robot drive. Its wheel encoder says how far it moves. A GPS says where it is. Neither is perfect. The encoder slowly drifts: its small errors add up over time. The GPS jumps around.
The Kalman filter uses both. First it predicts: "I was at 30 m and I move 5 m per step, so now I should be at 35 m." Then it corrects: "The GPS says 38 m. I trust it a bit, so I move my guess part of the way toward 38." Then it repeats, every step, forever.
A small case, splitting the gap in half for now:
The hard question is how far to move toward the reading. The answer depends on how noisy each source is. So before we build the filter, we need a precise way to talk about noise.
The plan
This lesson builds the tools: how to describe a pile of random readings with two numbers (slide 2), the bell-shaped curve that noise follows (slide 3), how to tell a good sensor from a poor one (slide 4), and the rule for fusing two sensors (slide 5). Lesson 2 turns that rule into the one-dimensional Kalman filter. Lessons 3 and 4 grow it to matrices and to curved, nonlinear problems (problems where things do not change in straight lines).
A bigger example: eight GPS readings
The robot is parked at 30 m. The GPS gives eight readings:
The worst single reading misses by 1.3 m. The average of all eight misses by about a quarter of a metre. With hundreds of readings it usually gets closer still. But a moving robot cannot wait for hundreds of readings, and that is the problem the Kalman filter solves.
The lab
Below the text on every slide is the lab: Python code you can edit and run, and a picture it draws. Here a robot moves 0.5 m per step (the dashed line is the truth) and a GPS reads its position with noise (the dots). The readout is the line of numbers shown under the picture. Here it shows the RMS error of the readings, short for root-mean-square: square each miss, average the squares, take the square root. It is roughly the typical miss, in metres.
The lab uses a few drawing helpers that this course provides; they are not part of numpy. plot(…) draws a chart, dashed(…) adds a dashed line to it, and dots(…) adds a set of dots. Later slides add gauss(…), which draws hill-shaped curves, bell(…) for one such curve, samples= to drop readings in as dots, and fuse=True to draw two curves combined.
Next: each reading is a random draw. How do we describe a whole pile of random readings with just two numbers?
Examples
Example 1 · Three GPS readings
A robot stands still at 30 m. The GPS reads 31.2, 28.9 and 30.4 m.
Show solutionHide solution
The readings are off by up to 1.2 m, but the average is off by only 0.17 m. The errors partly cancel. Combining readings is the first hint of what the filter does.
Example 2 · Predict and correct, roughly
The robot was at 30 m and moves 5 m per step. Then the GPS reads 38 m.
Show solutionHide solution
The real filter does not split the gap in half. It gives less weight to the noisier source. Here the GPS is noisier than the prediction, so it gets less than half the say: in lesson 2 you will see the filter move only about a third of the way.
Example 3 · RMS error, the lab's readout
Four readings miss the truth by +1, −2, +2 and −1 m. How big is the typical error?
Show solutionHide solution
That is the RMS error (root of the mean of the squares). Squaring stops the + and − errors from cancelling, and the square root brings the answer back to metres.
Example 4
The true temperature is 20°. A thermometer reads 21, 18, 20.5 and 19.5. Find the noise in each reading and the average reading.
Show solutionHide solution
Noise = reading − truth: +1, −2, +0.5, −0.5.
Average reading = (21 + 18 + 20.5 + 19.5) / 4 = 79 / 4 = 19.75°. It is off by only 0.25°, less than any single reading.
Example 5
Readings miss the truth by +3, −3, +3 and −3 m. What is the average error, and what is the RMS error? Which one tells you more about the sensor?
Show solutionHide solution
Average error: (3 − 3 + 3 − 3) / 4 = 0. It looks perfect, but it is not.
RMS error: squares 9, 9, 9, 9, mean 9, square root 3 m. The RMS error tells the truth: every reading is 3 m off.

Questions and comments
Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.