Kalman Filter, Live
Lesson 3 · State space

State-space models: the state, F, B and H

Slide 3 moved the uncertainty with a motion matrix F. Now we write F, and the rest of the model, properly. Picture a car trip. You are at kilometre 100 on the highway. Where will you be in an hour? You cannot say until you also know your speed. The few numbers you need right now to predict what comes next are the state. The state-space model gives the rules in two lines: the next state is a matrix times the current state, plus the effect of any push you apply. A second line says what the sensor can see. Three matrices, F, B and H, carry all the physics.

Lesson 3 · slide 4State space
xₖ₊₁ = F xₖ + B uₖ    zₖ = H xₖ    F = 1Δt01   H = 10
Key ideas
  1. The state is the smallest set of numbers you need now to predict what happens next.
  2. The transition matrix F steps the state forward by one tick of Δt.
  3. The input matrix B turns a known push u (a motor command) into its effect on the state.
  4. The measurement matrix H picks out the parts of the state a sensor can see.
  5. The same equations work for 2 numbers or 2 million: only the matrix sizes change.

What is the state?

Two robots stand at the same spot on a line. One is parked, the other drives at 3 m/s. One second later they are 3 m apart. Knowing the position alone was not enough to predict the future. You also need the velocity.

The state is the smallest set of numbers you need right now to predict what happens next, given the rules of motion and the inputs you apply. For the robot on a line it is two numbers: x = (position, velocity).

Parked robot: x = (0, 0)  →  1 s later still at 0 m
Moving robot: x = (0, 3)  →  1 s later at 0 + 3 · 1 = 3 m

How many numbers depends on what you track. A drone in 3D needs position and velocity in x, y and z: 6 numbers. Add its three tilt angles and their rates and you have 12. A weather model tracks temperature, pressure and wind at every grid point: millions of numbers. The Kalman filter equations do not care. The matrices get bigger, the structure stays the same.

The discrete model

A computer updates in ticks. Δt (read "delta t") is the length of one tick in seconds. We write the motion one tick at a time:

xₖ₊₁ = F xₖ + B uₖ

The small k counts the ticks: xₖ is the state at tick k, xₖ₊₁ one tick later. F is the transition matrix: the rules of motion for one tick. u is a known input, like a motor command, and B says how it changes the state. A second equation says what a sensor can read:

zₖ = H xₖ

H is the measurement matrix. It selects the parts of the state the sensor can see.

A moving mass: F

No friction, no push. In one tick the position grows by velocity times Δt, and the velocity stays the same:

pvₖ₊₁ = 1Δt01 pvₖ

Row 1 says p + v·Δt. Row 2 says v. That one matrix holds the physical fact "position changes by velocity times time". In lesson 2 the encoder handed us the distance moved, u, and we wrote x⁻ = x + u. Here the filter works out that distance itself, from the velocity it is tracking.

One tick with Δt = 1 s, starting at position 0 and velocity 3. Row 1 gives the new position 1·0 + 1·3 = 3. Row 2 keeps the velocity at 3.

Friction (in F) and a motor push (B)

Friction slows the robot in proportion to its speed. Write its strength as one number, the friction rate c, in units of 1 per second. (Treat c as a given number for the robot.) Each tick, friction removes the fraction c·Δt of the velocity, so the velocity row becomes 1 − cΔt.

Friction rate c = 0.2 per second, Δt = 1 s, so row 2 is 1 − 0.2 = 0.8. The position still grows by 3, but the velocity drops to 0.8 · 3 = 2.4.

A motor gives a known acceleration u, which adds u·Δt to the velocity. Here u is an acceleration, not a distance (in lesson 2 it was a distance). Only the velocity row changes:

xₖ₊₁ = 1Δt01 − cΔt xₖ + 0Δt u
c = 0.2, Δt = 1, u = 1, from (0, 3):   (0 + 3, 0.8 · 3 + 1) = (3, 3.4)

With friction and a steady push, the robot speeds up or slows down until the two balance. The velocity stops changing when the friction loss equals the push:

loss per tick c·v·Δt = gain per tick u·Δt
c·v = u  →  cruising speed v = u / c
c = 0.5, u = 1:   v = 1 / 0.5 = 2 m/s
The lab's robot (c = 0.5 per second, Δt = 0.1 s) starting at 3 m/s. With no push its velocity fades toward 0. With a push u = 1 m/s² it settles at u / c = 2 m/s.

Keep reading with the full course

Finish lesson 3 and you will be able to: track position and velocity in 2D with the matrix Kalman filter.

Still locked here:

  • The rest of this explanation
  • 3 more examples with step-by-step solutions
  • Five extra members-only problems with worked solutions

$20 once, lifetime access. Lessons 1 and 2 stay free.

Examples

Example 1 · Coasting

Δt = 1 s, start at x = (0, 3): position 0 m, velocity 3 m/s, no friction.

Show solutionHide solution
Tick 1: F x = (0 + 1·3, 3) = (3, 3)
Tick 2: (3 + 3, 3) = (6, 3)

Constant velocity, position up by 3 m every second. GPS would read z = H x = 3, then 6.

Example 2 · Friction

Δt = 0.1 s, c = 0.5 per second, start at (0, 3), no push. The velocity row is 1 − 0.5·0.1 = 0.95.

Show solutionHide solution
Tick 1: p = 0 + 0.1·3 = 0.3,   v = 0.95·3 = 2.85
Tick 2: p = 0.3 + 0.1·2.85 = 0.585,   v = 0.95·2.85 = 2.7075

The velocity shrinks by 5% each tick, and the position grows a little less each time. Left alone, the robot coasts to a stop.

Exercise The robot starts at 3 m/s and friction (0.5 per second) slows it down. Find the motor push u that holds it at exactly 3 m/s (change u on its line). Hint: rearrange the cruising-speed rule. Edit the code and press Run.
slide_4.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

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