← Back to the chapter · Previous detail · Next detail →
Technical detail · Imputation and Noise Filtering
Choose the cold-room median window
A three-sample window [−18, −28, −18] has mean −21.3°C but median −18°C. A five-sample window [−18, −18, −28, −18, −18] also returns −18°C and can reject two spikes when they remain a minority of the window. Choose an odd window and keep spike contamination below 50%.
At 0.5°C/minute, the maximum change per 10-second sample is about 0.083°C. A rounded declining sequence [−18.00, −18.08, −18.17, −18.25, −18.33] has median −18.17°C: the trend remains, with a delayed response. The original tenth-degree example [−18.0, −18.1, −18.2, −18.3, −18.4] has median −18.2°C, but its increments exceed that stated rate.
For a monotonic ramp, a trailing five-sample median returns the value two samples earlier: 20 seconds at this sampling interval. This delay is acceptable for the example’s non-time-critical review; verify it separately for alarms. The five-sample median needs no weighting coefficients, though its window remains a design choice. A temperature above a high threshold indicates warming; a value below −20°C would require a low-temperature alarm, not a high-temperature alarm.
median_filter = MedianFilter(window_size=5)
for reading in sensor_stream:
clean_temp = median_filter.filter(reading)
if clean_temp < -20: # Example low-temperature threshold; validate alarm delay separately
trigger_low_temp_alarm()