A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd
MIT licensed
Join the ETL community
cumulative_moving_average
A moving average algorithm that continuously calculates an average value from a stream of samples.
The sample size does not affect the size of the instantiated object. There is no overhead based on the number of
samples.

There are four variants of the algorithm; two for integral values and two for floating point. Each sub  variant allows the
selection of compile time or run time sample size.
The integral variant allows a compile time 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.
If this value is zero, then the run time sample size specialisation is used.

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.
__________________________________________________________________________________________________
cumulative_moving_average(const T initial_value, const size_t sample_size)

For runtime sample size specialisation only.
Constructs the object with the initial value for the average and the sample size.
__________________________________________________________________________________________________
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.
__________________________________________________________________________________________________
void set_sample_size(const size_t sample_size)

For runtime sample size specialisation only.
Sets the sample size.

____________________________________________________________________________________________________
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.

__________________________________________________________________________________________________
cumulative_moving_average(const T initial_value, const size_t sample_size)

For runtime sample size specialisation only.
Constructs the object with the initial value for the average and the sample size.

__________________________________________________________________________________________________
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.
__________________________________________________________________________________________________
void set_sample_size(const size_t sample_size)

For runtime sample size specialisation only.
Sets the sample size.

____________________________________________________________________________________________________
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, all we
need to do is 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