Skip to the lesson
FixTheRobot Explore Robotics / Perception / P7 Multi-Object Tracking and Temporal Perception / P7-T4

Perception · Theory · P7

P7-T4 — Simple Filtering and Kalman Filter Intuition

The Kalman filter is a weighted average whose weights come from how sure you are of each side.

  • Lesson 4 of 6
  • Module 7: Multi-Object Tracking and Temporal Perception
  • Theory
  • About 9 minutes to read

The alpha-beta filter leaves the gains to the designer. The Kalman filter computes them. It keeps, besides the state, a number that says how uncertain the state is, and at every step it weighs the prediction and the measurement by their uncertainties: believe the measurement more when the prediction is shaky, believe the prediction more when the measurement is noisy. This lesson builds the filter from the simplest case, two Gaussian estimates of one number, to the two-state constant-velocity filter that Lab P7-L3 asks you to write, and shows how to check that its noise settings are consistent with the data without any ground truth.

Learning objectives

After this lesson, you should be able to:

  • Fuse a prediction and a measurement of one quantity by inverse-variance weighting and compute the Kalman gain.
  • Write the predict and update equations of the constant-velocity Kalman filter and say what each term does.
  • Explain the roles of the process noise Q and the measurement noise R.
  • Use the normalised innovation squared to check whether the filter’s noise settings fit the data.

The idea: two noisy opinions, one better opinion

A prediction and a measurement are two opinions about where the object is now. Each is uncertain, and the uncertainty can be written as a variance: small for confident, large for doubtful. If you trust the one with the smaller variance more, you get an estimate that sits nearer to it, and, as the figure shows, the combined opinion is less uncertain than either.

Fusing a prediction with a measurementThree bell curves of position. The prediction is centred at 12 with variance 4 and is wide. The measurement is centred at 13.5 with variance 1 and is narrower. The fused estimate is centred at 13.2 with variance 0.8, the narrowest, and lies close to the measurement because it is the more certain of the two.89101112131400.1250.250.3750.5positiondensity orange: prediction, mean 12, variance 4blue: measurement, mean 13.5, variance 1green: fused, mean 13.2, variance 0.8 The result sits nearer to the more certain sourceand is more certain than either.
A wide prediction (variance 4) and a narrower measurement (variance 1) combine into an estimate near the measurement with a variance of 0.8, smaller than both.

Predict, update, repeat

The filter is a loop of two steps.

  1. Predict. Move the state forward with the motion model. The uncertainty grows, because the future is less certain than the present: the prediction adds the process noise Q, which represents the random changes of velocity that the model does not capture, and the uncertainty about the velocity leaks into the position.
  2. Update. Compare the new measurement with what was predicted. The difference is the innovation. Correct the state by a fraction of the innovation, the Kalman gain, and shrink the uncertainty, because a new piece of information has been absorbed.

The gain is large when the prediction is uncertain and the measurement is precise, and small in the opposite case. It starts high, when the filter knows little, and settles. That is the difference from the alpha-beta filter, whose gains are fixed.

The two knobs

Measurement noise R is how much to trust each measurement. It can often be taken from the detector’s known accuracy. Process noise Q is how much to trust the motion model, and it is harder: an object that can accelerate sharply needs a large Q, which makes the filter follow the measurements; a very predictable one allows a small Q and heavy smoothing. Too small a Q makes the filter over-confident: it stops listening, lags behind manoeuvres, and the gate based on its uncertainty becomes too narrow. Too large a Q passes the noise through.

The mathematics: from one number to the full filter

One quantity, two estimates. A prediction has mean xp and variance Pp; a measurement has value z and variance R. The fused estimate weights each by the inverse of its variance:

fusion of a prediction and a measurement
K=PpPp+RK=\frac{P_{p}}{P_{p}+R}
x=xp+K(z−xp)x=x_{p}+K(z-x_{p})
P=(1−K) PpP=(1-K)\,P_{p}
K
the Kalman gain, between 0 and 1: the share of the innovation (z − xp) that is believed. It tends to 1 when R is much smaller than Pp and to 0 when R is much larger.
x, P
the fused mean and variance. P is always smaller than both Pp and R: information has been added.

