2014-07-03 16:00:45 -04:00
|
|
|
//
|
2011-10-09 16:24:50 -04:00
|
|
|
// FILE: RunningAverage.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2015-03-16 16:47:59 -04:00
|
|
|
// VERSION: 0.2.07
|
|
|
|
// DATE: 2015-mar-16
|
2011-10-09 16:24:50 -04:00
|
|
|
// PURPOSE: RunningAverage library for Arduino
|
|
|
|
//
|
2015-03-07 07:47:56 -05:00
|
|
|
// The library stores N individual values in a circular buffer,
|
2014-07-03 16:00:45 -04:00
|
|
|
// to calculate the running average.
|
2011-10-09 16:24:50 -04:00
|
|
|
//
|
2014-07-03 16:00:45 -04:00
|
|
|
// HISTORY:
|
2011-10-09 16:24:50 -04:00
|
|
|
// 0.1.00 - 2011-01-30 initial version
|
|
|
|
// 0.1.01 - 2011-02-28 fixed missing destructor in .h
|
2013-08-17 08:43:42 -04:00
|
|
|
// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web)
|
|
|
|
// http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp
|
|
|
|
// 0.2.01 - 2012-11-21 refactored
|
|
|
|
// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue
|
2014-07-03 16:00:45 -04:00
|
|
|
// 0.2.03 - 2013-11-31 getElement
|
|
|
|
// 0.2.04 - 2014-07-03 added memory protection
|
2014-12-16 16:04:21 -05:00
|
|
|
// 0.2.05 - 2014-12-16 changed float -> double
|
2015-03-07 07:47:56 -05:00
|
|
|
// 0.2.06 - 2015-03-07 all size uint8_t
|
2015-03-16 16:47:59 -04:00
|
|
|
// 0.2.07 - 2015-03-16 added getMin() and getMax() functions
|
2011-10-09 16:24:50 -04:00
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "RunningAverage.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-03-07 07:47:56 -05:00
|
|
|
RunningAverage::RunningAverage(uint8_t size)
|
2011-10-09 16:24:50 -04:00
|
|
|
{
|
2015-03-07 07:47:56 -05:00
|
|
|
_size = size;
|
2014-12-16 16:04:21 -05:00
|
|
|
_ar = (double*) malloc(_size * sizeof(double));
|
2014-07-03 16:00:45 -04:00
|
|
|
if (_ar == NULL) _size = 0;
|
|
|
|
clear();
|
2015-03-16 16:47:59 -04:00
|
|
|
_min = NAN;
|
|
|
|
_max = NAN;
|
2011-10-09 16:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RunningAverage::~RunningAverage()
|
|
|
|
{
|
2014-07-03 16:00:45 -04:00
|
|
|
if (_ar != NULL) free(_ar);
|
2011-10-09 16:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// resets all counters
|
2014-07-03 16:00:45 -04:00
|
|
|
void RunningAverage::clear()
|
|
|
|
{
|
|
|
|
_cnt = 0;
|
|
|
|
_idx = 0;
|
|
|
|
_sum = 0.0;
|
2015-03-07 07:47:56 -05:00
|
|
|
for (uint8_t i = 0; i< _size; i++)
|
|
|
|
{
|
|
|
|
_ar[i] = 0.0; // keeps addValue simple
|
|
|
|
}
|
2011-10-09 16:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// adds a new value to the data-set
|
2015-03-07 07:47:56 -05:00
|
|
|
void RunningAverage::addValue(double value)
|
2011-10-09 16:24:50 -04:00
|
|
|
{
|
2014-07-03 16:00:45 -04:00
|
|
|
if (_ar == NULL) return;
|
|
|
|
_sum -= _ar[_idx];
|
2015-03-07 07:47:56 -05:00
|
|
|
_ar[_idx] = value;
|
2014-07-03 16:00:45 -04:00
|
|
|
_sum += _ar[_idx];
|
|
|
|
_idx++;
|
|
|
|
if (_idx == _size) _idx = 0; // faster than %
|
2015-04-09 18:16:59 -04:00
|
|
|
if (_cnt == 0) _min = _max = f;
|
2015-03-16 16:47:59 -04:00
|
|
|
if (f < _min) _min = f;
|
|
|
|
if (f > _max) _max = f;
|
2014-07-03 16:00:45 -04:00
|
|
|
if (_cnt < _size) _cnt++;
|
2011-10-09 16:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the average of the data-set added sofar
|
2014-12-16 16:04:21 -05:00
|
|
|
double RunningAverage::getAverage()
|
2011-10-09 16:24:50 -04:00
|
|
|
{
|
2014-07-03 16:00:45 -04:00
|
|
|
if (_cnt == 0) return NAN;
|
|
|
|
return _sum / _cnt;
|
|
|
|
}
|
|
|
|
|
2015-03-16 16:47:59 -04:00
|
|
|
// returns the lowest value added to the data-set
|
|
|
|
double RunningAverage::getMin()
|
|
|
|
{
|
|
|
|
if (_cnt == 0) return NAN;
|
|
|
|
return _min;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the highest value added to the data-set
|
|
|
|
double RunningAverage::getMax())
|
|
|
|
{
|
|
|
|
if (_cnt == 0) return NAN;
|
|
|
|
return _max;
|
|
|
|
}
|
|
|
|
|
2014-07-03 16:00:45 -04:00
|
|
|
// returns the value of an element if exist, 0 otherwise
|
2014-12-16 16:04:21 -05:00
|
|
|
double RunningAverage::getElement(uint8_t idx)
|
2014-07-03 16:00:45 -04:00
|
|
|
{
|
|
|
|
if (idx >=_cnt ) return NAN;
|
|
|
|
return _ar[idx];
|
2011-10-09 16:24:50 -04:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:43:42 -04:00
|
|
|
// fill the average with a value
|
|
|
|
// the param number determines how often value is added (weight)
|
|
|
|
// number should preferably be between 1 and size
|
2015-03-07 07:47:56 -05:00
|
|
|
void RunningAverage::fillValue(double value, uint8_t number)
|
2013-08-17 08:43:42 -04:00
|
|
|
{
|
2015-03-07 07:47:56 -05:00
|
|
|
clear(); // TODO conditional? if (clr) clear();
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < number; i++)
|
2014-07-03 16:00:45 -04:00
|
|
|
{
|
|
|
|
addValue(value);
|
|
|
|
}
|
2013-08-17 08:43:42 -04:00
|
|
|
}
|
2011-10-09 16:24:50 -04:00
|
|
|
// END OF FILE
|