Kalman Filter

March 24, 2026 · View on GitHub

Overview & Motivation

In any real system, sensors are noisy and models are imperfect. The Kalman filter answers a deceptively simple question: given a noisy measurement and an imperfect prediction, what is the best estimate of the true state?

The answer is an optimal, recursive, minimum-variance estimator for linear systems with Gaussian noise. At each time step it performs two operations:

  1. Predict — propagate the state and covariance forward using the system model.
  2. Update — correct the prediction using the new measurement, weighted by the Kalman gain, which automatically balances trust in the model vs. trust in the sensor.

The filter is computationally lightweight (matrix operations on small state vectors), requires no storage of past measurements, and adapts its confidence in real time via the covariance matrix.

Mathematical Theory

State-Space Model

Discrete-time linear system with process noise ww and measurement noise vv:

xk=Fk1xk1+Bk1uk1+wk1,wN(0,Q)x_k = F_{k-1} x_{k-1} + B_{k-1} u_{k-1} + w_{k-1}, \quad w \sim \mathcal{N}(0, Q) zk=Hkxk+vk,vN(0,R)z_k = H_k x_k + v_k, \quad v \sim \mathcal{N}(0, R)

where:

  • xkRnx_k \in \mathbb{R}^n — state vector
  • zkRmz_k \in \mathbb{R}^m — measurement vector
  • FF — state transition matrix (n×nn \times n)
  • BB — control input matrix (n×ln \times l)
  • HH — measurement matrix (m×nm \times n)
  • QQ — process noise covariance (n×nn \times n)
  • RR — measurement noise covariance (m×mm \times m)

Predict Step

x^k=Fk1x^k1\hat{x}_k^- = F_{k-1} \hat{x}_{k-1} Pk=Fk1Pk1Fk1T+Qk1P_k^- = F_{k-1} P_{k-1} F_{k-1}^T + Q_{k-1}

Update Step

yk=zkHkx^k(innovation)y_k = z_k - H_k \hat{x}_k^- \qquad \text{(innovation)} Sk=HkPkHkT+Rk(innovation covariance)S_k = H_k P_k^- H_k^T + R_k \qquad \text{(innovation covariance)} Kk=PkHkTSk1(Kalman gain)K_k = P_k^- H_k^T S_k^{-1} \qquad \text{(Kalman gain)} x^k=x^k+Kkyk(state update)\hat{x}_k = \hat{x}_k^- + K_k y_k \qquad \text{(state update)} Pk=(IKkHk)Pk(IKkHk)T+KkRkKkT(Joseph form covariance update)P_k = (I - K_k H_k) P_k^- (I - K_k H_k)^T + K_k R_k K_k^T \qquad \text{(Joseph form covariance update)}

Optimality

The Kalman filter is the minimum mean-square error (MMSE) estimator among all linear estimators. Under the assumption that ww and vv are Gaussian, it is also the MMSE among all estimators (linear or not).

Complexity Analysis

OperationTimeSpaceNotes
PredictO(n2)O(n^2)O(n2)O(n^2)Matrix multiply FPFTF P F^T
UpdateO(n2m+m3)O(n^2 m + m^3)O(nm)O(nm)Dominated by S1S^{-1} (m×mm \times m inversion) and gain computation
Total per stepO(n2m+m3)O(n^2 m + m^3)O(n2+nm)O(n^2 + nm)For mnm \ll n, approximately O(n2)O(n^2)

For typical embedded use with n10n \leq 10 and m5m \leq 5, each step completes in microseconds.

Step-by-Step Walkthrough

System: Estimating position and velocity from noisy position measurements.

State: x=[positionvelocity]x = \begin{bmatrix} \text{position} \\ \text{velocity} \end{bmatrix}, Δt=1\Delta t = 1 s.

F=[1101],H=[10],Q=[0.1000.1],R=[1]F = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}, \quad H = \begin{bmatrix} 1 & 0 \end{bmatrix}, \quad Q = \begin{bmatrix} 0.1 & 0 \\ 0 & 0.1 \end{bmatrix}, \quad R = [1]

Initial: x^0=[0,0]T\hat{x}_0 = [0, 0]^T, P0=[100010]P_0 = \begin{bmatrix} 10 & 0 \\ 0 & 10 \end{bmatrix}

Measurement: z1=1.2z_1 = 1.2 (true position = 1.0)

Predict:

x^1=[1101][00]=[00]\hat{x}_1^- = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 0 \\ 0 \end{bmatrix} = \begin{bmatrix} 0 \\ 0 \end{bmatrix}

