GY-63_MS5611/libraries/RunningAverage/RunningAverage.cpp

339 lines
6.6 KiB
C++
Raw Normal View History

//
2011-10-09 16:24:50 -04:00
// FILE: RunningAverage.cpp
// AUTHOR: Rob Tillaart
2022-11-23 13:22:02 -05:00
// VERSION: 0.4.3
// DATE: 2015-July-10
2020-11-27 05:33:55 -05:00
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage
2011-10-09 16:24:50 -04:00
//
2022-11-23 13:22:02 -05:00
// The library stores N individual values in a circular buffer,
// to calculate the running average.
2021-11-24 04:03:41 -05:00
2011-10-09 16:24:50 -04:00
#include "RunningAverage.h"
2021-01-29 06:31:58 -05:00
2021-05-26 10:39:03 -04:00
RunningAverage::RunningAverage(const uint16_t size)
2011-10-09 16:24:50 -04:00
{
_size = size;
2021-05-26 10:39:03 -04:00
_partial = _size;
2021-01-29 06:31:58 -05:00
_array = (float*) malloc(_size * sizeof(float));
if (_array == NULL) _size = 0;
clear();
2011-10-09 16:24:50 -04:00
}
2021-01-29 06:31:58 -05:00
2011-10-09 16:24:50 -04:00
RunningAverage::~RunningAverage()
{
2021-01-29 06:31:58 -05:00
if (_array != NULL) free(_array);
2011-10-09 16:24:50 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// resets all counters
void RunningAverage::clear()
{
2021-01-29 06:31:58 -05:00
_count = 0;
_index = 0;
_sum = 0.0;
_min = NAN;
_max = NAN;
2021-05-26 10:39:03 -04:00
for (uint16_t i = _size; i > 0; )
{
2021-01-29 06:31:58 -05:00
_array[--i] = 0.0; // keeps addValue simpler
}
2011-10-09 16:24:50 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// adds a new value to the data-set
void RunningAverage::addValue(const float value)
2011-10-09 16:24:50 -04:00
{
2021-11-24 04:03:41 -05:00
if (_array == NULL)
{
return;
}
2021-01-29 06:31:58 -05:00
_sum -= _array[_index];
_array[_index] = value;
_sum += _array[_index];
_index++;
2021-05-26 10:39:03 -04:00
if (_index == _partial) _index = 0; // faster than %
2022-11-23 13:22:02 -05:00
// handle min max
2021-01-29 06:31:58 -05:00
if (_count == 0) _min = _max = value;
else if (value < _min) _min = value;
else if (value > _max) _max = value;
2022-11-23 13:22:02 -05:00
// update count as last otherwise if ( _count == 0) above will fail
2021-05-26 10:39:03 -04:00
if (_count < _partial) _count++;
2011-10-09 16:24:50 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// returns the average of the data-set added so far, NAN if no elements.
float RunningAverage::getAverage()
{
2021-12-28 04:13:44 -05:00
if (_count == 0)
2021-11-24 04:03:41 -05:00
{
return NAN;
}
2022-11-23 13:22:02 -05:00
// OPTIMIZE local variable for sum.
_sum = 0;
2021-05-26 10:39:03 -04:00
for (uint16_t i = 0; i < _count; i++)
{
2021-01-29 06:31:58 -05:00
_sum += _array[i];
}
2021-01-29 06:31:58 -05:00
return _sum / _count; // multiplication is faster ==> extra admin
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// the larger the size of the internal buffer
// the greater the gain wrt getAverage()
float RunningAverage::getFastAverage() const
2011-10-09 16:24:50 -04:00
{
2021-11-24 04:03:41 -05:00
if (_count == 0)
{
return NAN;
}
2022-11-23 13:22:02 -05:00
return _sum / _count; // multiplication is faster ==> extra admin
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// returns the minimum value in the buffer
float RunningAverage::getMinInBuffer() const
{
2021-11-24 04:03:41 -05:00
if (_count == 0)
{
return NAN;
}
2021-11-24 04:03:41 -05:00
float _min = _array[0];
2021-05-26 10:39:03 -04:00
for (uint16_t i = 1; i < _count; i++)
{
2021-11-24 04:03:41 -05:00
if (_array[i] < _min) _min = _array[i];
}
2021-11-24 04:03:41 -05:00
return _min;
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// returns the maximum value in the buffer
float RunningAverage::getMaxInBuffer() const
{
2021-11-24 04:03:41 -05:00
if (_count == 0)
{
return NAN;
}
2021-11-24 04:03:41 -05:00
float _max = _array[0];
2021-05-26 10:39:03 -04:00
for (uint16_t i = 1; i < _count; i++)
{
2021-11-24 04:03:41 -05:00
if (_array[i] > _max) _max = _array[i];
}
2021-11-24 04:03:41 -05:00
return _max;
}
2022-11-23 13:22:02 -05:00
// returns the value of an element if exist, NAN otherwise
2021-05-26 10:39:03 -04:00
float RunningAverage::getElement(uint16_t index) const
{
2021-11-24 04:03:41 -05:00
if (_count == 0)
{
return NAN;
}
2021-01-29 06:31:58 -05:00
return _array[index];
2011-10-09 16:24:50 -04:00
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// Return standard deviation of running average.
// If buffer is empty or has only one element, return NAN.
float RunningAverage::getStandardDeviation() const
{
2022-11-23 13:22:02 -05:00
// see issue #13
// need float _stddev = -1;
// + patch add() and clear() to reset _stddev to -1;
// if (_stddev != -1) return _stddev;
2021-12-28 04:13:44 -05:00
if (_count <= 1) return NAN;
float temp = 0;
float average = getFastAverage();
2021-05-26 10:39:03 -04:00
for (uint16_t i = 0; i < _count; i++)
{
2021-01-29 06:31:58 -05:00
temp += pow((_array[i] - average), 2);
}
2021-01-29 06:31:58 -05:00
temp = sqrt(temp/(_count - 1));
return temp;
2022-11-23 13:22:02 -05:00
// see issue #13
// _stddev = temp; // cache the calculate value
// return _stddev;
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// Return standard error of running average.
// If buffer is empty or has only one element, return NAN.
2021-12-28 04:13:44 -05:00
float RunningAverage::getStandardError() const
{
2021-11-24 04:03:41 -05:00
float temp = getStandardDeviation();
if (temp == NAN) return NAN;
float n;
2021-01-29 06:31:58 -05:00
if (_count >= 30) n = _count;
else n = _count - 1;
2020-11-27 05:33:55 -05:00
temp = temp/sqrt(n);
return temp;
}
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
// fill the average with the same value number times. (weight)
// This is maximized to size times.
// no need to fill the internal buffer over 100%
2021-05-26 10:39:03 -04:00
void RunningAverage::fillValue(const float value, const uint16_t number)
{
clear();
2021-05-26 10:39:03 -04:00
uint16_t s = number;
2022-11-23 13:22:02 -05:00
if (s > _partial) s = _partial;
2021-05-26 10:39:03 -04:00
for (uint16_t i = s; i > 0; i--)
{
addValue(value);
}
}
2022-11-23 13:22:02 -05:00
2021-12-28 04:13:44 -05:00
// https://github.com/RobTillaart/RunningAverage/issues/13
// - substantially faster version off fillValue()
// - adds to program size
// void RunningAverage::fillValue(const float value, const uint16_t number)
// {
// uint16_t s = number;
2022-11-23 13:22:02 -05:00
// if (s > _partial) s = _partial;
// for (uint16_t i = 0; i < s; i++)
2021-12-28 04:13:44 -05:00
// {
2022-11-23 13:22:02 -05:00
// _array[i] = value;
2021-12-28 04:13:44 -05:00
// }
// _min = value;
// _max = value;
2022-11-23 13:22:02 -05:00
// _sum = value * s;
2021-12-28 04:13:44 -05:00
// _count = s;
2022-11-23 13:22:02 -05:00
// _index = s;
// if (_index == _partial) _index = 0;
2021-12-28 04:13:44 -05:00
// }
2021-01-29 06:31:58 -05:00
2021-11-24 04:03:41 -05:00
float RunningAverage::getValue(const uint16_t position)
{
2021-11-24 04:03:41 -05:00
if (_count == 0)
{
return NAN;
}
if (position >= _count)
{
return NAN; // cannot ask more than is added
}
2021-01-29 06:31:58 -05:00
2021-11-24 04:03:41 -05:00
uint16_t _pos = position + _index;
if (_pos >= _count) _pos -= _count;
return _array[_pos];
}
2020-11-27 05:33:55 -05:00
2021-05-26 10:39:03 -04:00
2021-11-24 04:03:41 -05:00
void RunningAverage::setPartial(const uint16_t partial)
2021-05-26 10:39:03 -04:00
{
2021-11-24 04:03:41 -05:00
_partial = partial;
2021-05-26 10:39:03 -04:00
if ((_partial == 0) || (_partial > _size)) _partial = _size;
clear();
}
2021-11-24 04:03:41 -05:00
////////////////////////////////////////////////////////////////
//
2022-11-23 13:22:02 -05:00
// 0.4.1 added.
2021-11-24 04:03:41 -05:00
//
float RunningAverage::getAverageLast(uint16_t count)
{
uint16_t cnt = count;
if (cnt > _count) cnt = _count;
if (cnt == 0) return NAN;
uint16_t idx = _index;
float _sum = 0;
for (uint16_t i = 0; i < cnt; i++)
{
if (idx == 0) idx = _size;
idx--;
_sum +=_array[idx];
}
return _sum / cnt;
}
float RunningAverage::getMinInBufferLast(uint16_t count)
{
uint16_t cnt = count;
if (cnt > _count) cnt = _count;
if (cnt == 0) return NAN;
uint16_t idx = _index;
if (idx == 0) idx = _size;
idx--;
float _min = _array[idx];
for (uint16_t i = 0; i < cnt; i++)
{
if (_array[idx] < _min) _min = _array[idx];
if (idx == 0) idx = _size;
idx--;
}
return _min;
}
float RunningAverage::getMaxInBufferLast(uint16_t count)
{
uint16_t cnt = count;
if (cnt > _count) cnt = _count;
if (cnt == 0) return NAN;
uint16_t idx = _index;
if (idx == 0) idx = _size;
idx--;
float _max = _array[idx];
for (uint16_t i = 0; i < cnt; i++)
{
if (_array[idx] > _max) _max = _array[idx];
if (idx == 0) idx = _size;
idx--;
}
return _max;
}
2022-11-23 13:22:02 -05:00
////////////////////////////////////////////////////////////////
//
// Experimental 0.4.3
//
float RunningAverage::getAverageSubset(uint16_t start, uint16_t count)
{
if (_count == 0)
{
return NAN;
}
uint16_t cnt = _count;
if (cnt > count) cnt = count;
float sum = 0; // do not disrupt global _sum
for (uint16_t i = 0; i < cnt; i++)
{
uint16_t idx = _index + start + i;
while (idx >= _partial) idx -= _partial;
sum += _array[idx];
}
return sum / cnt;
}
2020-11-27 05:33:55 -05:00
// -- END OF FILE --
2021-11-24 04:03:41 -05:00