stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 145

Day 145 of 365 · Advanced · Month 5: Memory and structure

Smoothing readings

Jumpy readings make control loops flutter. A moving average and exponential smoothing calm them down – with a start value, NaN guard and the price of lag.

Lesson 15 min Week 21

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:

asimilar to an average over63 % of a step after
0.53 values1–2 ticks
0.257 values3–4 ticks
0.0539 valuesabout 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.

Warning

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.

Tip

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.

  • g starts with the first reading (the line before start: 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

Esc
↑ ↓ selectEnter open/ opens search anywhere