BlyxDocumentation
Browse chapters
Documentation / Numerical programming

Smooth a sequence with one running value

Turn a noisy signal into a sequence of estimates and trace how much history each estimate retains.

Set the initial state

The first reading is the first estimate. Print it before the loop and begin processing at index one. This convention gives one output per input, including for a single reading. The example assumes a nonempty signal because it reads index zero.

Blend old and new

Each later estimate is half the new reading plus half the previous estimate. For readings 10, 14, 18, the second estimate is 0.5 × 14 + 0.5 × 10 = 12. The third uses 12 as its old state and becomes 15.

Understand the tradeoff

More weight on the new reading makes the output respond faster to changes. Less weight preserves more history. Choose complementary weights that sum to one so a constant signal stays constant. A weight of one on the new reading reproduces the input.

Check a boundary case

Feed the algorithm three copies of 6. Its estimate should remain 6 after every pass. This checks both the initial value and the blend. Then change one input at a time so the reason for any output change is visible.

Try the program

Output Ready
Expected output
10
12
15
Open in the full playground →

Practice

Give the new reading weight 0.75 and the previous estimate weight 0.25.

Hint

Both coefficients occur on the line that updates estimate.

Worked solution

The second estimate is 13. The last uses 13 as its old state, yielding 16.75.

Find

0.5 * at(signal, i) + 0.5 * estimate

Replace with

0.75 * at(signal, i) + 0.25 * estimate

Output after the change

10
13
16.75