Kalman Filter, Live
Recap · Reading

Lesson 2 recap: the 1D Kalman filter

This lesson took lesson 1's fusion rule and put it on a moving robot. Walking with your eyes closed (the encoder) is smooth but drifts. A friend calling out your position (the GPS) never drifts but jumps around. The Kalman filter runs a loop: predict with the encoder, which widens the bell, then correct with the GPS, which narrows it. Two numbers, Q and R, set how much it trusts each sensor. Here is everything on one page.

Lesson 2 · slide 6Recap
x̂⁻ = x̂ + u,   P⁻ = P + Q   →   K = P⁻/(P⁻ + R),   x̂ = x̂⁻ + K(z − x̂⁻),   P = (1 − K)P⁻
Key ideas
  1. Dead reckoning drifts: P = n·Q grows without end.
  2. GPS alone is centred on the truth but jumpy: variance R at every step.
  3. Predict moves the bell and widens it. Correct fuses it with the GPS and narrows it.
  4. Q and R set the gain. High R: smooth but laggy. High Q: jumpy.
  • Dead reckoning. Walking with your eyes closed: add up the encoder's moves, x̂[n] = x̂[n−1] + u[n], where u[n] is the move the encoder reports. Each step adds Q to the variance, so σ grows like √n and the estimate drifts.
  • GPS only. A friend calling out your position: z = x_true + w with variance R. No drift, because each reading has fresh noise. But two readings one step apart differ with variance 2R, so the track jumps.
  • Predict. One step in the dark: x̂⁻ = x̂ + u, P⁻ = P + Q. Variances add, σ values do not.
  • Correct. A quick peek: lesson 1's fusion rule with the prediction as sensor 1 and the GPS as sensor 2. The innovation z − x̂⁻ is the surprise, and K is the share of it we believe. The worked update: 35 m with P⁻ = 5, GPS 38 m with R = 9, gives K = 5/14, x̂ ≈ 36.07 m and P ≈ 3.21.
  • The loop and tuning. P settles at a steady state where growth and shrink balance. Starting from P = 0, only the ratio Q/R moves the estimate. Start from the sensors' real variances.
One step of the loop: predict (slide and widen), the GPS bell arrives, correct (fuse and narrow). 30 m → 35 m → 36.07 m.
The loop over 30 steps. Grey dead reckoning (encoder only) drifts, green GPS dots jump, and the blue Kalman estimate stays smooth and close to the dashed truth.

Next lesson: the matrix Kalman filter

One number is not enough for most robots. They need position and velocity, or x and y, at the same time. Lesson 3 replaces the variance P with a covariance matrix, draws it as an ellipse, writes the motion as a state-space model, and runs the same predict and correct loop with matrices, on a rocket heading for Mars.

A preview of lessons 3 and 4

You just tracked one number. Real robots track several at once: position and speed, in 2D, from sensors that do not measure what you want directly. Here is where the course goes next.

Lesson 3: a 4-number Kalman filter tracks a rocket in 2D from noisy GPS. The estimate (solid line) runs smoothly along the true path (dashed), with less than half the GPS error.
Lesson 4: an extended Kalman filter finds a robot from two radars that only measure distance. Dead reckoning wanders off by tens of metres. The EKF stays close to the dashed truth.

Lessons 3 and 4 are in the full course: the matrix Kalman filter, the extended Kalman filter, every solution and 5 extra problems per lesson. $20 once, yours for life.

Read along

This lesson follows the section "The One-Dimensional Kalman Filter" of the interactive article at kalmanfilter.steppeschool.com, where you can drag the noise sliders and press Play.

Examples

Example 1

Dead reckoning with σ_enc = 0.4 m per step, starting from a known position. What is σ after 100 steps?

Show solutionHide solution

Q = 0.16, so P = 100 · 0.16 = 16 and σ = 4 m. The ±2σ band is ±8 m.

Example 2

Do one full step: x̂ = 0 m, P = 3, u = 2 m, Q = 1, then the GPS reads z = 4 m with R = 4.

Show solutionHide solution

Predict: x̂⁻ = 2 m, P⁻ = 4. Gain: K = 4 / 8 = 0.5.

Correct: x̂ = 2 + 0.5 · (4 − 2) = 3 m, P = 0.5 · 4 = 2.

Example 3

You multiply both Q and R by 10. What happens to the estimate x̂, and what happens to the band?

Show solutionHide solution

Starting from P = 0, every P⁻ and P is also multiplied by 10, so K = P⁻/(P⁻ + R) does not change. The estimate is exactly the same.

The band is √P, so it gets √10 ≈ 3.16 times wider. The filter claims to be less sure, even though it is just as accurate.

Exercise Challenge: set gps_std = 5.0 (a much worse GPS). With R still at 4.84 the Kalman RMS error rises to about 1.56 m. Retune R to the new GPS variance (R = σ²) and get the RMS error below 1.5 m. Then try anything else: Q, the seed or enc_std. Edit the code and press Run.
slide_6.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Enjoying lesson 2? 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