2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/RunningAverage/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/RunningAverage/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/RunningAverage.svg?maxAge=3600)](https://github.com/RobTillaart/RunningAverage/releases)
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
# RunningAverage
|
|
|
|
|
|
|
|
Arduino library to calculate the running average by means of a circular buffer.
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
## Description
|
|
|
|
The RunningAverage object gives a running average of the last N numbers, giving them
|
2021-01-29 06:31:58 -05:00
|
|
|
all equal weight. This is done by adding new data to an internal circular buffer,
|
2020-11-27 05:33:55 -05:00
|
|
|
removing the oldest and replace it by the newest. The size of the internal buffer
|
2021-01-29 06:31:58 -05:00
|
|
|
can be set in the constructor.
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
By keeping track of the **\_sum** the runningAverage can be calculated fast (only 1 division).
|
2020-11-27 05:33:55 -05:00
|
|
|
This is done with **getFastAverage()**.
|
|
|
|
However the constant adding/subtracting when adding new elements introduces an accumulating error.
|
|
|
|
In tests adding up to 1500000 numbers this error was always small. But that is no proof.
|
|
|
|
In version 0.2.16 there is a fix added that uses the calculation of the sum in **getAverage()** to
|
2021-01-29 06:31:58 -05:00
|
|
|
update the internal **\_sum**.
|
|
|
|
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
### Constructor
|
|
|
|
|
2021-05-26 10:39:03 -04:00
|
|
|
- **RunningAverage(size)** allocates dynamic memory, one float (4 bytes) per element.
|
2021-01-29 06:31:58 -05:00
|
|
|
No default size (yet).
|
|
|
|
- **~RunningAverage()** deconstructor to free the memory allocated.
|
|
|
|
|
|
|
|
|
|
|
|
### Basic
|
|
|
|
|
|
|
|
- **clear()** empties internal buffer.
|
|
|
|
- **add(value)** wrapper for **addValue()**
|
|
|
|
- **addValue(value)** adds a new value to the object, if internal buffer is full, the oldest element is removed.
|
|
|
|
- **fillValue(value, nr)** adds nr elements of value. Good for initializing system.
|
|
|
|
- **getValue(pos)** returns element at position **pos**.
|
|
|
|
- **getAverage()** iterates over all elements to get the average, slower but accurate
|
|
|
|
- **getFastAverage()** reuses previous calculated values, therefor faster. Accuracy can drift.
|
|
|
|
|
|
|
|
|
|
|
|
### Extended functions
|
|
|
|
|
|
|
|
- **getStandardDeviation()** returns the stddev of the current content. Needs more than one element.
|
|
|
|
- **getStandardError()** returns the stderror of the current content.
|
|
|
|
- **getMin()** returns minimum since last clear, does not need to be in the buffer.
|
|
|
|
- **getMax()** returns maximum since last clear, does not need to be in the buffer.
|
|
|
|
- **getMinInBuffer()** returns minimum in the internal buffer.
|
|
|
|
- **getMaxInBuffer()** returns maxumum in the internal buffer.
|
|
|
|
|
|
|
|
|
|
|
|
### Admin functions
|
|
|
|
|
|
|
|
- **bufferIsFull()** returns true if buffer is full.
|
2021-05-26 10:39:03 -04:00
|
|
|
- **getElement(idx)** get element directly from internal buffer. (debug)
|
2021-01-29 06:31:58 -05:00
|
|
|
- **getSize()** idem.
|
|
|
|
- **getCount()** idem.
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
## Operation
|
|
|
|
|
|
|
|
See examples
|