Position and velocity are not always directly observable, and sensor readings contain noise. A Kalman filter combines a model with measurements to estimate the state as new data arrives, while tracking the uncertainty of the estimate.

Where to start

  • Learn the theory:Tutorials cover least squares, covariance, and the standard, extended, and unscented Kalman filters.
  • Try it:Toolbox estimates measurement noise R, generates process noise Q for two models, and includes animated demos.
  • See applications:Applications features recent research and engineering cases.Resources includes original papers and reference material.

How the Kalman filter works

Every step has two parts:prediction uses the previous estimate and the model to form a prior;update uses the new measurement to form a posterior. The Kalman gain determines how strongly to correct the prediction, based on uncertainty in the model and measurements.

Rudolf Emil Kalman

Rudolf Emil Kalman

In 1960, Kalman published a recursive approach to discrete-time linear filtering. The standard filter represents uncertainty with a mean and covariance. It gives the exact conditional mean for a linear Gaussian model. Without Gaussian assumptions, it can still give an optimal linear estimate under appropriate second-moment assumptions, but it does not track an arbitrary posterior distribution.

What if the model is nonlinear?

  • Extended Kalman filter (EKF): Linearize the model at the current estimate before predicting and updating.
  • Unscented Kalman filter (UKF): Pass representative sigma points through the nonlinear model and approximate the resulting mean and covariance.

A scalar filter in C

In this scalar example, the prediction carries over the previous state estimate and the sensor measures the state directly. q and r are the process and measurement variances; p represents the uncertainty of the estimate. This is not a general multivariate filter.

typedef struct
{
  float x;
  float p;
} typeKalman;

void kalmanfilter(typeKalman *in_out, float q, float r, float z)
{
  float x, p, k;
  x = in_out->x;                 // State prediction
  p = in_out->p + q;             // Covariance prediction
  k = p / (p + r);          // Calculate the Kalman gain
  in_out->x = x + k * (z - x); // State update
  in_out->p = (1 - k) * p * (1 - k) + k * r * k;     // Covariance update
}