Filtering and state estimation
Reduce measurement noise without hiding motion, delay, or faults, and quantify what an estimate can support.
- Implement and analyze a moving-average or exponential low-pass filter.
- Separate measurement, estimate, residual, and true-but-unknown state in a diagnostic record.
Related study — optional context
Understand
The working idea
Sensors do not reveal truth directly. A camera-derived position jitters, an ADC includes quantization and electrical noise, and a distance sensor may occasionally return an outlier. Filtering combines measurements over time to make a more useful estimate. The cost is usually delay or reduced ability to follow rapid change.
An N-sample moving average is ȳ_k = (1/N)Σx_{k-i}. For independent zero-mean noise with standard deviation σ, its standard deviation falls ideally to σ/√N, but the window delays changing signals. An exponential filter uses ŷ_k = αx_k + (1-α)ŷ_{k-1}, with 0 < α ≤ 1. Smaller α smooths more and responds more slowly. A state estimator goes further by combining a process model and measurement uncertainty; even a simple “predict, compare residual, correct” loop makes its assumptions explicit.
The residual r_k = measurement - predicted measurement is diagnostically valuable. Random residuals near zero may support the model; persistent bias suggests calibration error or an unmodelled force; abrupt large residuals may signal dropout or impact. Do not silently discard inconvenient readings. Record raw data, estimate, filter parameters, timestamp, saturation, and rejection decisions. For a camera mount, the build decision could be a modest filter that reduces pixel jitter while preserving enough bandwidth to track intentional pan motion.
A common misconception is that a smoother plot is more accurate. Heavy filtering can produce a clean but late estimate and hide a loose connector. Another is treating a filter's output as a physical measurement; it is a computed estimate based on assumptions. Averaging correlated drift does not remove the drift as σ/√N predicts. Choose filter bandwidth relative to system dynamics, examine residuals, and test known changes. If control stability worsens after filtering, added delay may outweigh reduced noise.
Follow the reasoning
Worked example
A distance sensor reports 100, 104, 98, and 102 mm. Compute the four-sample mean and the ideal standard-deviation reduction factor for independent noise.
- 1
Sum readings: 100 + 104 + 98 + 102 = 404 mm.
- 2
Mean is 404 mm ÷ 4 = 101 mm.
- 3
For independent zero-mean noise, standard deviation of the mean is σ/√4 = σ/2.
The estimate is 101 mm, with an ideal random-noise standard deviation half that of one reading.
Sanity checkThe mean lies within the observed 98–104 mm range; the uncertainty claim does not cover bias or correlated drift.
Try it yourself
Independent check
Move to the bench
Compare raw, filtered, and residual signals during stationary and moving tests.
- Low-voltage distance or light sensor
- Microcontroller
- Serial logger
- Movable target
- Ruler
Experiment
- Log raw readings from a fixed target and predict how windows N = 1, 4, and 16 will affect scatter and response time.
- Move the target by a measured amount at a marked time; compute each moving average without deleting raw samples.
- Compare actual scatter, time to cross 90% of the change, and residuals for each window.
- Deliberate fault: briefly cover or disconnect the sensor within its safe interface; diagnose the outlier/dropout from raw data and residual rather than masking it with a long average.
Observe
- Expected: longer windows reduce stationary scatter but delay the measured step.
- Measure: actual raw range, filtered crossing time, residuals, and dropout code; identify assumptions behind any uncertainty estimate.
Choose and justify a filter for a desk robot that must ignore millimetre jitter yet detect a hand approaching within 0.3 s.
- Use only sensor-safe low-voltage wiring and disconnect power before changing connections.
Keep evidence
Bench note
Record what you expected, what you actually measured or observed, and one difference worth investigating. This is self-reported evidence, not a certification.