5.11. Processing Sensor Data#
Raw sensor readings are rarely perfect. Noise, quantisation, vibrations, and environmental effects can make values jump around. Two common tools to make data more useful are smoothing and filtering (to reduce noise) and dead zones (to ignore tiny, meaningless changes).
5.11.1. Smoothing and Filtering#
Moving Average#
One of the simplest methods is a moving average, where the program remembers the last few samples and calculates their mean. Random spikes disappear, and the signal becomes easier to use. The trade-off is that the output reacts more slowly to sudden changes.
where $y[n]$ is the filtered signal at time $n$, $x[n]$ is the original signal at time $n$, $k$ and $N$ is the window length i.e. the number of past data points included.
Exponential Moving Average#
Another common method is the exponential moving average (EMA). Instead of averaging a whole window, each new reading is blended with the previous smoothed value. This is memory-efficient and fast, making it a favourite for embedded systems. A higher $alpha$ makes the result follow the input more closely, while a lower $alpha$ makes it smoother but slower to react.
Median Filter#
A different approach is the median filter, which takes the middle value of the last few samples instead of the mean. This is very effective for rejecting outliers, for example if one reading in ten is wildly wrong.
5.11.2. Dead Zones and Hysteresis#
Even with smoothing, a signal may still flicker slightly when nothing important is happening. A dead zone fixes this by treating small changes as zero. For example, if a joystick is near its centre position, a dead zone stops a robot creeping forward just because the stick is wobbling a little.
We can implement this in Python like so:
def deadband(x, center=0.0, threshold=5.0):
d = x - center
return 0.0 if -threshold <= d <= threshold else d
Another useful technique is hysteresis, which sets different thresholds for switching on and switching off. For instance, a fan controller might turn the fan on when the temperature rises above 30 °C, but not turn it off until it falls below 28 °C. Without this gap, the fan might chatter on and off every time the temperature hovers around 29.