Computing Library › Worked Examples
Worked Examples

Kalman-Filtering a Noisy Signal

Fuse a noisy model prediction with a noisy measurement to track a hidden state optimally, step by step.

The problem

You want the true value of a quantity you can only measure noisily, and you have a model of how it evolves. The Kalman filter combines the model prediction and the measurement, weighting each by its uncertainty, to produce the minimum-variance estimate at every step.

Predict and update

Kronos motion — pid vs model
python
import numpy as np
rng=np.random.default_rng(4)
true=5.0; Q=0.01; R=0.5          # process, measurement noise
x=0.0; P=1.0; est=[]
for t in range(60):
    z=true+rng.normal(0,np.sqrt(R))   # noisy measurement
    # predict (static model: x unchanged)
    P=P+Q
    # update
    K=P/(P+R)
    x=x+K*(z-x); P=(1-K)*P
    est.append(x)
print('final estimate:',round(x,3),'(true 5.0)')

The gain intuition

The Kalman gain K decides how much to trust the new measurement. When the measurement is noisy (large R) K is small and the filter leans on its model; when the model is uncertain (large P) K is near 1 and it follows the data. Over time the filter settles into a steady-state gain that balances the two.

Where it is used

The Kalman filter is optimal for linear systems with Gaussian noise and is the backbone of navigation, tracking, and real-time control - including estimating plasma state from noisy diagnostics for feedback control. Nonlinear systems use the extended or unscented variants, which linearize or sample around the current estimate.