Kalman Filter, Live
Lesson 2 · GPS only

GPS only: no drift, but every reading is noisy

Slide 1 showed that adding up encoder moves drifts further and further from the truth. Now the robot gets a second sensor, a GPS, which measures its position directly. Picture a friend across the room who calls out where you are every few seconds. Your friend is never lost, but each call is a bit off, and the next one is off by a different amount. So the GPS never drifts: on average it is right. But any single reading can be metres away, and trusting the GPS alone makes the robot seem to hop around the room.

Lesson 2 · slide 2GPS only
z[n] = x_true[n] + w[n]    w[n] ~ N(0, R)    error variance = R at every step
Key ideas
  1. A measurement z is the truth plus fresh noise w, with variance R.
  2. The noise is centred on zero, so the readings scatter evenly around the truth.
  3. Each reading has the same variance R. The error does not grow with time.
  4. But neighbouring readings disagree, so a GPS-only estimate is jumpy.

A friend calling out where you are

Back to walking with your eyes closed. Now a friend across the room shouts your position every few seconds. Your friend is never lost. But they are far away and guess a bit wrong each time: "3 metres!", then "1 metre!", then "4 metres!", while you only moved a little. You will never end up far from the truth. You will also never know exactly where you are.

A GPS receiver works the same way. It works out its position from satellite signals, and each answer comes with a small random error.

The measurement model

Write the GPS reading at step n as z[n]. It is the true position plus some noise:

z[n] = x_true[n] + w[n],    w[n] ~ N(0, R)

The squiggle ~ is read as in lesson 1: "follows" (or "is distributed as"). Here each step's noise w[n] is a fresh random value that follows the bell N(0, R). N(0, R) is the Gaussian from lesson 1, with mean 0 and variance R. We call R the measurement noise. A GPS with σ = 2.2 m has R = 2.2² = 4.84 m². If the robot is really at 10 m, a reading of 12.3 m is quite normal: the error is +2.3 m, about one σ.

Mean 0 is the important part. It says the GPS is centred on the truth: it is too high as often as it is too low. Engineers call this unbiased.

The robot sits at 10 m. Each dot is one GPS reading. They pile up around 10 m, and about 95% land within ±4.4 m (two σ).

No drift

Every reading gets a brand-new noise sample. Nothing carries over from the last step. So the error at step 1000 is no bigger than the error at step 1: the variance is R = 4.84 at both. That is exactly what dead reckoning could not do.

The same robot as on slide 1. Dashed: the truth. Green dots: GPS readings. They scatter around the truth by the same amount from start to end.

But jumpy

The same fresh noise makes the readings jump. Two readings taken one step apart have independent errors, so the gap between them has variance R + R = 2R. Subtracting an independent error adds its variance too, because −w is just as spread out as w. For example, with R = 4:

variance of the jump = 4 + 4 = 8  →  σ = √8 ≈ 2.83 m

The robot moved about 0.5 m, yet the GPS can easily say it moved 3 m, or backwards.

Measuring how good an estimate is

To score an estimate against the truth, we use the RMS error (root mean square): square each error, take the mean, then the square root. It is like an average error in metres that punishes big misses. Errors of 1, −2, 2 and −1 m give:

squares: 1, 4, 4, 1  →  mean 10 / 4 = 2.5  →  RMS = √2.5 ≈ 1.58 m

For a GPS used on its own, the RMS error is close to its σ. In the 30-step picture above it is about 2.4 m, for a GPS with σ = 2.2 m. The lab runs a longer 80-step track with different noise, so its numbers differ a little. Under the lab's picture, the readout shows the RMS error of every line and every dot series against the dashed truth. The lab computes it for you.

Two flawed sensors

So the encoder is smooth but drifts. The GPS is right on average but jumpy. Each one's weakness is the other's strength.

Both sensors on the same robot. Orange: dead reckoning, smooth but drifting away. Green dots: GPS, jumpy but always around the truth.

Over the 30 steps in this picture, dead reckoning has an RMS error of about 3.1 m and the GPS about 2.4 m. Neither is good. The rest of this lesson combines them: the encoder smooths out the GPS jumps, and the GPS pins down the encoder drift.

A bigger example: five readings of a parked robot

The robot is parked at exactly 10 m. A GPS gives five readings: 12.1, 8.4, 10.9, 7.6 and 11.5 m.

errors: +2.1, −1.6, +0.9, −2.4, +1.5  →  mean error = 0.5 / 5 = 0.1 m
squares: 4.41 + 2.56 + 0.81 + 5.76 + 2.25 = 15.79  →  RMS = √(15.79 / 5) ≈ 1.78 m
jumps between readings: −3.7, +2.5, −3.3, +3.9 m, while the robot did not move at all

The mean error is almost zero, so the GPS is centred. But single readings are almost 2 m off, and the track jumps by more than 3 m. A phone logs thousands of readings like these, with the same picture every time.

Where this shows up

A phone GPS is typically good to a few metres. Standing still, the blue dot on your map still wanders around. Drones see the same thing as altitude jitter. That is why no serious drone or car uses raw GPS alone.

Next: how do we use both sensors? The Kalman filter works in two steps, and the first one, predict, uses the encoder.

Examples

Example 1 · How far off one reading can be

A GPS has σ = 2.2 m, so R = 4.84 m².

Show solutionHide solution
about 95% of readings fall within ±2σ = ±4.4 m of the truth
at step 1 and at step 1000: the same ±4.4 m

The band is wide, but it stays the same width forever.

Example 2 · The robot that seems to jump

Same GPS. The robot moves 0.4 m between two readings. How much do the two readings disagree?

Show solutionHide solution
error of the difference: variance R + R = 2 · 4.84 = 9.68
σ = √9.68 ≈ 3.11 m  →  about one jump in three is bigger than 3 m

The real move is 0.4 m. The noise in the jump is almost eight times bigger than the move itself.

Example 3 · Averaging helps, until you move

The robot is parked. Average 10 GPS readings.

Show solutionHide solution
variance of the average: R / 10 = 4.84 / 10 = 0.484  →  σ ≈ 0.70 m

Much better than 2.2 m. But if the robot is moving, the old readings point to old positions, and the average lags behind. To average a moving robot, you need to know how it moved between readings. That is the encoder's job.

Example 4

Four GPS readings have errors +2, −1, −3 and +2 m. Find the mean error and the RMS error.

Show solutionHide solution

Mean: (2 − 1 − 3 + 2) / 4 = 0. The readings are centred on the truth.

RMS: squares 4, 1, 9, 4 add to 18. The mean is 18 / 4 = 4.5, and √4.5 ≈ 2.12 m. A mean of zero does not mean the errors are small.

Example 5

Dead reckoning with Q = 0.25 per step starts with P = 0. The GPS has R = 4.84. After how many steps is dead reckoning less certain than a single GPS reading?

Show solutionHide solution

Dead reckoning has P = 0.25 · n. We need 0.25 · n > 4.84, so n > 19.36.

From step 20 on, one raw GPS reading is more certain than the encoder estimate. Before that, the encoder wins. Neither wins all the time, which is a strong hint to use both.

Exercise Fit a better GPS module. Set gps_std = 0.5 and check that the RMS error in the readout drops to about 0.5 m. Edit the code and press Run.
slide_2.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