Kalman Filter, Live
Lesson 4 · The extended Kalman filter

The extended Kalman filter: two changes to the update

Slide 3 showed how sure the filter should be about the radar's next reading. That was the last missing piece. Think of a recipe you know by heart: swap butter for oil, and two lines of the recipe change while the method stays the same. From now on we call the state s, as on lesson 3's rocket page, so that x and y can be its position. The extended Kalman filter (EKF) is lesson 3's matrix filter with two changes, ① and ②. Predict is untouched. In the update, ① the predicted reading uses the real radar formula h(s), and ② the Jacobian HJ, recomputed at every step, takes the place of H. The gain, the correction and the shrinking of P are the filter you already know.

Lesson 4 · slide 4The EKF
① ν = z − h(s⁻)    ② S = HJ P⁻ HJᵀ + R,   K = P⁻ HJᵀ S⁻¹    s = s⁻ + Kν    P = (I − K HJ) P⁻
Key ideas
  1. Predict is the same as before: s⁻ = F s + B u (B u = 0 when there is no input), P⁻ = F P Fᵀ + Q.
  2. Change ①: predict the reading with the real sensor formula, h(s⁻) = distance from s⁻ to the radar. The innovation ν = z − h(s⁻) (ν is Greek "nu") is how wrong that prediction was.
  3. Change ②: wherever the filter used H, use the Jacobian HJ, worked out fresh at s⁻ each step.
  4. The gain, the state correction and the covariance update keep their exact form.
  5. Along the line to the radar, the update is the fusion rule from lesson 1: the prediction's variance and the radar's variance combine.

Side by side

The state is s, and its predicted position is s⁻ = (x̂⁻, ŷ⁻). The hat means "estimate" and the minus means "before the reading". B u is the effect of a known input, like an encoder step; B u = 0 when there is no input. Here is the linear filter from lesson 3 next to the EKF. The predict step is identical. The update has two changes, marked ① and ②; change ② touches three lines.

Predict (both):   s⁻ = F s + B u    P⁻ = F P Fᵀ + Q
Innovation   linear: ν = z − H s⁻   │   EKF ①: ν = z − h(s⁻)
Innovation variance   linear: S = H P⁻ Hᵀ + R   │   EKF ②: S = HJ P⁻ HJᵀ + R
Gain   linear: K = P⁻ Hᵀ S⁻¹   │   EKF ②: K = P⁻ HJᵀ S⁻¹
Correct (both):   s = s⁻ + K ν
Shrink   linear: P = (I − K H) P⁻   │   EKF ②: P = (I − K HJ) P⁻

S is the variance of the innovation: the prediction's doubt about the reading (slide 3) plus the radar's own noise R. With one radar S is a single number, so S⁻¹ is plain division by S.

Change ①: predict the reading honestly

The innovation ν (Greek "nu") asks "how surprised am I by this reading?" To answer it, the filter needs its best prediction of the reading. For a radar that is the real distance from the predicted position s⁻ = (x̂⁻, ŷ⁻) to the radar, h(s⁻) = √((x̂⁻ − a)² + (ŷ⁻ − b)²) for a radar at (a, b). We have the true formula, so we use it. The tangent is only needed where the filter has to move uncertainty around.

Radar at the origin, s⁻ = (3, 4), reading z = 5.3 m:   h(s⁻) = √(9 + 16) = 5  →  ν = 5.3 − 5 = 0.3 m

Change ②: the Jacobian, fresh every step

S, K and the new P all push uncertainty through the sensor. That needs a matrix, so they use the tangent: HJ = (x̂⁻ − a)/r̂(ŷ⁻ − b)/r̂00. As the robot moves, the direction to the radar changes, so HJ changes. The filter recomputes it at s⁻ on every step. That is the "extended" part.

At s⁻ = (3, 4): HJ = 0.60.8    later at s⁻ = (8, 6): r̂ = 10,  HJ = 0.80.6

(If the motion is nonlinear too, say a car that turns, the same trick applies to predict: s⁻ = f(s) with the real motion formula, and its Jacobian in place of F. In this lesson the motion stays linear.)

