Skip to content
Tactiverge

School/○ Innovator/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

1.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.

  • ◇ Use a median filter to reject isolated spikes, and explain why averages cannot.
  • ◇ Explain how filter lag inside a feedback loop can make control worse.
  • ◇ Tune a thermostat with noisy readings, comparing smoothing with hysteresis.

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

2.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?

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

What is the median of 12, 97, 11, 13, 12?

4.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.

Open BUILD 007: Design a Smart Greenhouse →

5.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

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.

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

  • Every filter is a kind of average.
    ✓ The median is not an average; it is a ranking, which is why it can ignore outliers that averages can't.
  • If a filter makes the plot look nicer, the controller will work better.
    ✓ Inside a loop, lag can make control worse even when the trace looks smoother.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2

For the same readings 12, 97, 11, 13, 12, what is the mean?

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…

Practice 5 · a fresh version every time

An exponential filter uses y_new = y_old + α·(x − y_old) with α = 0.5. Its last output was 19 and the new reading is 11.7. What is the new output?

7.Predict

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

Guess first. Being wrong here is how the surprise works.

8.Reflect

Concepts: Noise and filtering · Open-loop and closed-loop control