A moving average calculator (exponential weighted moving average) which does not keep track of
previous samples. Less accurate, but uses less system resources.
The scaling parameter determines smoothing. A value of 1 means that
the latest value is used as the average - that is, no smoothing. Higher numbers
introduce progressively more smoothing by weighting the accumulated prior average more heavily.
Note that the final average of 50 is pretty far from the last value of 0. To make it more responsive,
we could use a lower scaling factor: movingAverageLight(2). This yields a final average of 37.5 instead.
A moving average calculator (exponential weighted moving average) which does not keep track of previous samples. Less accurate, but uses less system resources.
The
scaling
parameter determines smoothing. A value of1
means that the latest value is used as the average - that is, no smoothing. Higher numbers introduce progressively more smoothing by weighting the accumulated prior average more heavily.Note that the final average of 50 is pretty far from the last value of 0. To make it more responsive, we could use a lower scaling factor:
movingAverageLight(2)
. This yields a final average of37.5
instead.