Kalman Filter, Live
Lesson 2 · The 1D Kalman filter

The 1D Kalman filter: predict, correct, repeat

Slides 3 and 4 gave the two steps. Now put them in a loop: at every time step, predict with the encoder, then correct with the GPS. Think of walking with your eyes closed and peeking now and then: step, peek, adjust, again and again. Each step makes you a bit less sure, and each peek makes you a bit more sure. So you never get lost, and you never shake around like the blurry peeks. That loop is the 1D Kalman filter, and two numbers decide how much it trusts each sensor: Q, the process noise variance (how much the motion can go wrong each step), and R, the measurement noise variance (how noisy the GPS is).

Lesson 2 · slide 5The 1D filter
predict: x̂⁻ = x̂ + u,   P⁻ = P + Q   →   correct: K = P⁻/(P⁻ + R),   x̂ = x̂⁻ + K(z − x̂⁻),   P = (1 − K)P⁻
Key ideas
  1. The filter is a loop: predict (P grows by Q), then correct (P is multiplied by (1 − K)).
  2. The growth and the shrink balance, so P settles at a steady state instead of running away.
  3. High R: small K, a smooth estimate that is slow to react, and drifts if R is far too high.
  4. High Q: big K, the filter snaps to every GPS reading and gets jumpy.

The rhythm

Walk with your eyes closed for one step, peek, adjust. Step, peek, adjust. Each step makes you a bit less sure. Each peek makes you a bit more sure. You never get lost, and you never shake around like the blurry peeks themselves. That rhythm is the whole filter.

The loop

At every time step n the filter does:

step 1, predict:   (1) x̂⁻ = x̂ + u[n],   (2) P⁻ = P + Q
step 2, gain:   (3) K = P⁻ / (P⁻ + R)
step 3, correct:   (4) x̂ = x̂⁻ + K (z[n] − x̂⁻),   (5) P = (1 − K) P⁻

Then it hands x̂ and P to the next step. That is the entire 1D Kalman filter: three steps, five equations. It only needs to remember two numbers between steps.

Here is the loop on the robot from slides 1 and 2, with the filter's two noise numbers set to the real sensors: Q = 0.25, the process noise variance (how much each encoder move can be off), and R = 4.84, the measurement noise variance (how noisy the GPS is):

Dashed: the truth. Green dots: GPS. Grey: the encoder only (dead reckoning), drifting away. Blue: the Kalman estimate with its ±2σ band. It stays smooth and stays near the truth.

Over the 30 steps in this picture the RMS errors are about 3.1 m for dead reckoning, 2.4 m for the GPS, and 0.9 m for the Kalman estimate. The filter beats both of its sensors.

Two steps by hand

Start where slide 4 stopped: x̂ ≈ 36.07 m with P ≈ 3.21 (Q = 1, R = 9). The encoder says u = 5 m again, and the GPS reads z = 40 m.

predict: x̂⁻ = 36.07 + 5 = 41.07 m,   P⁻ = 3.21 + 1 = 4.21
gain: K = 4.21 / (4.21 + 9) ≈ 0.319
correct: x̂ = 41.07 + 0.319 · (40 − 41.07) ≈ 40.73 m,   P = (1 − 0.319) · 4.21 ≈ 2.87
The second step of the loop. This time the GPS is below the prediction, so the corrected bell moves left, back toward 40 m.

The steady state

Dead reckoning's P grew forever. Here it does not. When P⁻ is small, K is small and the correct step barely shrinks it, so the predict step wins and P grows. When P⁻ is big, K is big and the correct step shrinks it hard. P settles where the two balance. In the big picture above, the blue band quickly reaches a fixed width and stays there.

Steady means every step looks the same as the last one. So P⁻ is the same number each time, and the predict step brings P back up to it: P + Q = P⁻, or P = P⁻ − Q. The correct step says P = P⁻R/(P⁻ + R).

set them equal:   P⁻ − Q = P⁻R/(P⁻ + R)
multiply both sides by (P⁻ + R):   (P⁻ − Q)(P⁻ + R) = P⁻R
multiply out:   (P⁻)² + R·P⁻ − Q·P⁻ − QR = P⁻R
cancel P⁻R on both sides:   (P⁻)² − Q·P⁻ − QR = 0

