DSPRelated.com

Kalman Filter

Category: Adaptive

A Kalman filter is a recursive Bayesian estimator that fuses a dynamic model of a system with noisy sensor measurements to produce an optimal (minimum mean-square-error) estimate of the system's state, provided the process and measurement models are correct and the noise is Gaussian and white. It alternates between a prediction step, which propagates the state estimate forward using a process model, and an update step, which corrects that estimate using a new measurement weighted by the relative confidence in the model versus the sensor.

In practice

In embedded work, Kalman filters appear most often in sensor fusion tasks: combining accelerometer and gyroscope data for attitude estimation (IMU-based AHRS), fusing GPS position with inertial dead-reckoning, or estimating battery state-of-charge from voltage and current readings. The classic linear Kalman filter assumes Gaussian noise and a linear process model, which is a reasonable approximation for many physical systems operating near an equilibrium point.

The core computational cost is dominated by matrix multiplications and a matrix inversion whose size scales with the number of state variables. On a Cortex-M4F or Cortex-M7 with a hardware FPU, a small filter (two to six states) can typically execute in well under a millisecond at common clock speeds, though actual execution time depends heavily on clock frequency, compiler optimization, math library, and matrix implementation. On 8-bit or 16-bit MCUs without FPUs, floating-point overhead is significant and fixed-point implementations or simplified scalar filters are often used instead.

The filter requires tuning two noise covariance matrices: Q (process noise, representing model uncertainty) and R (measurement noise, representing sensor uncertainty), as well as the initial state covariance and any other model-specific parameters. Choosing Q and R is the main practical challenge. If Q is set too small relative to R, the filter trusts the model too much and responds slowly to real state changes. If Q is too large, it over-weights noisy measurements. Initial values are often estimated from sensor datasheets and system characterization, then refined empirically. The EmbeddedRelated post "Filtering Noise: The Basics (Part 1)" provides useful grounding in the noise and signal concepts that underpin this tuning process.

For nonlinear systems, the Extended Kalman Filter (EKF) linearizes the process and measurement functions at each time step using Jacobians, while the Unscented Kalman Filter (UKF) propagates a set of deterministically chosen sigma points through the nonlinear functions without computing Jacobians. Both add significant implementation complexity and computational cost compared to the linear form, and the EKF can diverge if the linearization error is large.

Frequently asked

Do I need a Kalman filter, or will a simpler low-pass filter work?
A fixed low-pass filter is often sufficient when you have a single sensor, the signal bandwidth is well-separated from noise, and you do not need to track a dynamic state variable (like velocity or acceleration) explicitly. A Kalman filter adds value when you are fusing multiple sensors with different noise characteristics, when the optimal filter bandwidth changes with operating conditions, or when you need state estimates that are not directly measured (e.g., estimating velocity from position measurements).
What is the difference between a Kalman filter and a complementary filter?
A complementary filter combines high-frequency data from one sensor with low-frequency data from another using manually chosen weights, typically implemented as a pair of high-pass and low-pass filters that sum to unity. In its basic form the weights are fixed, though some variants are adaptive or use more general blending structures. A Kalman filter computes the blending weights (the Kalman gain) automatically at each step based on the estimated error covariances, adapting to changing noise conditions. Complementary filters are simpler to implement and tune, and are a common choice for attitude estimation on resource-constrained MCUs when the Kalman filter's added accuracy is not required.
How do I represent the Kalman filter in code on a microcontroller?
For small state vectors (two to four states), many implementations unroll the matrix operations into scalar arithmetic to avoid the overhead of a general matrix library. For larger state vectors, a small linear algebra library such as CMSIS-DSP's matrix functions (on Cortex-M targets) or a custom fixed-point matrix library can be used. The prediction and update equations map directly to a handful of matrix multiply, add, transpose, and invert operations, which can be derived from any standard Kalman filter reference and written as a straightforward C function.
What is the Extended Kalman Filter (EKF) and when do I need it?
The EKF handles nonlinear process or measurement models by linearizing them at each time step, typically using first-order Taylor expansion (Jacobian matrices), though some implementations use numerical approximations rather than explicit Jacobians. It is commonly used in applications such as robot localization, attitude estimation with quaternions, and GPS/INS integration where the state dynamics or sensor models are nonlinear. The EKF is more computationally expensive than the linear Kalman filter and can diverge if the nonlinearity is severe or the linearization is inaccurate. For mildly nonlinear systems it is often adequate; for strongly nonlinear systems, the Unscented Kalman Filter or particle filters may be more reliable.
How is a Kalman filter related to Bayesian estimation?
A Kalman filter is a special case of a Bayesian recursive estimator. At each step it maintains a Gaussian belief over the system state (represented as a mean vector and covariance matrix), predicts how that belief evolves under the process model, and updates it using Bayes' rule when a new measurement arrives. Because both the process model and measurement model are linear and the noise is Gaussian, the posterior remains Gaussian and the update has an exact closed-form solution, which is what makes the Kalman filter computationally tractable. The EmbeddedRelated post 'Bayes meets Fourier' explores related connections between Bayesian reasoning and signal processing.

Differentiators vs similar concepts

The Kalman filter is most commonly confused with the complementary filter and with general low-pass filters. A complementary filter fuses sensors using fixed, manually tuned frequency-domain weights and is simpler to implement but does not adapt to changing noise statistics. A plain low-pass filter operates on a single signal and has no model of system dynamics. The Kalman filter distinguishes itself by propagating a full state estimate with an associated uncertainty (covariance), computing optimal blending gains from that uncertainty at runtime, and being able to estimate states that are not directly observed. Among Kalman variants, the EKF handles nonlinear models via Jacobian linearization, the UKF uses sigma-point propagation to avoid Jacobians, and the particle filter handles arbitrary (non-Gaussian) noise at the cost of much higher computational load -- the linear Kalman filter remains the most practical starting point for embedded targets.