The constant-velocity filter. For the state x = (position, velocity)ᵀ with covariance P (a 2 × 2 matrix), transition F = [[1, Δt], [0, 1]] and observation H = [1, 0]:

Predict
x←F xx\leftarrow F\,x
P←F P F⊤+QP\leftarrow F\,P\,F^{\top}+Q
Update
y=z−H xy=z-H\,x
S=H P H⊤+RS=H\,P\,H^{\top}+R
K=P H⊤ S−1K=P\,H^{\top}\,S^{-1}
x←x+K yx\leftarrow x+K\,y
P←(I−K H) PP\leftarrow (I-K\,H)\,P
P
the state covariance: its diagonal holds the variances of position and velocity, and the off-diagonal term couples them (a position error and a velocity error are related).
Q
the process-noise covariance. For random acceleration of variance q, Q = q · [[Δt⁴/4, Δt³/2], [Δt³/2, Δt²]].
R
the measurement variance (a single number for a scalar position).
y
the innovation: the measurement minus the predicted measurement.
S
the innovation covariance: how large an innovation to expect. It is also used for the gate: d² = y² / S is the squared statistical distance of Lesson P7-T2.
K
the Kalman gain, here a 2 × 1 vector: one component corrects the position and the other the velocity.

Consistency check. If the filter’s model and noise settings are right, the innovations are zero-mean with variance S, so the normalised innovation squared NIS = y² / S has an average of 1 for a scalar measurement. An average well above 1 means the filter is over-confident (Q or R too small); well below 1, under-confident. This test needs no ground truth (Lab P7-L3, stage 3).

Worked examples: one number, then one full step

One number. The predicted position is 12 with variance Pp = 4. The detector reports 13.5 with variance R = 1. Then

worked fusion
K=44+1=0.8K=\frac{4}{4+1}=0.8
x=12+0.8×(13.5−12)=13.2x=12+0.8\times (13.5-12)=13.2
P=(1−0.8)×4=0.8P=(1-0.8)\times 4=0.8

This is the picture in the figure. The estimate is close to the measurement because the measurement was four times as certain, and the variance 0.8 is below both 4 and 1.

One full two-state step. The state is x = (10, 2) with P = [[1, 0], [0, 1]]; Δt = 1, q = 0.4, R = 1, and the measurement is z = 13.5.

One predict and update step of the constant-velocity Kalman filter
StepCalculationResult
Predict statex ← F x(12, 2)
F P Fᵀ[[1 + 1, 0 + 1], [0 + 1, 1]][[2, 1], [1, 1]]
Q0.4 × [[0.25, 0.5], [0.5, 1]][[0.1, 0.2], [0.2, 0.4]]
Predicted PF P Fᵀ + Q[[2.1, 1.2], [1.2, 1.4]]
Innovation covarianceS = 2.1 + 13.1
GainK = (2.1, 1.2) / 3.1(0.677, 0.387)
Innovationy = 13.5 − 121.5
Updated state(12 + 0.677 × 1.5, 2 + 0.387 × 1.5)(13.02, 2.58)
Updated PP00 = (1 − 0.677) × 2.1; P11 = 1.4 − 0.387 × 1.2P00 = 0.677, P11 = 0.935

The position moved by 1.02 of the 1.5 innovation. The velocity rose from 2 to 2.58 although only the position was measured: the coupling term in P (1.2) lets a position surprise change the velocity. The position variance dropped from 2.1 to 0.68, and the velocity variance only from 1.4 to 0.94, because a single position measurement says little about velocity. After several steps the velocity variance falls further.

The check. The NIS for this step is y² / S = 1.5² / 3.1 = 0.73, near 1, as expected for a consistent filter (one value says little; the average over a run is what matters).

Engineering implications and limitations

Set R from the detector, Q from the motion. Measurement noise can be measured on a still target. Process noise is a judgement about how much the object can accelerate in one step: choose q = σa², with σa about half of the largest change of velocity per step you expect, then tune with the NIS.

Tune with the innovations. Plot the NIS over time on logged data. Persistently above 1: raise Q or R. Persistently below: lower them. This is the tool that works on a real robot, where truth is absent.

