https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average

The Wiki page shows that the formula can be simplified to a form not requiring a multiplication, but it requires that an interim value may become negative. This may be a problem if you are using scaled unsigned integral average values.

]]>((Old_Average * (Sample_Size-1)) + New_Sample) / Sample_Size

Have not checked if it is right…

Have done similar calculations

avg = avg * 0.9 + sample * 0.1

replace 10% of old avg with the new sample

The idea to change sample size on the fly is a bit interesting. Thinking it could maybe be used to implement a filter that does not lag in certain situations. Ex. after an actuator have been activated, and we expect the signal to change.

]]>Thanks. ]]>

template

constexpr std::size_t arraySize(T (&)[N] ){ return N; }