TactivergeLite

Lite / Intelligent Robotic Systems / Week 23

Spikes, median filters and filtering inside a controller

One glitch reading of 85 °C in a greenhouse at 18 °C. An average says 24 °C. What does the plant actually feel?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Real sensor logs have spikes: a dropped bit, a loose connector, a reflection. A controller that trusts a spike can switch a heater on for nothing; one that smooths too heavily reacts late to a real problem. This lesson puts filtering inside a working control loop.

Safety: Simulation only. Real greenhouse heaters are mains-powered and must be wired by a qualified person.

Predict

Readings 18.0, 18.1, 85.0, 18.2, 17.9. What does a 5-sample median give?

  1. 31.4
  2. 18.1
  3. 85.0
Guess first, then reveal

Sorted: 17.9, 18.0, 18.1, 18.2, 85.0. The middle one is 18.1. The spike sits at the end of the sorted list and has no effect. The mean would be 31.4 °C.

Discover

Open the smart greenhouse at Level 3. First run the night with the default thermostat and count the switches. Then try heavy smoothing with a narrow gap, and finally no smoothing with a 1.5–2 °C gap. Which actually solves the chattering, and what does heavy smoothing do during the open-vent dip?

What the interactive shows (text version)

In the greenhouse simulator, the default thermostat switches 348 times in a night because noisy readings cross its narrow thresholds repeatedly. Heavy smoothing reduces the switching but delays the reaction to the open vent, letting the temperature dip below 16 °C. A gap of about 1.5–2 °C (for example on below 16.5, off above 18.5, no smoothing) keeps the plants in range with about 62 switches.

You can also open the interactive on the full site when you have a faster connection.

Understand

The median ignores outliers

A median filter outputs the middle value of the last k readings. A spike shorter than half the window lands at one end of the sorted list and is ignored, while real steps still pass through (after a delay of about half the window). Averages, by contrast, spread a spike's error over the whole window.

Filters live inside loops

In a feedback loop the controller acts on the filtered reading. Lag in the filter means the controller sees the world late: it keeps heating after the target is reached and overshoots. A filter that looks perfect on a recorded log can make a live loop oscillate.

Smoothing versus hysteresis

An on/off thermostat near its setpoint chatters as noise crosses the threshold back and forth. Heavy smoothing reduces chatter but adds lag. Hysteresis (switch on below one temperature, off above a higher one) stops chatter without lag, because noise smaller than the gap cannot cross both thresholds.

Explain it another way

Intuitive

Asking five people the time and taking the middle answer ignores the one whose watch is broken. Averaging all five lets the broken watch drag the answer.

Technical

The median filter is non-linear: it preserves edges and rejects impulses up to ⌊k/2⌋ samples long. Filter group delay adds directly to the loop's dead time, which reduces the gain margin of a feedback controller.

Applied

Ultrasonic range sensors occasionally return a wildly wrong echo; robot code commonly runs a 3- or 5-sample median before any averaging so one bad echo can't trigger an emergency stop or hide a real obstacle.

Worked example 1

A 3-sample median over readings 5, 5, 40, 6, 6. Outputs from the third reading onward?

Windows (5,5,40) → 5; (5,40,6) → 6; (40,6,6) → 6. The spike never appears in the output.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 3. A 5-sample median filter faces a glitch that lasts 4 samples. What happens?
Practice 4. Adding a heavy filter to a heater's temperature reading most likely causes…

Apply it

Finish Level 3 of the greenhouse: no more than 10 minutes outside 16–24 °C and at most 80 switches all night. Record your on/off temperatures and smoothing, the switch count and the energy, then explain in two sentences why the gap beats smoothing here.

Reflect

When would you put a median filter before an average, and why in that order?

Write your answer on paper or in your own notes. Lite never stores what you write.

← The exponential filter: one number of memoryImages are arrays of numbers →