Use the covariance in the gate. The innovation covariance S already combines the prediction and the measurement uncertainty; gate on yᵀS⁻¹y and the gate widens when a track coasts (Lesson P7-T2).

Keep P well behaved. In long runs, round-off can make P lose symmetry or positive definiteness. Use the symmetric (Joseph) form of the covariance update or enforce symmetry, and clip variances to a sane range.

Skip the update, not the predict. On a frame with no detection, run the predict step only; the covariance grows and the gate widens, which is the right behaviour for a coasting track (Lesson P7-T5).

Know the filter’s assumptions. It is optimal for linear models and Gaussian noise. Detector errors that are heavy-tailed (an occasional wild box) break the Gaussian assumption; use the gate to reject outliers, or a robust update.

Common misconceptions

“The Kalman filter removes noise.”

It weighs a model against measurements. If the model is wrong (a manoeuvre) the filter lags; if Q is too large it simply passes the noise.

“The gain is a constant I choose.”

The gain is computed from the covariances and changes over time, starting high and settling. What you choose are Q and R.

“A small P means the estimate is accurate.”

It means the filter believes it is accurate. Over-confident settings give a small P and a large error. Check consistency with the NIS.

“The velocity must be measured.”

The filter infers it from the changes of position through the coupling in P; no velocity sensor is needed.

Engineering takeaways

  1. Fusing a prediction and a measurement weights them by inverse variance: K = Pp/(Pp + R), P = (1 − K)Pp; in the example 12 and 13.5 give 13.2 with variance 0.8.
  2. The filter alternates predict (x ← Fx, P ← FPFᵀ + Q: uncertainty grows) and update (gain from P and R: uncertainty shrinks).
  3. The innovation covariance S = HPHᵀ + R is used both for the gain and for the statistical gate.
  4. Q says how much to trust the motion model, R how much to trust the measurement; too small a Q makes the filter over-confident and slow to follow manoeuvres.
  5. The mean NIS (y²/S) should be near 1; use it to tune on real data without ground truth.

References and further reading

The original paper, textbooks and a standard tutorial. The numbers in the examples were computed by hand and checked by running the equations.

  • Kalman, R. E. (1960). A New Approach to Linear Filtering and Prediction Problems. Journal of Basic Engineering.
  • Bar-Shalom, Y., Li, X.-R. and Kirubarajan, T. (2001). Estimation with Applications to Tracking and Navigation. Wiley: the filter, noise models and consistency tests (NIS).
  • Thrun, S., Burgard, W. and Fox, D. (2005). Probabilistic Robotics. MIT Press: Gaussian filters.
  • Welch, G. and Bishop, G. (2006). An Introduction to the Kalman Filter. UNC Chapel Hill technical report TR 95-041.

Knowledge check

Three conceptual questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

A prediction of 5.0 has variance 1 and a measurement of 6.0 has variance 3. What are K, the fused mean and the fused variance?

Explanation

K = 1 / (1 + 3) = 0.25; x = 5.0 + 0.25 × 1.0 = 5.25; P = (1 − 0.25) × 1 = 0.75. The result lies near the more certain prediction.

Question 2

After a long run the mean NIS of a tracker is 3.2. What is wrong with the settings and what would you change?

Explanation

A mean above 1 means the innovations are bigger than the filter expects: it is over-confident. Raise the process noise Q (the target manoeuvres more than assumed) or the measurement noise R (the detector is noisier than assumed), then re-check that the mean falls toward 1.

Question 3

Why is the covariance update skipped on a frame with no detection, and what happens to the gate?

Explanation

With no measurement there is nothing to correct the prediction, so only the predict step runs. P grows by Q each step, S grows with it, and the statistical gate widens, which lets the track find the object again when it reappears.

What’s next

Next: the life of a track: how it starts, how it persists through gaps, and when it ends.

Practice this lesson

Investigate How Noisy Detections Affect Track Quality

Add a motion model and a filter and measure track error against noise.

Next lesson

P7-T5 — Track Initiation, Persistence and Deletion

Design the track lifecycle with confirmation and deletion rules.

Nothing below is required to finish this lesson.