Ground Embedded KO Subscribe

Notes / Sensors & Signal Processing

Kalman filter basics: blending prediction and measurement

Blend the value predicted from a model with the value measured by a sensor, each according to its uncertainty, to get a more accurate estimate. The principle with a one-dimensional example.

GROUND EMBEDDED · NOTESensors & Signal Processing 04Kalman filter basicsMCUTempI2CIMUSPIHallGPIORangeUARTCurrentADC

The idea

Predictnext value from the modelMeasuresensor valueBlendby how much each is trustedNew estimate
One round of a Kalman filter. More weight goes to whichever of prediction and measurement is more trustworthy.
  • Predict: estimate the current value from the previous state and the model. Uncertainty grows as time passes.
  • Measure: take the sensor value. It is uncertain by the amount of sensor noise.
  • Update: take a weighted average that trusts the less uncertain side more.

One-dimensional Kalman filter

// predict
P = P + Q;                 // Q: model (process) noise
// update
K = P / (P + R);           // R: measurement noise
x = x + K * (z - x);       // z: measurement
P = (1 - K) * P;

With the Kalman gain K near 0 it trusts the prediction, and near 1 it trusts the measurement. The difference from a fixed-alpha IIR is that K is set automatically from the noise statistics (Q, R).

RoadmapSee where this note sits in Sensors & Signal Processing→

A feel for tuning

Increase Result
R (measurement noise) Trusts the measurement less → smooth but slow
Q (model noise) Trusts the model less → fast but noisy

Where it is really used

Tilt estimation on a balancing robot (predict with the gyro, correct with the accelerometer), GPS and IMU fusion, battery state-of-charge (SOC) estimation, speed estimation for sensorless motors. With several states the matrix version is used, and for non-linear systems the extended Kalman filter (EKF).

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →