An Intuitive Introduction to Kalman Filtering · Module 1 of 10

Signals, noise
and sensors

Before we can estimate anything, we need to understand what a sensor actually hands us: the truth, plus an error we can describe but never see directly.

Use the arrow keys or the buttons below to move. Press N for instructor notes.

The whole course in one question

Every module asks the same thing

“How much should I trust this new measurement, compared with what I already believe?”

The answer is always a number between 0 and 1. We will call it the gain, K.

estimate  =  prediction  +  K · (measurement − prediction)

Where we start

In Module 2 you choose K by hand and watch what happens.

Where we finish

By Module 7 the filter computes K itself, at every step, from how uncertain it is. That is the Kalman filter.

The story

Meet the sled

An instrumented sled runs along a straight test track. We want to know where it is, how fast it is going, and how hard it is accelerating.

0 m100200300400500 sensors position sensor: “I think we are at 251 m” true position p = 248.7 m velocity v

The color code on this picture is used in every figure of the course: blue is the truth, red is what a sensor reports, and green will be our estimate.

The run we will study all course

The true motion

The sled starts at rest, then goes through a series of pushes, coasts and one hard brake over 60 seconds.

  • It travels 575 m and peaks at 22 m/s.
  • Acceleration is the cause, velocity is its integral, and position is the integral of velocity.
  • The variety is deliberate. Later modules need the rest period and the changing acceleration levels.
We only know these curves because this is a simulation. A real sled never tells you the truth. All it gives you is sensor readings.
True position, velocity and acceleration of the sled over 60 seconds
Figure from m01_solution.m. Dotted lines mark the phases.
The problem

What the position sensor reports

Five times a second the sensor reports a position. It is never exactly right.

True position and noisy sensor readings, whole run and zoom on the first ten seconds

Over the whole run the readings look perfect, because a few meters of error is invisible beside 575 m of travel. Zoom in and each reading misses by a different amount. That scatter is what this module is about.

Idea 1

Every reading is a draw

Park the sled at 0 m and ask the sensor where it is. Then ask again.

You cannot predict the next reading. But take many, and a stable pattern appears: most land near the truth, a few land far away.

A quantity like this, unpredictable one at a time but with a predictable pattern, is called a random variable.

Try it: take a few single readings first, then add a hundred at a time.

Idea 1, with a name

The normal distribution

The bell-shaped pattern is the normal (or Gaussian) distribution. Two numbers describe it completely:

  • the mean, where it is centered. For our sensor noise that is 0.
  • the standard deviation σ (“one sigma”), how wide it is.

The rule worth memorizing: about 68% of draws fall within ±1σ, 95% within ±2σ, and 99.7% within ±3σ.

Drag the slider. The curve gets wider and lower, but the area under it stays 1 and the percentages never change.

Idea 1, in math

Writing it down

We write “the noise n is normally distributed with mean 0 and variance σ²” as

n~N(0,σ2)

and the height of the bell curve at an error x is the probability density

f(x)=1σ2πe−x22σ2

Standard deviation σ

Same units as the measurement (meters). This is the number people quote and plot.

Variance σ²

Units squared (m²). This is the number the mathematics prefers, because variances of independent errors simply add.

Idea 2

Noise adds to the signal, sample by sample

At each sample time k the sensor reports

zk=pk+nk

measurement = true position + a fresh noise draw.

The noise does not care what the sled is doing. It is the same size at rest as at full speed.

Raise σ and watch the bottom panel. Press “New draw”: the truth stays put, the noise changes completely.

Idea 2, the fine print

What “white” noise means

Noise is white when each sample is independent of every other sample. Knowing this reading’s error tells you nothing about the next one.

Both traces on the right have the same σ. The lower one is not white: its errors linger, so it wanders.

Why it matters: averaging beats white noise quickly. It beats lingering errors slowly, and a constant error not at all.

Until Module 6, our position sensor has white Gaussian noise and nothing else.

Where the sensors live

Strapdown navigation, part 1

In a strapdown system the sensors are bolted directly to the vehicle. They feel what the vehicle feels.

  • An inertial measurement unit (IMU) holds accelerometers and gyroscopes. It needs no outside signal and reports hundreds of times a second.
  • Aiding sensors, such as a satellite (GNSS) receiver, report position or velocity directly, but slowly and with noise.

Our sled is the one-dimensional version: later, one accelerometer along the track; for now, one position sensor.

vehicle body IMU accelerometers + gyros GNSS position fixes fast, smooth, self-contained 100+ readings per second a few readings per second noisy, but does not drift
Where the sensors live

Strapdown navigation, part 2

An accelerometer does not measure position. Position comes from integrating acceleration twice.

accelerometermeasured acceleration ∫ velocity ∫ position small error in error grows with time error grows faster still

The catch

Integration never forgets. Every small sensor error is added in and carried forward, so an inertial solution on its own slowly drifts away from the truth.

The plan

Use the fast inertial sensors to follow the motion and the slow position sensor to pull the drift back. Deciding how hard to pull is the Kalman filter’s job.

A field guide

Common navigation sensors

SensorMeasuresTypical rateMain weaknesses
AccelerometerSpecific force along its axis (acceleration, plus the reaction to gravity)100 to 1000 HzBias, scale factor, noise. Errors grow when integrated.
GyroscopeRotation rate about its axis100 to 1000 HzBias and its slow drift, scale factor, noise. Heading error grows with time.
GNSS receiverPosition and velocity from satellite signals1 to 10 HzMeter-level noise, dropouts, reflections off buildings, delay.
BarometerAir pressure, converted to altitude10 to 50 HzSlow drift with weather, sensitivity to airflow.
MagnetometerDirection of the local magnetic field (heading)10 to 100 HzDisturbed by nearby iron and electric currents.
Odometer / wheel encoderDistance traveled or wheel speed10 to 100 HzWheel slip, tire-size (scale) error, quantization.

