HomeArticlesState Estimation

Kalman Filter

GPS drifts, IMUs accumulate error — the Kalman filter is the math that quietly fuses both into a single trustworthy estimate.

mysimulator teamUpdated July 2026≈ 10 min read▶ Open the simulation

Two imperfect sources, one better answer

Suppose you want to know a drone's exact position and velocity. You have two ways to guess, and neither is trustworthy alone. Your model can tell you, from the last known state and the control inputs you just applied, where the drone should be now — accurate over short horizons, but it slowly drifts as unmodeled wind gusts and motor imperfections accumulate. Your sensor — GPS, say — measures position directly, but it is noisy (typically ±3 m) and updates slowly (1-10 Hz). Neither source alone is good enough. The Kalman filter, derived by Rudolf Kálmán in 1960 and famously used to navigate Apollo to the Moon, is the mathematically optimal way to combine the two: it weights each source inversely by how uncertain it currently is, trusting the sensor more when the model has drifted and trusting the model more when the sensor is being noisy.

live demo · noisy measurement fused with a model prediction● LIVE

The state-space model underneath

A linear dynamic system is described by two equations — one for how the state evolves, one for how it is observed:

x_k = A · x_(k-1) + B · u_k + w_k    // process model
y_k = C · x_k + v_k                   // measurement model

x  state vector (position, velocity, ...)     A  state-transition matrix
B  control-input matrix; u  control input     C  observation matrix
w ~ N(0, Q)  process noise                    v ~ N(0, R)  measurement noise

1D constant-velocity example (state = [position, velocity]):
  A = [[1, dt], [0, 1]]      C = [1, 0]   // we only measure position

The predict-update cycle

Every timestep runs two phases in sequence. Predict projects the state forward with the model alone, and lets its uncertainty grow: x̂⁻ₖ = A·x̂ₖ₋₁ + B·uₖ, and the error covariance P⁻ₖ = A·Pₖ₋₁·Aᵀ + Q grows by the process noise Q. Update then folds in the new measurement: it computes the Kalman gain K = P⁻·Cᵀ·(C·P⁻·Cᵀ + R)⁻¹, corrects the state estimate using the residual between measurement and prediction, x̂ₖ = x̂⁻ₖ + K·(yₖ − C·x̂⁻ₖ), and shrinks the covariance accordingly, Pₖ = (I − K·C)·P⁻ₖ. The term (yₖ − C·x̂⁻ₖ) is called the innovation — how surprised the filter is by the new reading — and K decides how much of that surprise to actually believe.

Reading the gain

In the scalar case the gain collapses to a single, very readable ratio:

K = P⁻ / (P⁻ + R)

K → 1   as P⁻/R → ∞   (model very uncertain, sensor accurate)  → trust sensor
K → 0   as P⁻/R → 0   (model accurate, sensor noisy)           → trust model

Example: model uncertainty P⁻ = 9 m², GPS variance R = 4 m²
K = 9 / (9 + 4) ≈ 0.69  →  lean mostly toward the GPS reading

A useful practical shortcut: when A, C, Q and R stay constant over time, the covariance P converges to a fixed steady-state value after only a handful of iterations, and you can precompute that steady-state gain offline — a significant saving for embedded systems that cannot afford to run matrix inversions every cycle.

When the world isn't linear: EKF and UKF

The standard filter assumes both A and C are fixed matrices, which breaks the moment the dynamics turn genuinely nonlinear — a robot whose heading changes its velocity direction sinusoidally, or a bearing-only sensor. The Extended Kalman Filter (EKF) keeps the same predict-update structure but re-linearises the nonlinear functions f and h at every step using their Jacobian matrices, F = ∂f/∂x and H = ∂h/∂x, evaluated at the current estimate — it is the default choice behind most robotics stacks, including ROS's robot_localization package and typical SLAM pipelines. When the nonlinearity is severe enough that a first-order Jacobian approximation starts to diverge, the Unscented Kalman Filter (UKF) instead propagates a small deterministic set of sample points — sigma points — directly through the true nonlinear function, capturing the distribution to third-order accuracy without ever computing a Jacobian.

Where it actually runs

Beyond spacecraft guidance, the same predict-update loop shows up almost anywhere noisy sensors need combining with a model: GPS and IMU fusion in phones and drones, radar target tracking, multi-object tracking in computer-vision pipelines like SORT and DeepSORT (where the Kalman filter predicts each bounding box's motion between frames), estimating hidden volatility from noisy financial time series, and even denoising speech from a noisy microphone signal. Its enduring appeal is that it needs no training data, runs in closed form with a handful of matrix operations per step, and is provably the optimal linear estimator under Gaussian noise — properties that make it hard to beat for embedded, real-time applications even in an era of learned alternatives.

Frequently asked questions

What does the Kalman gain actually control?

The Kalman gain K sets how much of the residual between the prediction and the new measurement gets folded into the updated estimate. In the scalar case K = P/(P+R): when the model's uncertainty P is large relative to the measurement noise R, K approaches 1 and the filter trusts the sensor; when the sensor is noisier than the model is uncertain, K approaches 0 and the filter trusts the prediction.

Why can't a plain Kalman filter handle a robot turning or a nonlinear sensor model?

The standard Kalman filter assumes both the state-transition and measurement functions are linear matrices. A turning robot, a bearing-only sensor or any trigonometric relationship breaks that assumption. The Extended Kalman Filter linearises the nonlinear functions with a Jacobian at each step; the Unscented Kalman Filter instead propagates a small set of deterministic sample points through the true nonlinear function, avoiding Jacobians entirely and staying accurate to a higher order.

Is the Kalman filter still relevant given modern machine learning methods?

Yes — it remains the standard tool wherever you need a fast, provably optimal (for linear-Gaussian systems), interpretable estimator with a tiny computational footprint, from embedded GPS/IMU fusion to multi-object tracking in computer vision pipelines like SORT. Learned models can outperform it when the dynamics are highly nonlinear and training data is abundant, but the Kalman filter needs no training data at all and runs comfortably on microcontrollers.

Try it live

Everything above runs in your browser — open Kalman Filter and watch the estimate track a noisy signal as the gain adapts in real time.

▶ Open Kalman Filter simulation

What did you find?

Add reproduction steps (optional)