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.
The idea
- 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).