Creates a moving average for a set number of samples.
It returns a function which in turn yields an average value.
Moving average are useful for computing the average over a recent set of numbers.
A lower number of samples produces a computed value that is lower-latency yet more jittery.
A higher number of samples produces a smoother computed value which takes longer to respond to
changes in data.
Sample size is considered with respect to the level of latency/smoothness trade-off, and also
the rate at which new data is added to the moving average.
A weighting function can be provided to shape how the average is
calculated - eg privileging the most recent data over older data.
It uses Arrays.averageWeighted under the hood.
// Give more weight to data in middle of sampling window constma = movingAverage(100, gaussian());
Because it keeps track of samples previous data, there is a memory impact. A lighter version is movingAverageLight which does not keep a buffer of prior data, but can't be as easily fine-tuned.
Creates a moving average for a set number of
samples
. It returns a function which in turn yields an average value.Moving average are useful for computing the average over a recent set of numbers. A lower number of samples produces a computed value that is lower-latency yet more jittery. A higher number of samples produces a smoother computed value which takes longer to respond to changes in data.
Sample size is considered with respect to the level of latency/smoothness trade-off, and also the rate at which new data is added to the moving average.
A weighting function can be provided to shape how the average is calculated - eg privileging the most recent data over older data. It uses
Arrays.averageWeighted
under the hood.Because it keeps track of
samples
previous data, there is a memory impact. A lighter version is movingAverageLight which does not keep a buffer of prior data, but can't be as easily fine-tuned.