A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd
Join the ETL community
cumulative_moving_average
A moving average algorithm that continuously calculates an average value from a stream of samples.

There are two variants of the algorithm; one for integral values and another for floating point.
Both allow compile time specification of the sample size while the integral variant also allows a scaling factor to emulate
fixed point arithmetic.

____________________________________________________________________________________________________
Integral

template <typename T, const size_t SAMPLE_SIZE, const size_t SCALING>
cumulative_moving_average;
__________________________________________________________________________________________________
static const size_t SAMPLE_SIZE

The number of samples averaged over.
__________________________________________________________________________________________________
static const size_t SCALING

The sample scaling factor.
__________________________________________________________________________________________________
cumulative_moving_average(const T initial_value)

Constructs the object with the initial value for the average.
__________________________________________________________________________________________________
void clear(const T initial_value)

Clears the object to the initial value for the average.
__________________________________________________________________________________________________
void add(T new_value)

Adds a new sample value.
__________________________________________________________________________________________________
T value() const

Returns the scaled value of the average.

To unscale the returned value, use one of the rounding found in scaled_rounding.

____________________________________________________________________________________________________
Floating point

template <typename T, const size_t SAMPLE_SIZE>
cumulative_moving_average;

__________________________________________________________________________________________________
static const size_t SAMPLE_SIZE
The number of samples averaged over.

__________________________________________________________________________________________________
cumulative_moving_average(const T initial_value)

Constructs the object with the initial value for the average.

__________________________________________________________________________________________________
void clear(const T initial_value)

Clears the object to the initial value for the average.

__________________________________________________________________________________________________
void add(T new_value)

Adds a new sample value.

__________________________________________________________________________________________________
T value() const

Returns the average value.

____________________________________________________________________________________________________
How It Works

If the current moving average is 5, then an equivalent sequence of samples (for a sample size of 9), that gives the same
average, would be 5, 5, 5, 5, 5, 5, 5, 5, 5

This means, to find the average when adding a new sample to a moving average that has a current value of 5, we need to
multiply the current average by the sample size (9), add the new sample, and divide by the sample size + 1 (10).

Wikipedia
cumulative_moving_average.h