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
Expected output
10 12 15
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 * estimateReplace with
0.75 * at(signal, i) + 0.25 * estimateOutput after the change
10 13 16.75