Kalman Filter, Live
Lesson 1 · Data fusion

Data fusion: combining two sensors with the gain K

Slide 4 said to lean toward the sensor with the smaller variance. Now we find out exactly how far. Two friends guess the price of a used car, and one of them sells cars for a living. You would not just take the middle of their guesses. You would lean toward the expert. That is a weighted average, and one number, the gain K, decides how far to lean. The result beats either guess alone, and this rule is the heart of the Kalman filter.

Lesson 1 · slide 5Data fusion
K = σ₁² / (σ₁² + σ₂²)    x̂ = z₁ + K (z₂ − z₁)    σ̂² = σ₁² σ₂² / (σ₁² + σ₂²)
Key ideas
  1. Fuse two readings with a weighted average: the sensor with the smaller variance gets the bigger weight.
  2. The gain K (between 0 and 1) says how far to move from reading 1 toward reading 2.
  3. The fused estimate is always more certain than either sensor: its variance is smaller than both.
  4. Equivalently, precisions add (precision = 1/variance): 1/σ̂² = 1/σ₁² + 1/σ₂².

Two thermometers in one room

A cheap thermometer says 10°. A better one says 16°. Both are aimed at the same room, so the true temperature is one number. What is our best guess?

The plain average, 13°, treats both thermometers as equally good. But they are not. Say the cheap one has variance 4 and the better one variance 1, a quarter as much. Then the better reading should count for more. So we use a weighted average, with weights set by the variances.

The gain K: how far to lean

Write sensor 1's reading as z₁ with variance σ₁², and sensor 2's as z₂ with variance σ₂². Our fused estimate of the truth x is written x̂ (say "x-hat"). The hat means "our estimate of". Start at z₁ and move toward z₂ by a fraction K of the gap:

x̂ = z₁ + K (z₂ − z₁)    with    K = σ₁² / (σ₁² + σ₂²)

For the two thermometers, σ₁² = 4 and σ₂² = 1:

K = 4 / (4 + 1) = 0.8  →  x̂ = 10 + 0.8 · (16 − 10) = 10 + 4.8 = 14.8°
The fused bell starts at sensor 1 and slides 80% of the way toward sensor 2 (K = 0.8). It ends up narrower than both.

This is the promised weighted average. Expanded, x̂ = (1 − K) z₁ + K z₂: z₂ gets weight K = σ₁²/(σ₁²+σ₂²) and z₁ gets 1 − K = σ₂²/(σ₁²+σ₂²). The noisier sensor 1 is, the more weight sensor 2 gets, and the other way round.

Why is sensor 1's variance on top? K grows when sensor 1 is noisy, so we lean further toward sensor 2. Of all the weights you could pick, this K gives the smallest fused variance. That is why it is the right one.

Here is why, with the rules from slide 3. Write x̂ = (1 − K) z₁ + K z₂. Scaling z₁ by (1 − K) gives variance (1 − K)² σ₁². Scaling z₂ by K gives K² σ₂². The two sensors are independent, so the variances add:

fused variance = (1 − K)² σ₁² + K² σ₂²

Try it with the two thermometers, σ₁² = 4 and σ₂² = 1:

K = 0.5:   0.25·4 + 0.25·1 = 1 + 0.25 = 1.25
K = 0.6:   0.16·4 + 0.36·1 = 0.64 + 0.36 = 1.00
K = 0.7:   0.09·4 + 0.49·1 = 0.36 + 0.49 = 0.85
K = 0.8:   0.04·4 + 0.64·1 = 0.16 + 0.64 = 0.80   ← the smallest
K = 0.9:   0.01·4 + 0.81·1 = 0.04 + 0.81 = 0.85
K = 1.0:   0·4 + 1·1 = 1.00

The smallest value sits exactly at K = 4 / (4 + 1) = 0.8, the K from the rule.

Look at the two extremes. If sensor 2 is perfect (σ₂² = 0), then K = 1 and we jump all the way to z₂. If sensor 2 is useless (σ₂² huge), then K is almost 0 and we stay at z₁. When both are equally good, K = ½ and we land exactly halfway.

σ₁² = 4, σ₂² = 0:   K = 4 / 4 = 1    σ₁² = 4, σ₂² = 400:   K = 4 / 404 ≈ 0.01    σ₁² = σ₂² = 9:   K = 9 / 18 = 0.5
Two equally good sensors read 18 and 24, both with variance 9. K = 0.5, so the fused bell lands halfway, at 21, with half the variance: 4.5.

K is the one knob that decides how much to believe the new reading. Remember it: it comes back in every Kalman filter, where it is called the Kalman gain.

The fused estimate is more certain than both

The fused variance is written σ̂² (the hat again means it belongs to our estimate):

σ̂² = σ₁² σ₂² / (σ₁² + σ₂²)

For the two thermometers: σ̂² = 4 · 1 / (4 + 1) = 0.8. That is smaller than even the good sensor's 1.

It is easiest to see why this is small by flipping both sides over. One over a variance is called the precision: how much information a sensor carries. The rule becomes