Rates and error sizes vary enormously with the grade of the device. The pattern to notice: sensors that are fast and smooth tend to drift, and sensors that do not drift tend to be slow and noisy.

A field guide

How sensors go wrong

Blue is the true signal, red is what the sensor reports.

This course starts with white noise only. Bias and scale factor join in Module 6, and estimating them is where the course ends up.
Worked example

Reading one measurement

Our position sensor has white Gaussian noise with σ = 2 m. It reports

z = 103.1 m

What do we know about the true position?

  • The most likely value is 103.1 m itself.
  • 68% of the time the truth is within ±2 m: 101.1 to 105.1 m.
  • 95% of the time within ±4 m: 99.1 to 107.1 m.
  • Almost always (99.7%) within ±6 m: 97.1 to 109.1 m.
Sensor error over the run with one and three sigma lines
All 301 errors of our run. 70% lie inside ±1σ and 100% inside ±3σ. The largest is 5.5 m.
A first taste of estimation

While the sled sits still, average

For the first 5 seconds the sled is parked at 0 m. The sensor gives us 25 readings of the same position.

One reading is off by about 2 m. But the errors are independent, so highs and lows tend to cancel. The average closes in on the truth.

In our run the average of all 25 readings is −0.12 m, against a truth of 0.

Animation: the running average of the rest readings converges toward zero inside a narrowing band
Animation from MATLAB. The shaded band is the expected one-sigma error of the average.
A first taste of estimation

How much does averaging help?

Averaging N independent readings, each with noise σ, gives an estimate with noise

σavg=σN

Why: variances of independent errors add, so the sum of N readings has variance Nσ². Dividing by N divides the variance by N², leaving σ²/N.

For us: 2 m / √25 = 0.40 m. Repeating the experiment 2000 times in MATLAB gave 0.42 m.

Diminishing returns: to halve the error you need four times the readings.

Your turn

In MATLAB: build the sensor yourself

Open m01_student.m and press Run. It works as it is, with a perfect sensor. Then fill in three short blocks:

  1. TODO 1. Write the noise generator: n draws with standard deviation σ.
  2. TODO 2. Make the sensor readings: truth plus noise.
  3. TODO 3. Estimate the parked position from the rest readings, and predict how good that estimate should be.

Run again after each one. The Command Window prints PASS or FAIL for each TODO.

%% TODO 1 (student): write the noise generator
% Goal:  makeNoise(sigma, n) returns an n-by-1
%        column of white Gaussian noise with
%        mean 0 and standard deviation sigma.
% Hint:  randn(n, 1) returns n samples with
%        standard deviation 1.
% >>> YOUR CODE STARTS HERE
makeNoise = @(sigma, n) zeros(n, 1);
% <<< YOUR CODE ENDS HERE
KinematicsPosition, velocity, acceleration: truth and sensor
ErrorsError over time, and its histogram
Averaging at restRunning average; one reading vs the average
PlaygroundSlide the noise σ and watch everything change
ScoreboardThe course-long error chart
Results

What you should see

Animation: errors arriving over time on the left while their histogram fills in on the right
As readings arrive, their errors fill in the bell curve.
Histogram of the sensor errors with the normal curve
  • Requested σ: 2.00 m. Sample standard deviation of this run: 1.91 m.
  • RMS error of this run: 1.91 m.

They differ slightly from 2.00 because 301 samples is a finite sample. That is expected, not a bug.

The scoreboard

Our starting point: 2.00 m

Scoreboard bar chart with one bar for the raw sensor

Every module ends by adding a bar to this chart: the RMS position error of the best technique we have so far, on this same run.

Bar one is doing nothing at all, just believing the sensor. Averaged over 100 different noise draws it scores 2.00 m, which is the sensor’s σ, as it should be.

Everything from here on is an attempt to get below this bar while the sled is moving.
Check your understanding

Three questions

1. A sensor has σ = 2 m. Roughly how often will a reading be more than 4 m from the truth?

About 5% of the time, or one reading in twenty. 4 m is 2σ, and about 95% of draws fall within ±2σ. At 5 Hz that is roughly one such reading every four seconds.

2. You average 25 readings of the parked sled and get an error of about 0.4 m. How many readings would you need for 0.1 m?

400. The error falls with the square root of the number of readings, so a 4× improvement costs 16× the readings. At 5 Hz that is 80 seconds of sitting still.

3. Your position sensor reads 1.5 m too high on every sample. Will averaging more readings fix it?

No. Averaging only cancels errors that are independent from sample to sample. A constant offset (a bias) is in every reading, so it is in the average too. You would get a very steady, very confident, wrong answer.

What still goes wrong

The sled does not stay parked

Averaging the latest 25 readings while the sled moves lags far behind the truth

Averaging the latest 25 readings was a great idea at rest. Once the sled moves, those readings are of 25 different positions. The average lags behind by up to 54 m, and its RMS error over the run is 29.5 m: far worse than the raw sensor’s 2.00 m.

Next, Module 2: can we smooth the noise without falling behind? We will blend each new reading into the old estimate with a gain we choose, and meet the trade-off that the rest of the course is about.