Science

The Kalman Filter: Guessing Well From Noisy Sensors

· 4 min

Predict, measure, correct, repeat.

Every sensor lies a little. A GPS fix wanders by a few metres, an accelerometer drifts, a wheel encoder slips. Yet a drone holds its position, a phone's map dot glides smoothly down the road, and a robot knows roughly where it is. A large part of the reason is one idea from 1960: the Kalman filter.

Where it came from

In 1960 Rudolf E. Kálmán published "A New Approach to Linear Filtering and Prediction Problems" in the ASME Journal of Basic Engineering. A year later, with Richard Bucy, he extended the idea to continuous time.

The filter found its first big job almost immediately. Stanley F. Schmidt at NASA Ames saw that it could help solve spacecraft navigation for the Apollo programme, and his team adapted it for the nonlinear problem of flying to the Moon. That work is a big part of why the method spread through aerospace and, later, everywhere else. Kálmán received the US National Medal of Science in 2009.

The core idea: two imperfect sources of truth

Imagine tracking a robot moving down a corridor. You have two ways to know where it is:

  1. A model. You know its last position and its speed, so you can predict where it should be now. But the model is imperfect: wheels slip, the floor isn't flat.
  2. A measurement. A sensor tells you where it appears to be. But the sensor is noisy.

Neither is trustworthy alone. The Kalman filter combines them, weighting each by how uncertain it is. If the sensor is very noisy, lean on the prediction. If the model is shaky, lean on the sensor. The weight is called the Kalman gain, and the filter recomputes it at every step.

Predict, then update

The filter runs as a loop with two steps.

Predict. Push the current estimate forward through the model, and grow the uncertainty, because time passing without a measurement means you know less.

Update. When a measurement arrives, compare it with the prediction. The difference (the innovation) is scaled by the Kalman gain and added to the estimate, and the uncertainty shrinks.

For a single quantity, the whole thing fits in a few lines of Python:

def kalman_1d(measurements, q=0.01, r=1.0):
    x, p = measurements[0], 1.0      # estimate and its variance
    out = []
    for z in measurements:
        # predict: state stays put, uncertainty grows by process noise q
        p = p + q
        # update: blend in measurement z with noise variance r
        k = p / (p + r)              # Kalman gain, between 0 and 1
        x = x + k * (z - x)
        p = (1 - k) * p
        out.append(x)
    return out

Try it on a noisy reading of a constant value and the output settles smoothly near the truth. Make r small (trust the sensor) and it follows the measurements closely; make it large and it becomes calm and slow to react. That single trade-off is the heart of tuning a Kalman filter.

Why it shows up everywhere

For linear systems with Gaussian noise, the Kalman filter is the optimal estimator in the least-squares sense, and it is cheap: it only needs the current estimate and its uncertainty, not the whole history. That made it practical on 1960s flight computers, and it makes it practical on a microcontroller today.

Real systems are rarely linear, so engineers use variants: the extended Kalman filter linearises the model around the current estimate, and the unscented Kalman filter pushes a small set of sample points through the nonlinear model instead. The open-source PX4 autopilot, used on many drones, fuses its IMU, GPS, barometer and other sensors with an extended Kalman filter it calls EKF2.

The lesson for cyber-physical systems

In a cyber-physical system the estimate is only as good as the data reaching it, and data travels over networks. A measurement that arrives late or not at all changes the picture: the filter keeps predicting, its uncertainty keeps growing, and the controller acting on that estimate becomes less sure of itself. Studying how estimation behaves under delay and packet loss is exactly the kind of question that co-simulation tools for networks and robots are built to answer.

The deeper habit the Kalman filter teaches is worth keeping even outside engineering: never trust a single source completely, keep track of how uncertain you are, and update your beliefs in proportion to the quality of new evidence.