1/σ̂² = (σ₁² + σ₂²) / (σ₁² σ₂²)
     = σ₁² / (σ₁² σ₂²) + σ₂² / (σ₁² σ₂²)    (split the top)
     = 1/σ₂² + 1/σ₁²    (cancel)

Precisions add. Each sensor puts its information on the pile, so the total is always bigger than either part. A bigger precision means a smaller variance. Adding an independent measurement can never make you less certain.

σ₁² = 3, σ₂² = 1:   1/3 + 1/1 = 4/3  →  σ̂² = 3/4 = 0.75,   below both 3 and 1

Fusing at every moment

A thermometer does not read once. It reads again and again while the room slowly warms. We can fuse the two sensors at every moment with the same K = 0.8.

one moment: z₁ = 19.1°, z₂ = 20.3°  →  x̂ = 19.1 + 0.8 · (20.3 − 19.1) = 19.1 + 0.96 = 20.06°
A room warms from 18° to 22°. Sensor 1 (dots) scatters by about 2°, sensor 2 (red line, drawn as a line so the two sensors are easy to tell apart) by about 1°. The fused blue line stays closest to the dashed truth.

A bigger example: three thermometers

Add a third thermometer: 13° with variance 2. Fuse the first two, then fuse the result with the third, using the same rule twice.

step 1: 10° (variance 4) with 16° (variance 1)  →  14.8°, variance 0.8
step 2: K = 0.8 / (0.8 + 2) ≈ 0.286  →  x̂ = 14.8 + 0.286 · (13 − 14.8) ≈ 14.29°
variance: 0.8 · 2 / (0.8 + 2) ≈ 0.571
check with precisions: 1/4 + 1/1 + 1/2 = 1.75,   1 / 1.75 ≈ 0.571 ✓
Step 2: the result of the first two thermometers is fused with the third. K ≈ 0.29, so it moves only a little toward 13°, and the bell gets narrower again.

The order does not matter, and it works the same for 10 or 100 sensors: add up all the precisions, and the fused variance is one over the total.

Where this shows up

A phone fuses GPS with Wi-Fi positions. A drone fuses its barometer with its GPS altitude. A robot fuses two wheel encoders. Each time, the same rule decides how much to trust each source. In lesson 2 the "first sensor" becomes the filter's own prediction, and this rule turns into the correct step of the Kalman filter.

Coming up in lesson 3: this same rule fuses whole lists of numbers at once, like position and speed together. The variances become a matrix, and K becomes a matrix too.

Next: the recap puts all of lesson 1 on one page: noise, the bell curve, sensor quality and the fusion rule.

Examples

Example 1 · The two thermometers

z₁ = 10° with σ₁² = 4 (σ = 2°), z₂ = 16° with σ₂² = 1 (σ = 1°).

Show solutionHide solution
K = 4 / (4 + 1) = 0.8
x̂ = 10 + 0.8 · (16 − 10) = 14.8°
σ̂² = 4 · 1 / (4 + 1) = 0.8

The estimate leans toward the better thermometer, and its variance 0.8 is smaller than even the good sensor's 1. Check with precisions: 1/4 + 1/1 = 1.25, and 1/1.25 = 0.8.

Example 2 · Two equally good sensors

z₁ = 20 and z₂ = 22, both with variance 9.

Show solutionHide solution
K = 9 / 18 = 0.5  →  x̂ = 21, the plain average
σ̂² = 81 / 18 = 4.5, half of 9

Two equal sensors halve the variance. That is why averaging repeated readings works.

Example 3 · A good sensor and a poor one

A GPS gives 50 m with variance 25. A laser gives 47 m with variance 1.

Show solutionHide solution
K = 25 / 26 ≈ 0.96  →  x̂ = 50 + 0.96 · (47 − 50) ≈ 47.1 m
σ̂² = 25 / 26 ≈ 0.96

The fused value is almost the laser's. The GPS still helps a little: 0.96 is below the laser's 1. K and σ̂² are both 25/26 here. That is no accident: with σ₂² = 1, the formula σ₁²·σ₂² / (σ₁² + σ₂²) becomes σ₁² / (σ₁² + 1), which is exactly K.

Example 4

Sensor 1 reads 4.0 with variance 2. Sensor 2 reads 7.0 with variance 6. Find K, the fused estimate and its variance.

Show solutionHide solution

K = 2 / (2 + 6) = 0.25. x̂ = 4 + 0.25 · 3 = 4.75. σ̂² = 2 · 6 / 8 = 1.5, smaller than both 2 and 6.

Example 5

For which σ₂² does the fused estimate land exactly a quarter of the way from z₁ to z₂, if σ₁² = 3?

Show solutionHide solution

We need K = 3 / (3 + σ₂²) = 0.25, so 3 + σ₂² = 12 and σ₂² = 9. Sensor 2 must be three times worse in variance.

Exercise Make sensor 2 four times worse in variance (set var2 = 4.0). Now both sensors have variance 4, so K = 0.5 and the fused estimate moves to the halfway point, 13°. 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 1? 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