Noisy Sensors & Kalman Filters, bonus step
Bonus: Don't trust every reading
Now and then the sensor reports something wild. Spot readings that can't be right and skip them.
Builds on Noise, mean, variance & bias, from the free Foundations.
Write and run this step in the simulator with ProWild readings
Real sensors glitch: a reflection, a detection of the wrong object, a dropped packet. In this step about 1 reading in 20 is off by 20 cm. A Kalman filter believes every reading in proportion to , so one wild reading yanks the position, and worse, the velocity. That's enough to ruin an intercept.
Surprise, measured properly
Before updating, ask how surprising the reading is. The innovation is the gap between the reading and what you expected, and is how big that gap normally is:
The squared Mahalanobis distance measures the gap in units of its own spread:
For a good reading, follows a distribution with 2 degrees of freedom, and 99 % of good readings have . A 20 cm glitch with 1 cm noise scores in the hundreds. So gate the update: if , skip it and keep the prediction.
Don't lock yourself out
A gate trusts your belief. If the first reading is a wild one, the filter starts in the wrong place, and the good readings that follow soon look like outliers and get skipped forever. A simple fix: after 5 rejections in a row, restart the filter from the latest reading. Not after 1 or 2: wild readings sometimes come in pairs, and you must not restart from one.
Your task
- Implement
gated_update(x, P, z, R, H): return(x, P, True)after a normal update, or the unchanged(x, P, False)when . - Implement
track_robust(zs, dt)( states, liketrack). The grader runs it on two fixed tracks, one with 4 wild readings mixed in and one whose very first reading is wild, and the position error must stay under 1 cm on both. A plain filter gets about 2 cm on the first; a gate that never restarts loses the second for good. - Bonus: the intercept below already uses your gate. Catch the ball with outliers on.