This is a quadratic equation in P⁻. The quadratic formula gives two roots. A variance can't be negative, so we keep the + root:

P⁻ = (Q + √(Q² + 4QR)) / 2,    and then P = P⁻ − Q

With Q = 1 and R = 9, as in the two steps above:

P⁻ = (1 + √(1 + 36)) / 2 = (1 + √37) / 2 ≈ 3.54    P ≈ 3.54 − 1 = 2.54  →  σ ≈ 1.59 m
P over 12 steps, starting from 4. Each step it jumps up by Q (predict), then drops (correct). The zigzag settles between about 2.54 and 3.54.

The GPS alone has σ = 3 m. The filter settles at about half that, and it stays there no matter how long the robot drives.

Tuning: two knobs

In a real robot, you do not know the true noise exactly. You choose Q and R, and that choice is called tuning.

R too high (you distrust the GPS): K is small, corrections are gentle and the estimate leans on the smooth prediction. Smooth, but slow to react. Push R far too high and the filter drifts like the encoder.

Q too high (you distrust the encoder): P⁻ balloons, K grows, and the filter snaps to each GPS reading. It gets jumpy, like raw GPS.

For the robot in the picture, the steady-state gain shows this directly:

matched, Q = 0.25, R = 4.84:   K ≈ 0.20
R too high, Q = 0.25, R = 50:   K ≈ 0.07   (laggy)
Q too high, Q = 5, R = 4.84:   K ≈ 0.62   (jumpy)
Same robot, same readings, two bad tunings. Orange: R too high, smooth but lagging behind and wandering off. Grey: Q too high, chasing the green GPS dots. Compare both with the blue line in the first picture.

A neat fact: only the ratio Q/R moves the estimate, as long as P starts at 0. Compare two tunings, (Q, R) = (1, 9) and (2, 18), for the first two steps:

step 1:   Q = 1, R = 9: P⁻ = 0 + 1 = 1, K = 1/10 = 0.1   |   Q = 2, R = 18: P⁻ = 0 + 2 = 2, K = 2/20 = 0.1
step 2:   Q = 1, R = 9: P⁻ = 0.9 + 1 = 1.9, K = 1.9/10.9 ≈ 0.174   |   Q = 2, R = 18: P⁻ = 1.8 + 2 = 3.8, K = 3.8/21.8 ≈ 0.174

Every P⁻ doubled, and so did P⁻ + R, so K stayed the same. The rule: multiply Q and R by the same number, and every K, and so the Kalman line, stays exactly the same. Only the band changes. (If P started at, say, 4, that starting 4 would not double, and the line would change a little.) A good starting point is to set R to the GPS's real variance from its datasheet, and Q to the encoder's real variance per step.

A bigger example: the first six steps of the picture

Here are the first six steps of the robot in the big picture, by hand. These readings come from the picture's own noise draw, not from the lab, so the lab's numbers will be different. The start is known: x̂ = 2 m, P = 0. The filter uses Q = 0.25 and R = 4.84.

step 1: u = 0.28, z = 2.62  →  x̂⁻ = 2.28, P⁻ = 0.25, K ≈ 0.05  →  x̂ ≈ 2.30, P ≈ 0.24
step 2: u = 0.00, z = 2.64  →  x̂⁻ ≈ 2.30, P⁻ ≈ 0.49, K ≈ 0.09  →  x̂ ≈ 2.33, P ≈ 0.44
step 3: u = 0.96, z = 5.66  →  x̂⁻ ≈ 3.29, P⁻ ≈ 0.69, K ≈ 0.13  →  x̂ ≈ 3.59, P ≈ 0.61
step 4: u = 0.12, z = 6.03  →  x̂⁻ ≈ 3.71, P⁻ ≈ 0.86, K ≈ 0.15  →  x̂ ≈ 4.05, P ≈ 0.73
step 5: u = 1.73, z = 5.10  →  x̂⁻ ≈ 5.78, P⁻ ≈ 0.98, K ≈ 0.17  →  x̂ ≈ 5.67, P ≈ 0.81
step 6: u = 1.22, z = 4.88  →  x̂⁻ ≈ 6.89, P⁻ ≈ 1.06, K ≈ 0.18  →  x̂ ≈ 6.53, P ≈ 0.87

