As a beginner, you will spend 80% of your time tuning Q and R. Here is a simple guide:
Q (Process Noise): How much do you distrust your physics model?
filtered_positions = zeros(size(t));for k = 1:length(t) % --- Predict --- x_pred = A * x_est; P_pred = A * P_est * A' + Q;
% --- Update using measurement --- z = measurements(k); K = P_pred * H' / (H * P_pred * H' + R); x_est = x_pred + K * (z - H * x_pred); P_est = (eye(2) - K * H) * P_pred; % Store filtered position filtered_positions(k) = x_est(1);
end
A Kalman filter runs in a loop:
Think of it as:
Prediction + Measurement × Weight = New Estimate
The "weight" is called the Kalman Gain. If the measurement is very noisy, the gain is small (trust prediction more). If the prediction is uncertain, the gain is large (trust measurement more).
Let’s build a 1D Kalman Filter in MATLAB to track a car moving at constant velocity. We will generate noisy measurements, run the filter, and plot the beautiful result. As a beginner, you will spend 80% of
We defined F = [1 dt; 0 1]. This matrix tells the filter how the object moves based on high-school physics:
We defined H = [1 0]. This tells the filter that our sensor can see Position, but it cannot see Velocity. The filter must mathematically calculate the velocity based on how the position changes over time.
In Example 1, change R from 25 to 250 and re-run. Notice how the blue line becomes extremely smooth but lags behind the true position. Change R to 1, and the blue line becomes almost as noisy as the red dots. This is the trade-off. Q (Process Noise): How much do you distrust