Skip to content

Commit 2cf71d9

Browse files
committed
Added functions for calculating standard deviation and standard error of running average. They may be useful for statistical evaluation of running average.
Also added function for check fullness of buffer.
1 parent 4d9a7d6 commit 2cf71d9

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

libraries/RunningAverage/RunningAverage.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
// 0.2.10 - 2015-09-01 added getFastAverage() and refactored getAverage()
2626
// http://forum.arduino.cc/index.php?topic=50473
2727
// 0.2.11 - 2015-09-04 added getMaxInBuffer() getMinInBuffer() request (Antoon)
28-
//
28+
// 0.2.12 - 2016-12-01 added GetStandardDeviation() GetStandardError() BufferIsFull() (V0v1kkk)
2929
// Released to the public domain
3030
//
3131

3232
#include "RunningAverage.h"
3333
#include <stdlib.h>
34+
#include <math.h>
3435

3536
RunningAverage::RunningAverage(const uint8_t size)
3637
{
@@ -119,13 +120,46 @@ double RunningAverage::GetMaxInBuffer() const
119120
return max;
120121
}
121122

123+
// return true if buffer is full
124+
bool RunningAverage::BufferIsFull() const
125+
{
126+
if (_cnt == _size) return true;
127+
return false;
128+
}
129+
122130
// returns the value of an element if exist, NAN otherwise
123131
double RunningAverage::getElement(uint8_t idx) const
124132
{
125133
if (idx >=_cnt ) return NAN;
126134
return _ar[idx];
127135
}
128136

137+
// Return standard deviation of running average. If buffer is empty, return NAN.
138+
double RunningAverage::GetStandardDeviation() const
139+
{
140+
if (_cnt == 0) return NAN;
141+
double temp = 0;
142+
double average = getFastAverage();
143+
for (uint8_t i = 0; i < _cnt; i++)
144+
{
145+
temp += pow((_ar[i] - average),2);
146+
}
147+
temp = sqrt(temp/(_cnt - 1));
148+
return temp;
149+
}
150+
151+
// Return standard error of running average. If buffer is empty, return NAN.
152+
double RunningAverage::GetStandardError() const //++
153+
{
154+
double temp = GetStandardDeviation();
155+
if(temp==NAN) return NAN;
156+
double n;
157+
if(_cnt>=30) n= _cnt;
158+
else n= _cnt - 1;
159+
temp = temp/sqrt(n);
160+
return temp;
161+
}
162+
129163
// fill the average with a value
130164
// the param number determines how often value is added (weight)
131165
// number should preferably be between 1 and size

libraries/RunningAverage/RunningAverage.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// FILE: RunningAverage.h
33
// AUTHOR: Rob dot Tillaart at gmail dot com
4-
// VERSION: 0.2.11
5-
// DATE: 2015-sep-04
4+
// VERSION: 0.2.12
5+
// DATE: 2016-dec-01
66
// PURPOSE: RunningAverage library for Arduino
77
// URL: http://arduino.cc/playground/Main/RunningAverage
88
// HISTORY: See RunningAverage.cpp
@@ -17,7 +17,7 @@
1717
#ifndef RunningAverage_h
1818
#define RunningAverage_h
1919

20-
#define RUNNINGAVERAGE_LIB_VERSION "0.2.11"
20+
#define RUNNINGAVERAGE_LIB_VERSION "0.2.12"
2121

2222
#include "Arduino.h"
2323

@@ -35,18 +35,27 @@ class RunningAverage
3535
double getAverage() const; // does iterate over all elements.
3636
double getFastAverage() const; // reuses previous values.
3737

38-
// returns min/max added to the data-set since last clear
38+
// return statistical characteristics of the running average
39+
double GetStandardDeviation() const;
40+
double GetStandardError() const;
41+
42+
// returns min/max added to the data-set since last clear
3943
double getMin() const { return _min; };
4044
double getMax() const { return _max; };
4145

42-
// returns min/max from the values in the internal buffer
46+
// returns min/max from the values in the internal buffer
4347
double GetMinInBuffer() const;
4448
double GetMaxInBuffer() const;
49+
50+
// return true if buffer is full
51+
bool BufferIsFull() const;
4552

4653
double getElement(uint8_t idx) const;
4754

4855
uint8_t getSize() const { return _size; }
4956
uint8_t getCount() const { return _cnt; }
57+
58+
5059

5160
protected:
5261
uint8_t _size;

libraries/RunningAverage/keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ fillValue KEYWORD2
2121
getElement KEYWORD2
2222
getSize KEYWORD2
2323
getCount KEYWORD2
24+
BufferIsFull() KEYWORD2
2425
getFastAverage KEYWORD2
26+
GetStandardDeviation KEYWORD2
27+
GetStandardError KEYWORD2
2528
GetMinInBuffer KEYWORD2
2629
GetMaxInBuffer KEYWORD2
2730

0 commit comments

Comments
 (0)