What you learn today
- Moving average
- Exponential smoothing
- Noise versus lag
The gas sensor in the hangar hangs right next to the vent. Its pressure readings jump by a few kPa every tick, and the control behind it switches the pump on and off every second. The pressure in the room is perfectly calm – only the measurement is jumpy. The fix: smooth the readings before the control loop sees them.
Moving average
The simplest way: remember the last N readings and take their average. The values live in a ring buffer in the stack, as in the project “Measurement history”. An index runs in a circle and always overwrites the oldest value:
add i i 1
mod i i N # 0 to N-1, then 0 again
poke i x # replace the oldest value
move sum 0
move k 0
adding:
get tmp db k
add sum sum tmp
add k k 1
blt k N adding
div mean sum N
A single outlier now counts for only one N-th. In return the average needs N ticks until a real step has fully arrived, and every slot in the buffer costs stack.
Exponential smoothing
Without a buffer it works with a single formula:
g = g + a · (x − g)
g is the smoothed value, x the new reading, a a share between 0 and 1. Every tick g moves closer to the reading by that share:
sub d x g
mul d d SHARE # SHARE = 0.25
add g g d
Three lines, one register for the state – no stack, no loop. With a = 0.25 and a step from 100 to 120 kPa, g shows 105, 108.75, 111.6 … one after another and keeps getting closer to the new value.
The start value matters. If g starts at 0, it creeps up to 100 kPa only slowly after switching on – a control loop thinks the room is nearly empty all that time. So set g to the first reading before the main loop.
Noise versus lag
Smoothing has a price. The stronger you smooth, the later the chip notices a real change:
| a | similar to an average over | 63 % of a step after |
|---|---|---|
| 0.5 | 3 values | 1–2 ticks |
| 0.25 | 7 values | 3–4 ticks |
| 0.05 | 39 values | about 20 ticks, so 10 s |
A display may well be sluggish. For a control loop a little smoothing is usually enough, the hysteresis does the rest.
A single NaN value poisons the filter. If the sensor is missing for one tick, g + a · (NaN − g) becomes NaN – and stays that way forever, even long after the sensor is back. In the ring buffer a NaN spoils the average until it is overwritten after N ticks. Check the reading with snan before it goes into the filter, or start over with the next valid value.
Emergency stops and overpressure protection belong on the unsmoothed values. A filter that swallows noise also swallows the first ticks of a real emergency.
Exercise
Task
The gas sensor “Halle” delivers a jumpy pressure. The LED display “Druck” should show the exponentially smoothed value, with SHARE = 0.25.
gstarts with the first reading (the line beforestart:is already there)- If the reading is missing (NaN), the display shows −1
- After that the filter starts over with the next valid reading instead of dragging NaN along
Tip: if g is NaN, simply take x.
Hints
Hint 1
Start over: snan tmp g, then select g tmp x g.
Hint 2
Smoothing: sub d x g, mul d d SHARE, add g g d.
Hint 3
If x is NaN, the calculation itself makes g NaN – in the next tick the start-over kicks in.
Quiz
5 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in