P1=FP0FT+Q=[20.1101010.1]P_1^- = F P_0 F^T + Q = \begin{bmatrix} 20.1 & 10 \\ 10 & 10.1 \end{bmatrix}

Update:

y1=1.2[1  0][00]=1.2y_1 = 1.2 - [1\; 0] \begin{bmatrix} 0 \\ 0 \end{bmatrix} = 1.2

S1=HP1HT+R=20.1+1=21.1S_1 = H P_1^- H^T + R = 20.1 + 1 = 21.1

K1=P1HT/S1=[20.110]/21.1=[0.9530.474]K_1 = P_1^- H^T / S_1 = \begin{bmatrix} 20.1 \\ 10 \end{bmatrix} / 21.1 = \begin{bmatrix} 0.953 \\ 0.474 \end{bmatrix}

x^1=[00]+[0.9530.474]1.2=[1.1430.569]\hat{x}_1 = \begin{bmatrix} 0 \\ 0 \end{bmatrix} + \begin{bmatrix} 0.953 \\ 0.474 \end{bmatrix} \cdot 1.2 = \begin{bmatrix} 1.143 \\ 0.569 \end{bmatrix}

The filter quickly moves toward the measurement because the initial covariance P0P_0 is large (low confidence). Over subsequent steps, PP shrinks and the gain decreases — the filter becomes more reliant on the model.

Pitfalls & Edge Cases

  • Model mismatch. If the true system is nonlinear or the noise is non-Gaussian, the Kalman filter is suboptimal. Consider the Extended Kalman Filter (EKF) or Unscented Kalman Filter (UKF) for moderate nonlinearities.
  • Covariance divergence. Numerical errors can cause PP to lose positive-definiteness over time. Symmetrize PP after each update; consider a square-root or UD factorization form for long-running filters.
  • Tuning QQ and RR. These are rarely known exactly. Increasing QQ makes the filter more responsive (trusts model less); increasing RR makes it smoother (trusts measurements less). Tune empirically or use adaptive methods.
  • Innovation covariance singularity. If SkS_k is singular, the measurement does not provide new information in some direction. Regularize RR or reduce the measurement model.
  • Missing measurements. If no measurement is available, skip the update step entirely and only predict. The covariance will grow, correctly reflecting increasing uncertainty.
  • Fixed-point caution. Matrix inversions and multiplications in the update step can overflow Q15/Q31 ranges. Use floating-point or carefully scale all matrices.

Variants & Generalizations

VariantKey Difference
Extended Kalman Filter (EKF)Linearizes nonlinear f(x)f(x) and h(x)h(x) around the current estimate
Unscented Kalman Filter (UKF)Propagates sigma points through nonlinearities; avoids explicit Jacobians
Square-root Kalman FilterMaintains P\sqrt{P} instead of PP for improved numerical stability
Information FilterWorks with the inverse covariance (information matrix); better for multi-sensor fusion
Kalman SmootherUses future measurements to refine past estimates (offline, non-causal)
Adaptive Kalman FilterEstimates QQ and RR online from innovation statistics

Applications

  • GPS/INS navigation — Fusing inertial measurements (high rate, drifty) with GPS (low rate, noisy).
  • Object tracking — Radar, lidar, or camera-based tracking of moving targets.
  • Sensor fusion — Combining accelerometer, gyroscope, and magnetometer for attitude estimation.
  • Process control — State estimation for model-based controllers (LQG = LQR + Kalman).
  • Battery state-of-charge estimation — Estimating internal states from voltage and current measurements.
  • Econometrics — Estimating latent economic variables from noisy indicators.

Connections to Other Algorithms

graph LR
    KF["Kalman Filter"]
    LQR["LQR Controller"]
    DARE["DARE Solver"]
    PID["PID Controller"]
    KF -->|"state estimate"| LQR
    DARE -->|"steady-state gain"| KF
    KF -->|"filtered measurement"| PID
AlgorithmRelationship
LQR ControllerThe Kalman filter + LQR = LQG (Linear Quadratic Gaussian), the separation principle
DARE SolverThe steady-state Kalman gain can be found by solving a DARE (dual of the LQR DARE)
PID ControllerKalman-filtered measurements can serve as the PID's process variable input

References & Further Reading

  • Kalman, R.E., "A New Approach to Linear Filtering and Prediction Problems", Journal of Basic Engineering, 82(1), 1960.
  • Simon, D., Optimal State Estimation: Kalman, H∞, and Nonlinear Approaches, Wiley, 2006.
  • Bar-Shalom, Y., Li, X.R. and Kirubarajan, T., Estimation with Applications to Tracking and Navigation, Wiley, 2001.