The Kalman Filter: State Estimation Under Noise
The Kalman filter estimates the true state of a system from noisy measurements. It alternates between prediction and correction, weighting each by how much it trusts the model vs the sensor.
The Problem
You are running a system and you want to know its true state . The problem: your sensors produce noisy measurements, and your model of how the state evolves also has uncertainty. The Kalman filter combines both sources of information (the physical model and the measurement) to produce the best possible estimate of the true state.
Variables
| Symbol | Meaning |
|---|---|
| , | True state / predicted state |
| , | Process covariance matrices |
| State transition matrix | |
| Control input matrix | |
| Measurement conversion matrix | |
| Noise input matrix | |
| Measurement matrix (shapes the calculation) | |
| Kalman gain | |
| Control input (external variable) | |
| Measured state | |
| Process noise | |
| Measurement noise | |
| Process noise covariance | |
| Measurement noise covariance |
The Two-Phase Loop
The Kalman filter alternates between predict and correct at every time step.
Phase 1: Predict
Use the physical model to project the state forward:
Project the process covariance forward:
This gives a predicted state and predicted uncertainty, both based on the model alone, before seeing the new measurement.
Phase 2: Correct
Take the new measurement:
Compute the Kalman gain, which determines how much to trust the measurement vs the prediction:
Update the state estimate by blending the prediction with the measurement residual :
Update the covariance:
Understanding the Kalman Gain
The term is the innovation, the difference between what the sensor measured and what the model predicted. The Kalman gain controls how much of this innovation to absorb:
- If is large (sensor is noisy), is small. Trust the model more, ignore most of the measurement
- If is small (sensor is accurate), is large. Trust the measurement more, correct the model aggressively
The covariance update shows the same logic: a larger (more trust in measurement) shrinks the uncertainty faster.
Why This Matters for Control
The Kalman filter is commonly embedded inside MPC and other control systems when the full state cannot be measured directly. It provides a clean, statistically optimal estimate of the state at each time step, which the controller then uses to compute the next action.
What You Can Do Now
The code below runs a 1D Kalman filter on a constant signal buried in sensor noise, showing the predict-correct loop and how the estimate converges to the true value.
import numpy as np
np.random.seed(42)
n_steps = 40
true_state = 5.0 # constant true value we are trying to estimate
# Noise parameters
Q = 0.01 # process noise variance (model uncertainty)
R = 2.0 # measurement noise variance (sensor uncertainty)
# Noisy measurements
measurements = true_state + np.random.normal(0, np.sqrt(R), n_steps)
# Initial estimates
x_est = 0.0 # prior state estimate
P = 1.0 # prior uncertainty
print(f"{'Step':>4} {'Measured':>10} {'Estimated':>10} {'Kalman Gain':>12}")
for k, z in enumerate(measurements):
# --- Predict ---
x_pred = x_est # state doesn't change (constant signal model)
P_pred = P + Q # uncertainty grows slightly each step
# --- Correct ---
K = P_pred / (P_pred + R) # Kalman gain
x_est = x_pred + K * (z - x_pred) # blend prediction with measurement
P = (1 - K) * P_pred # update uncertainty
if k < 5 or k == n_steps - 1:
print(f"{k+1:>4} {z:>10.3f} {x_est:>10.3f} {K:>12.4f}")
print(f"\nTrue value: {true_state:.3f}, Final estimate: {x_est:.3f}")
Change R to a larger value (noisy sensor) and watch the Kalman gain stay small. The filter leans on the model. Reduce R and the gain grows, trusting measurements more. Setting Q = 0 models a perfectly known system with no drift.