Scalar Kalman Filter: 1D Train Tracking#
This demo runs the canonical scalar Kalman filter on a 1D scenario: a train moves at approximately constant velocity along a track, the only sensor is a noisy position measurement that arrives every few seconds, and the filter has to estimate the true position continuously. It is the simplest setting in which every characteristic Kalman behavior is visible: the gain converging to steady state, the bounds growing between updates and snapping tighter at each update, the estimate jumping at measurements and sliding along the model in between.
The filter#
The five recurring equations from the Block 4 reading, specialized to a scalar state with constant-velocity propagation:
Process noise is parameterized as a rate (\(\sigma_\text{process}\) in m/√s, \(Q = \sigma_\text{process}^2\) in m²/s); measurement noise variance is \(R = \sigma_\text{meas}^2\). The Kalman gain \(K\) is the inverse-variance weight from Block 3: the prior \(P^-\) acts as “sensor 1” and the measurement \(R\) acts as “sensor 2”.
Interactive demo#
The demo simulates the full scenario deterministically (given the current parameters and seed), and animates a moving “now” cursor through the run. The live track view at the top is a top-down spatial view of the train: the cyan triangle is truth, the blue diamond is the filter estimate, the dark dot is the most recent measurement, and the red bar with dashed caps is the current ±95% covariance interval. The track auto-scrolls to keep the estimate at the center. Below it, the latest-update panel freezes the most recent measurement update as three Gaussians: the prior \(\mathcal{N}(\hat{x}^-, P^-)\) (green, dashed), the measurement \(\mathcal{N}(z, R)\) (black) and the posterior \(\mathcal{N}(\hat{x}^+, P^+)\) (blue, filled). It also draws the innovation \(\nu = z - \hat{x}^-\) against its 95% gate \(\pm 1.96\sqrt{S}\), \(S = P^- + R\); its readout works \(K = P^-/(P^- + R)\) through with the actual numbers. The three time-series panels below carry a synchronized vertical “now” cursor; the gain panel also draws the steady-state gain \(K_\infty\) as a dashed line. A cue line under the playback bar states what to notice at the current time.
Walkthrough#
The demo opens paused at \(t = 0\) in the canonical Block 4 scenario: \(v_\text{true} = 20\) m/s, \(\sigma_\text{meas} = 30\) m, measurement interval = 10 s, \(\sigma_\text{process} = 10\) m/√s, initial estimate \(-100\) m with \(\sigma_0 = 200\) m. Press Play; at the default 4× speed the 120 s run takes 30 s. Try the following:
Watch the first measurement at \(t = 10\) s. Pause right after it (or drag the time slider to 10). At \(t = 0\) the estimate (blue diamond) is 100 m behind truth with \(\sigma_0 = 200\) m. Just before the update the prior \(\hat{x}^- = 100\) m is still about 100 m behind truth (\(-103\) m) and slightly more uncertain: \(\sigma^- = 202.5\) m, because \(P^-\) has grown by \(Q \cdot t = 1000\) m². The gain is \(K = 41000/(41000 + 900) = 0.979\) (\(P^-\) dwarfs \(R\)), so the estimate lands almost on the measurement and \(\sigma\) drops to 29.7 m. In the latest-update panel the green prior is nearly flat and the blue posterior sits on top of the black measurement curve.
Let it play through. The gain runs 0.979, 0.676, 0.641, 0.637 and then sits on the dashed \(K_\infty = 0.636\) line. That is the steady-state Kalman gain: the fixed point at which each update removes exactly the 1000 m² that prediction adds over one 10 s interval (\(P^- = 1572 \to P^+ = 572\) m²). From here on, the bounds widen and snap tighter periodically, but their post-update value stops shrinking. Tick zoom to steady state on the error panel to see the converged sawtooth: ±46.9 m just after each update, ±77.7 m just before.
Slide \(\sigma_\text{meas}\) up to 80 m. The measurement is now much noisier than the prediction, so the gain converges to a smaller value, \(K_\infty = 0.325\) (the filter trusts measurements less). The bounds stay wider and the post-update jumps in the position panel are smaller. Slide it down to 5 m and \(K_\infty = 0.976\): the filter all but ignores its prior and snaps onto every measurement.
Slide \(\sigma_\text{process}\) up to 25 m/√s. Bounds grow much faster between updates, and the gain converges higher, \(K_\infty = 0.887\): when the prediction is uncertain, the filter trusts measurements more. This is the \(Q\) vs \(R\) tug-of-war the reading describes.
Slide the measurement interval up to 30 s. Bounds grow visibly between the now-rare updates, and the steady-state gain rises to \(K_\infty = 0.805\) because \(P^-\) has 30 s to grow before each update. At very long intervals the filter is essentially open-loop between fixes.
Set \(v_\text{model} \neq v_\text{true}\) (e.g. 15 vs 20). The estimate slides off truth between updates and gets yanked back at each measurement; the error panel shows a one-sided sawtooth. The gain does not change: \(K\) is 0.979, 0.676, … , 0.636 exactly as with the right model, because \(K\) depends only on \(P\), \(Q\) and \(R\), never on the data or on \(v_\text{model}\). So the filter never “notices” the mismatch. Just before each update the error rides at or outside the bound: from \(t = 40\) s on it is outside ±77.7 m at 8 of 9 updates, worst \(-113\) m at \(t = 60\) s. The steady pre-update bias is \((v_\text{model} - v_\text{true})\,\Delta t_\text{meas}/K_\infty = (15 - 20)(10)/0.636 = -78.6\) m, right at the bound. The innovation shows it too: \(|\nu|\) exceeds its \(\pm 1.96\sqrt{S}\) gate at 3 of the 12 updates (none with the right model). This gate check is the residual test you will build in Block 8. Model mismatch shows up as a structurally biased error, not as wider bounds; a tight CI does not protect you from a wrong model.
Pause and scrub. Drag the time slider; the live track, the latest-update panel, all three time cursors and the stats cards update in lockstep. Use this to inspect the filter state at any time of interest. Pressing Play resumes from wherever you scrubbed to.
Key observations#
The gain converges. The Kalman gain is not a fixed value: it depends on the current \(P^-\), which depends on the run’s history of \(Q\) growth and \(R\) updates. With time-invariant \(Q\) and \(R\), the gain settles into a steady state, and \(P^+\) approaches a fixed point. This is the discrete-time Riccati equation reaching equilibrium; for this filter the fixed point has a closed form, \(P^-_\infty = \tfrac{1}{2}\big(q + \sqrt{q^2 + 4qR}\big)\) with \(q = Q\,\Delta t_\text{meas}\), and \(K_\infty = P^-_\infty/(P^-_\infty + R)\), the dashed line in the gain panel.
Bounds shrink at updates, grow between. This is the visible signature of \(P^+ = (1-K) P^-\) followed by \(P^- = P^+ + Q\,\Delta t\). Watch the position panel: the dashed red bounds make a sawtooth pattern.
Confidence interval \(\neq\) accuracy. When \(v_\text{model} \neq v_\text{true}\), the filter still produces tight bounds but the actual error rides outside them. The CI captures noise variability around the model; if the model is wrong, the CI is wrong about something true. The innovations are where it shows (step 6), and they are what Block 8’s residual tests watch.
The first few updates do most of the work. The initial covariance shrinks dramatically after the first one or two measurements: \(\sigma\) falls from 202.5 m to 29.7 m at the first update and is within 0.1 m of its steady-state value (23.9 m) after the third. This is why in practice the initial uncertainty \(\sigma_0\) has surprisingly little effect on long-run behavior: the filter “forgets” the prior quickly once measurements arrive.
Every behavior generalizes. Replace the scalar with a vector, \(F\) with a matrix, \(H\) with a measurement matrix, and the same predict-update cycle is the multi-state Kalman filter from Block 5. Same recursion, same trade-off, same tug-of-war.
Source#
The MATLAB companion runs the same default scenario. It steps at \(\Delta t = 1\) s, so its per-step process variance \(\sigma_\text{process}^2\,\Delta t\) equals \(\sigma_\text{process}^2\) (the demo steps at 0.1 s; both add 1000 m² between 10 s updates), and it prints the same gains: 0.9785, 0.6763, 0.6412, 0.6367, … → \(K_\infty = 0.6360\).