Kalman Filter, Live
Lesson 1 · What is the Kalman filter?

What the Kalman filter does: truth plus noise

In July 1969, Apollo 11 flew 384,000 km to the Moon. Every sensor on board was noisy, and its computer was far weaker than the phone in your pocket. It still knew where it was, thanks to a few lines of maths called the Kalman filter. The same maths now steadies drones, steers robots and draws the blue dot on your phone. This course teaches it from zero, in four lessons, with code you run on the page.

Lesson 1 · slide 1The Kalman filter
reading = truth + noise     estimate = predict, then correct
Key ideas
  1. Every sensor reports the truth plus noise. No reading is exactly right.
  2. The Kalman filter does data fusion: it is a recipe for combining several imperfect readings into one better guess that beats each of them.
  3. It works in two steps, over and over: predict where things went, then correct the guess with a new reading.
  4. 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

EarthMoon 384,000 km planned path noisy sensor readings filter's estimate
A sketch, not to scale. Each dot is one noisy reading. The solid line is the Kalman filter's estimate: it stays close to the planned path all the way 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.

Lesson 1FreeNoise and data fusionWe build the base. How to measure a sensor's noise with one number, and how to fuse two noisy sensors into one answer that beats both.Lesson 2FreeThe 1D Kalman filterWe build the filter itself. A robot on a line, an encoder that drifts and a GPS that jumps. Predict, correct, repeat.Lesson 3PreviewThe matrix Kalman filterMany variables at once. Position and velocity together, the covariance matrix, and a rocket tracked in 2D.Lesson 4PreviewThe extended Kalman filterNonlinear systems, where the sensor does not read the state in a straight line. A robot tracked by two radars that only measure distance.

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:

reading = truth + noise

Say your true weight is 70.0 kg. Then the noise in each reading is what is left over:

70.2 − 70.0 = +0.2    69.8 − 70.0 = −0.2    70.1 − 70.0 = +0.1

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.

The dashed line is where the robot really is. Each dot is one GPS reading: close to the line, never exactly on it.

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:

The dots jump by a metre or more. The blue line, the average of all readings so far, settles close to the true 30 m.

A small case: three readings of 31.2, 28.9 and 30.4 m.

errors: +1.2, −1.1, +0.4    average reading = 90.5 / 3 ≈ 30.17 m, off by only 0.17 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:

was at 20 m, moves 4 m  →  predict 20 + 4 = 24 m
GPS reads 27 m  →  correct halfway: 24 + (27 − 24) / 2 = 25.5 m
The red encoder line slowly drifts above the truth. The GPS dots jump around it. The blue line, a Kalman filter, uses both and stays closest to the dashed truth. You will build it in lesson 2.

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:

29.2,  28.7,  29.8,  30.4,  31.1,  30.1,  29.4,  29.2 m
noise = reading − 30:   −0.8, −1.3, −0.2, +0.4, +1.1, +0.1, −0.6, −0.8
average = 237.9 / 8 ≈ 29.74 m, off by 0.26 m

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
noise = reading − truth:   +1.2,   −1.1,   +0.4
average reading = (31.2 + 28.9 + 30.4) / 3 = 90.5 / 3 ≈ 30.17 m

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
predict: 30 + 5 = 35 m
correct, trusting both equally: halfway from 35 to 38 = 36.5 m

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
square each error: 1, 4, 4, 1
mean of the squares: (1 + 4 + 4 + 1) / 4 = 2.5
square root: √2.5 ≈ 1.58 m

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.

Exercise Change sigma from 0.5 to 2.0 (four times bigger) and watch the dots scatter. The readout shows their RMS error growing from about 0.5 m to about 2 m. Slide 2 explains what sigma measures. Edit the code and press Run.
slide_1.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Enjoying lesson 1? Get the full course: all 4 lessons through the matrix filter and the EKF, your progress saved lesson by lesson, every solution and 5 extra problems per lesson. $20 once, yours for life.

Questions and comments

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

The full course

From noisy sensors to the EKF, by running it

Lessons 1 and 2 are free. Finish all four and you will be able to:

You get: all 4 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · Steppe, your AI tutor inside every lesson, day or night · every future update.

$20 once · lifetime access · 14-day money-back guarantee
Continue to checkout →

Already a member? Log in to the course