-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRunningAverage.h
87 lines (64 loc) · 2.47 KB
/
RunningAverage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
//
// FILE: RunningAverage.h
// AUTHOR: Rob Tillaart
// VERSION: 0.4.7
// DATE: 2011-01-30
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage
//
// The library stores N individual values in a circular buffer,
// to calculate the running average.
#include "Arduino.h"
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.7"))
class RunningAverage
{
public:
explicit RunningAverage(const uint16_t size);
~RunningAverage();
// return false if internal buffer not allocated.
bool clear();
bool add(const float value) { return addValue(value); };
bool addValue(const float value);
bool fillValue(const float value, const uint16_t number);
float getValue(const uint16_t position);
float getAverage(); // iterates over all elements.
float getFastAverage() const; // reuses previous calculated values.
// return statistical characteristics of the running average
float getStandardDeviation() const;
float getStandardError() const;
float getCoefficientOfVariation() const;
// returns min/max added to the data-set since last clear
float getMin() const { return _min; };
float getMax() const { return _max; };
// returns min/max/sum from the values in the internal buffer
float getMinInBuffer() const;
float getMaxInBuffer() const;
float getSum() const { return _sum; };
// return true if buffer is full
bool bufferIsFull() const { return _count == _size; };
float getElement(const uint16_t index) const;
uint16_t getSize() const { return _size; }
uint16_t getCount() const { return _count; }
// use not all elements just a part from 0..partial-1
// (re)setting partial will clear the internal buffer.
bool setPartial(const uint16_t partial = 0); // 0 ==> use all
uint16_t getPartial() { return _partial; };
// get some stats from the last count additions.
float getAverageLast(const uint16_t count);
float getStandardDeviationLast(const uint16_t count);
float getMinInBufferLast(const uint16_t count);
float getMaxInBufferLast(const uint16_t count);
// Experimental 0.4.3
float getAverageSubset(const uint16_t start, const uint16_t count);
protected:
uint16_t _size;
uint16_t _count;
uint16_t _index;
uint16_t _partial;
float _sum;
float* _array;
float _min;
float _max;
};
// -- END OF FILE --