Merge pull request #48 from V0v1kkk/master

Added functions for calculating standard deviation and standard error, function for check fullness of buffer.
This commit is contained in:
Rob Tillaart 2016-12-02 19:08:00 +01:00 committed by GitHub
commit 0be0a563ef
3 changed files with 52 additions and 6 deletions

View File

@ -25,12 +25,13 @@
// 0.2.10 - 2015-09-01 added getFastAverage() and refactored getAverage()
// http://forum.arduino.cc/index.php?topic=50473
// 0.2.11 - 2015-09-04 added getMaxInBuffer() getMinInBuffer() request (Antoon)
//
// 0.2.12 - 2016-12-01 added GetStandardDeviation() GetStandardError() BufferIsFull() (V0v1kkk)
// Released to the public domain
//
#include "RunningAverage.h"
#include <stdlib.h>
#include <math.h>
RunningAverage::RunningAverage(const uint8_t size)
{
@ -119,6 +120,13 @@ double RunningAverage::GetMaxInBuffer() const
return max;
}
// return true if buffer is full
bool RunningAverage::BufferIsFull() const
{
if (_cnt == _size) return true;
return false;
}
// returns the value of an element if exist, NAN otherwise
double RunningAverage::getElement(uint8_t idx) const
{
@ -126,6 +134,32 @@ double RunningAverage::getElement(uint8_t idx) const
return _ar[idx];
}
// Return standard deviation of running average. If buffer is empty, return NAN.
double RunningAverage::GetStandardDeviation() const
{
if (_cnt == 0) return NAN;
double temp = 0;
double average = getFastAverage();
for (uint8_t i = 0; i < _cnt; i++)
{
temp += pow((_ar[i] - average),2);
}
temp = sqrt(temp/(_cnt - 1));
return temp;
}
// Return standard error of running average. If buffer is empty, return NAN.
double RunningAverage::GetStandardError() const //++
{
double temp = GetStandardDeviation();
if(temp==NAN) return NAN;
double n;
if(_cnt>=30) n= _cnt;
else n= _cnt - 1;
temp = temp/sqrt(n);
return temp;
}
// fill the average with a value
// the param number determines how often value is added (weight)
// number should preferably be between 1 and size

View File

@ -1,8 +1,8 @@
//
// FILE: RunningAverage.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2.11
// DATE: 2015-sep-04
// VERSION: 0.2.12
// DATE: 2016-dec-01
// PURPOSE: RunningAverage library for Arduino
// URL: http://arduino.cc/playground/Main/RunningAverage
// HISTORY: See RunningAverage.cpp
@ -17,7 +17,7 @@
#ifndef RunningAverage_h
#define RunningAverage_h
#define RUNNINGAVERAGE_LIB_VERSION "0.2.11"
#define RUNNINGAVERAGE_LIB_VERSION "0.2.12"
#include "Arduino.h"
@ -35,18 +35,27 @@ public:
double getAverage() const; // does iterate over all elements.
double getFastAverage() const; // reuses previous values.
// returns min/max added to the data-set since last clear
// return statistical characteristics of the running average
double GetStandardDeviation() const;
double GetStandardError() const;
// returns min/max added to the data-set since last clear
double getMin() const { return _min; };
double getMax() const { return _max; };
// returns min/max from the values in the internal buffer
// returns min/max from the values in the internal buffer
double GetMinInBuffer() const;
double GetMaxInBuffer() const;
// return true if buffer is full
bool BufferIsFull() const;
double getElement(uint8_t idx) const;
uint8_t getSize() const { return _size; }
uint8_t getCount() const { return _cnt; }
protected:
uint8_t _size;

View File

@ -21,7 +21,10 @@ fillValue KEYWORD2
getElement KEYWORD2
getSize KEYWORD2
getCount KEYWORD2
BufferIsFull() KEYWORD2
getFastAverage KEYWORD2
GetStandardDeviation KEYWORD2
GetStandardError KEYWORD2
GetMinInBuffer KEYWORD2
GetMaxInBuffer KEYWORD2