RobTillaart / RunningAverage

Arduino library to calculate the running average by means of a circular buffer.
MIT License
53 stars 10 forks source link
arduino average running

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

RunningAverage

Arduino library to calculate the running average by means of a circular buffer.

Description

The RunningAverage object gives a running average of the last N floating point numbers, giving them all equal weight. This is done by adding new data to an internal circular buffer, removing the oldest and replace it by the newest. The size of the internal buffer can be set in the constructor.

By keeping track of the _sum the runningAverage can be calculated fast (only 1 division) at any time. This is done with getFastAverage(). However the constant adding and subtracting when adding new elements to the RA object possibly introduces an ever increasing error. In tests adding up to 1500000 numbers this error was always small. But that is no proof. In version 0.2.16 a fix was added that uses the calculation of the sum in getAverage() to update the internal _sum.

Related

Interface

#include "RunningAverage.h"

Constructor

Basic

The following functions return false if the internal buffer is not allocated.

The following functions returns NAN if there are no values present (count == 0) or of internal array is not allocated.

Extended functions

Admin functions

Partial functions

Last functions

These functions get the basic statistics of the last N added elements. Returns NAN if there are no elements and it will reduce count if there are less than count elements in the buffer.

These functions are useful in cases where you might want to calculate and display the statistics of a subset of the added elements. Reason might be to compare this with the numbers of the whole buffer to notice changes earlier. Otherwise one should create multiple RunningAverage objects each with its own length, effectively having multiple copies of the data added.

Note: if called with a value larger or equal to getCount() (including getSize()) as parameter, the functions will return the statistics of the whole buffer.

Subset (experimental)

Performance

Indicative performance on an UNO, see examples.

Function 0.4.5 us 0.4.6 us Notes
clear 60 60
addValue 24 24
fillValue 1512 1520
getValue 4 8
getAverage 520 552
getFastAverage 36 40
getStandardDeviation 1856 1856
getStandardError 1920 1920
getMin 8 4
getMax 4 4
getMinInBuffer 216 212
getMaxInBuffer 208 208
getSum - 8
bufferIsFull 8 8
getElement 4 4
getSize 8 8
getCount 8 8
last functions - - not tested

Operation

See examples

Future

Must

Should

Could

Wont

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,