- 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. Lesson 1 builds that base. Lessons 2 to 4 build the filter: one dimension, then many variables, then nonlinear systems.
It flew to the Moon
The Apollo craft had to follow a strict path. To do that it needed a good estimate of where it was, even though noise spoiled every sensor reading. The Kalman filter gave it that estimate, again and again, for the whole flight.
More than sixty years later the same idea 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.
Why you need it
If you build robots or work with embedded systems, your sensors lie a little. A GPS jumps. A gyroscope drifts. A wheel encoder slips. Sooner or later you need one good estimate from several bad readings, and the Kalman filter is the standard tool for that job.
I first ran into it 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 whole course in four steps
Here is the big picture before we start. Each lesson builds on the one before it, and every slide has code you can run and break.
So the order is: first the base, then the filter in one dimension, then many variables, then nonlinear systems. If a later lesson feels hard, the gap is almost always in an earlier one.
What you can do by the end
- Fuse two noisy sensors into one estimate that beats both.
- Build a 1D Kalman filter that tracks a robot from encoder and GPS data.
- Tune Q and R, the two noise settings of the filter, and predict how it will react.
- Write the matrix Kalman filter for position and velocity in 2D.
- Track a robot from range-only radars with an extended Kalman filter.
We start with the smallest piece: what a single sensor reading really is.
Truth plus noise
Step on a bathroom scale three times and you may read 70.2, 69.8 and 70.1 kg. You did not change. The scale did. 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.
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.