One update, with numbers

As on slide 3, the state here is just the position (x, y), so HJ drops its velocity zeros and has two entries. The radar is at the origin. The prediction is s⁻ = (3, 4) with P⁻ = diag(0.25, 0.64) (diag means a matrix with these numbers on the diagonal and zeros elsewhere), as on slide 3. The radar reads z = 5.3 m, with noise variance R = 0.25 (σ = 0.5 m).

① h(s⁻) = √(9 + 16) = 5  →  ν = 5.3 − 5 = 0.3 m
② HJ = 0.60.8    S = 0.4996 + 0.25 = 0.7496
P⁻ HJᵀ = (0.25 · 0.6, 0.64 · 0.8) = (0.15, 0.512)  →  K = (0.15, 0.512) / 0.7496 = (0.2001, 0.6830)
s = (3, 4) + 0.3 · (0.2001, 0.6830) = (3.0600, 4.2049)
K HJ = 0.12010.16010.40980.5464 (column K times row HJ),   I − K HJ = 0.8799−0.1601−0.40980.4536
P = (I − K HJ) P⁻ = 0.2200−0.1025−0.10250.2903

The new position is 5.2005 m from the radar: between the predicted 5 m and the measured 5.3 m, and nearer the 5.3 m. The estimate moved mostly in y, because y was less certain and points more along the line to the radar.

Look only along the line to the radar and this is lesson 1's fusion rule. The prediction says 5 m with variance 0.4996; the radar says 5.3 m with variance 0.25. The lesson 1 gain is 0.4996 / 0.7496 = 0.67, so the fused range is about 5 + 0.67 · 0.3 = 5.2 m.

Along the line of sight the EKF fuses two bells, as in lesson 1: the prediction (5 m, variance 0.50) and the radar (5.3 m, variance 0.25). The result is 5.2 m with variance 0.17.

P now has a negative covariance (the sign depends on where the radar is): the ellipse has been squeezed along the line of sight and left wide around the circle, where a range says nothing.

After the update the robot sits 5.2 m from the radar. The pale band is the radar's ± 0.5 m: the reading pins the distance, but not the direction around the circle.

The lab runs this exact update and draws both ellipses. Make R tiny and the new ellipse becomes a thin sliver: the radar pins the distance, but not the direction.

The EKF running, step after step

One update is one step. A real filter runs the loop again and again. Take the simplest case: a robot on a straight rail 5 m from the radar post. The rail is the line y = 5 and the radar sits at the origin, so the state s is one number: the position x along the rail. Each step (1 s) the robot moves about 1 m. The encoder measures that step as u, with σ = 0.5 m of noise, and the radar reads the range with σ = 0.3 m.

Predict: s⁻ = s + u, where u is the encoder's step;   P⁻ = P + 0.5²
h(x) = √(x² + 25),   HJ = x / √(x² + 25)
At x = −12: HJ = −12 / 13 = −0.92    at x = 0: HJ = 0    at x = 12: HJ = 0.92

At x = 0 the robot passes right in front of the radar. There the range hardly changes when x changes (the slope is 0), so the radar can't help, and the filter's doubt grows for a few steps.

The error of each estimate (0 = the truth). The encoder alone drifts away. The EKF stays close, and its ± 2σ band widens around step 12, when the robot passes in front of the radar.

Over the 30 steps in this figure the EKF is off by 0.29 m on average (RMS), dead reckoning by 0.73 m.

Keep reading with the full course

Finish lesson 4 and you will be able to: track a robot from range-only radars with an EKF.

Still locked here:

  • The rest of this explanation
  • 4 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 · The full update

s⁻ = (3, 4), P⁻ = diag(0.25, 0.64), radar at the origin, z = 5.3, R = 0.25.

Show solutionHide solution
ν = 0.3,   HJ = [0.6, 0.8],   S = 0.7496
K = (0.2001, 0.6830)  →  s = (3.0600, 4.2049), 5.2005 m from the radar
Exercise Give the filter a much sharper radar (set R = 0.01 on the marked line) and watch the new ellipse squash flat along the line to the radar. 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