The gain starts tiny, because at first the filter trusts its known start more than a 2.2 m GPS. Then K and P climb toward their steady values for this Q = 0.25 and R = 4.84: K ≈ 0.20 and P ≈ 0.98, from the steady-state formula (worked out in the examples below). The computer runs the same three steps, five equations, for every step after this, thousands of times if needed.

Where this shows up

This exact loop runs inside drones for altitude (a barometer, which senses height from air pressure, plus an accelerometer, which senses changes in speed), in robot vacuums for heading, and in phones for the blue dot. Tuning Q and R is the first thing an engineer does when a filter feels "laggy" or "twitchy".

Next: the whole lesson on one page, with a free lab to try any Q, R and sensor noise.

Examples

Example 1 · Two steps from a fresh start

Start at x̂ = 0 m with P = 1. The filter uses Q = 1 and R = 3. Step 1: u = 2 m, z = 2.5 m. Step 2: u = 2 m, z = 3.6 m.

Show solutionHide solution
step 1: x̂⁻ = 2, P⁻ = 2, K = 2/5 = 0.4  →  x̂ = 2 + 0.4 · 0.5 = 2.2 m,   P = 0.6 · 2 = 1.2
step 2: x̂⁻ = 4.2, P⁻ = 2.2, K = 2.2/5.2 ≈ 0.42  →  x̂ ≈ 4.2 + 0.423 · (−0.6) ≈ 3.95 m,   P ≈ 1.27

The second GPS reading was below the prediction, so the estimate moved down. P went 1 → 2 → 1.2 → 2.2 → 1.27: up by Q, down by the correction, again and again.

Example 2 · Where the lab robot's P settles

The lab robot uses Q = 0.25 and R = 4.84. Where does P end up?

Show solutionHide solution
P⁻ = (0.25 + √(0.0625 + 4 · 0.25 · 4.84)) / 2 = (0.25 + √4.9025) / 2 ≈ 1.23
K = 1.23 / (1.23 + 4.84) ≈ 0.20,   P ≈ 1.23 − 0.25 = 0.98  →  σ ≈ 0.99 m

The GPS alone has σ = 2.2 m. The filter settles below 1 m, so its ±2σ band is about ±2 m. That is the band you'll see in the lab once you set R = 4.84 (the lab starts at R = 50).

Example 3 · The tuning knobs on the lab's robot

These numbers come from the lab's longer 80-step run, not the 30-step picture above. The lab robot has a real encoder σ of 0.5 m per step (Q = 0.25) and a real GPS σ of 2.2 m (R = 4.84). With the same noise, compare RMS errors:

Show solutionHide solution
raw GPS: 2.19 m    dead reckoning: 4.94 m
Kalman, R = 4.84 (matched): 0.92 m
Kalman, R = 50 (GPS distrusted): 1.60 m, laggy and drifting
Kalman, Q = 5 (encoder distrusted): 1.52 m, jumpy

Matched noise values beat both sensors by a wide margin. Mistuning either knob throws much of that gain away.

Example 4

Your estimate trails behind the robot every time it speeds up, and the line looks very smooth. Your colleague's estimate shakes around with every GPS reading. Which knob does each of you turn, and which way?

Show solutionHide solution

Smooth but lagging means K is too small: lower R or raise Q, so the filter listens to the GPS more.

Shaky means K is too big: raise R or lower Q, so the filter leans on its prediction more. Only the ratio Q/R matters, so turning either knob works.

Example 5

The filter sits at P = 1 when the robot enters a tunnel. For 10 steps there is no GPS (Q = 0.25). Then a GPS reading arrives with R = 4.84. Find P before that reading, and K and P after it.

Show solutionHide solution

Ten predict steps: P⁻ = 1 + 10 · 0.25 = 3.5 (σ ≈ 1.87 m).

K = 3.5 / (3.5 + 4.84) ≈ 0.42, and P = (1 − 0.42) · 3.5 ≈ 2.03. The first reading after the tunnel gets a big gain, because the filter knows it has been guessing.

Exercise This filter is mistuned: it thinks the GPS has variance R = 50, but the real GPS has σ = 2.2 m. Tune R (or Q) until the Kalman RMS error is below 1.0 m. Remember R is a variance: R = σ². The code prints it, and it is also in the readout under the picture. Edit the code and press Run.
slide_5.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