Skip to content

Commit

Permalink
Develop (#10)
Browse files Browse the repository at this point in the history
* 0.4.0 increase size to 16 bit
* add examples
  • Loading branch information
RobTillaart authored May 26, 2021
1 parent 655b256 commit b5d01a3
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ update the internal **\_sum**.

### Constructor

- **RunningAverage(const uint8_t size)** allocates dynamic memory, one float (4 bytes) per element.
- **RunningAverage(size)** allocates dynamic memory, one float (4 bytes) per element.
No default size (yet).
- **~RunningAverage()** deconstructor to free the memory allocated.

Expand Down Expand Up @@ -55,7 +55,7 @@ No default size (yet).
### Admin functions

- **bufferIsFull()** returns true if buffer is full.
- **getElement(uint8_t idx)** get element directly from internal buffer. (debug)
- **getElement(idx)** get element directly from internal buffer. (debug)
- **getSize()** idem.
- **getCount()** idem.

Expand Down
39 changes: 24 additions & 15 deletions RunningAverage.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: RunningAverage.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.2
// VERSION: 0.4.0
// DATE: 2015-July-10
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage
Expand Down Expand Up @@ -35,14 +35,15 @@
// 0.3.0 2020-04-16 main refactor
// 0.3.1 2020-06-19 fix library.json; minor refactor
// 0.3.2 2021-01-15 add add() + license + refactor

// 0.4.0 2021-05-18 increase size above 256 elements (16 bit version)

#include "RunningAverage.h"


RunningAverage::RunningAverage(const uint8_t size)
RunningAverage::RunningAverage(const uint16_t size)
{
_size = size;
_partial = _size;
_array = (float*) malloc(_size * sizeof(float));
if (_array == NULL) _size = 0;
clear();
Expand All @@ -63,7 +64,7 @@ void RunningAverage::clear()
_sum = 0.0;
_min = NAN;
_max = NAN;
for (uint8_t i = _size; i > 0; )
for (uint16_t i = _size; i > 0; )
{
_array[--i] = 0.0; // keeps addValue simpler
}
Expand All @@ -80,15 +81,15 @@ void RunningAverage::addValue(const float value)
_sum += _array[_index];
_index++;

if (_index == _size) _index = 0; // faster than %
if (_index == _partial) _index = 0; // faster than %

// handle min max
if (_count == 0) _min = _max = value;
else if (value < _min) _min = value;
else if (value > _max) _max = value;

// update count as last otherwise if ( _count == 0) above will fail
if (_count < _size) _count++;
if (_count < _partial) _count++;
}


Expand All @@ -98,7 +99,7 @@ float RunningAverage::getAverage()
if (_count == 0) return NAN;

_sum = 0;
for (uint8_t i = 0; i < _count; i++)
for (uint16_t i = 0; i < _count; i++)
{
_sum += _array[i];
}
Expand All @@ -121,7 +122,7 @@ float RunningAverage::getMinInBuffer() const
if (_count == 0) return NAN;

float min = _array[0];
for (uint8_t i = 1; i < _count; i++)
for (uint16_t i = 1; i < _count; i++)
{
if (_array[i] < min) min = _array[i];
}
Expand All @@ -135,7 +136,7 @@ float RunningAverage::getMaxInBuffer() const
if (_count == 0) return NAN;

float max = _array[0];
for (uint8_t i = 1; i < _count; i++)
for (uint16_t i = 1; i < _count; i++)
{
if (_array[i] > max) max = _array[i];
}
Expand All @@ -144,7 +145,7 @@ float RunningAverage::getMaxInBuffer() const


// returns the value of an element if exist, NAN otherwise
float RunningAverage::getElement(uint8_t index) const
float RunningAverage::getElement(uint16_t index) const
{
if (index >=_count ) return NAN;

Expand All @@ -159,7 +160,7 @@ float RunningAverage::getStandardDeviation() const

float temp = 0;
float average = getFastAverage();
for (uint8_t i = 0; i < _count; i++)
for (uint16_t i = 0; i < _count; i++)
{
temp += pow((_array[i] - average), 2);
}
Expand Down Expand Up @@ -188,19 +189,19 @@ float RunningAverage::getStandardError() const //++

// 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%
void RunningAverage::fillValue(const float value, const uint8_t number)
void RunningAverage::fillValue(const float value, const uint16_t number)
{
clear();
uint8_t s = number;
uint16_t s = number;
if (s > _size) s = _size;
for (uint8_t i = s; i > 0; i--)
for (uint16_t i = s; i > 0; i--)
{
addValue(value);
}
}


float RunningAverage::getValue(const uint8_t index)
float RunningAverage::getValue(const uint16_t index)
{
if (_count == 0) return NAN;
if (index >= _count) return NAN; // cannot ask more than is added
Expand All @@ -210,4 +211,12 @@ float RunningAverage::getValue(const uint8_t index)
return _array[pos];
}


void RunningAverage::setPartial(const uint16_t part)
{
_partial = part;
if ((_partial == 0) || (_partial > _size)) _partial = _size;
clear();
}

// -- END OF FILE --
59 changes: 32 additions & 27 deletions RunningAverage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: RunningAverage.h
// AUTHOR: [email protected]
// VERSION: 0.3.2
// VERSION: 0.4.0
// DATE: 2016-dec-01
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage
Expand All @@ -13,53 +13,58 @@
#include "Arduino.h"


#define RUNNINGAVERAGE_LIB_VERSION (F("0.3.2"))
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.0"))


class RunningAverage
{
public:
explicit RunningAverage(const uint8_t size);
explicit RunningAverage(const uint16_t size);
~RunningAverage();

void clear();
void add(const float value) { addValue(value); };
void addValue(const float);
void fillValue(const float, const uint8_t);
float getValue(const uint8_t);
void clear();
void add(const float value) { addValue(value); };
void addValue(const float);
void fillValue(const float, const uint16_t);
float getValue(const uint16_t);

float getAverage(); // iterates over all elements.
float getFastAverage() const; // reuses previous calculated values.
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 getStandardDeviation() const;
float getStandardError() const;

// returns min/max added to the data-set since last clear
float getMin() const { return _min; };
float getMax() const { return _max; };
float getMin() const { return _min; };
float getMax() const { return _max; };

// returns min/max from the values in the internal buffer
float getMinInBuffer() const;
float getMaxInBuffer() const;
float getMinInBuffer() const;
float getMaxInBuffer() const;

// return true if buffer is full
bool bufferIsFull() const { return _count == _size; };
bool bufferIsFull() const { return _count == _size; };

float getElement(uint8_t idx) const;
float getElement(uint16_t idx) const;

uint8_t getSize() const { return _size; }
uint8_t getCount() const { return _count; }
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.
void setPartial(const uint16_t part = 0); // 0 ==> use all
uint16_t getPartial() { return _partial; };

protected:
uint8_t _size;
uint8_t _count;
uint8_t _index;
float _sum;
float* _array;
float _min;
float _max;
uint16_t _size;
uint16_t _count;
uint16_t _index;
uint16_t _partial;
float _sum;
float* _array;
float _min;
float _max;
};

// -- END OF FILE --
41 changes: 41 additions & 0 deletions examples/ra_300/ra_300.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// FILE: ra_300.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2021-05-26
//
// PUPROSE: demonstrate large (16 bit) buffer
//

#include "RunningAverage.h"

RunningAverage myRA(300);
int samples = 0;


void setup(void)
{
Serial.begin(115200);
Serial.println("Demo RunningAverage lib");
Serial.print("Version: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);

myRA.clear();
for (uint16_t i = 0; i < 1000; i++)
{
myRA.addValue(i);
Serial.print(i);
Serial.print("\t");
Serial.print(myRA.getCount());
Serial.print("\t");
Serial.print(myRA.getAverage());
Serial.println();
}
}


void loop(void)
{
}

// -- END OF FILE --
8 changes: 4 additions & 4 deletions examples/ra_getValue/ra_getValue.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ void loop(void)
Serial.println();

myRA.clear();
for (int i = 0; i < 7; i++)
for (uint16_t i = 0; i < 7; i++)
{
myRA.addValue(i);
}

Serial.print("\t getValue 0..getCount(): ");
for (int i = 0; i < myRA.getCount(); i++)
for (uint16_t i = 0; i < myRA.getCount(); i++)
{
Serial.print("\t");
Serial.print(myRA.getValue(i), 0);
Expand All @@ -41,13 +41,13 @@ void loop(void)

// note first values (0..2) will be overwritten by 10..12
myRA.clear();
for (int i = 0; i < 13; i++)
for (uint16_t i = 0; i < 13; i++)
{
myRA.addValue(i);
}

Serial.print("\t getValue 0..getCount(): ");
for (int i = 0; i < myRA.getCount(); i++)
for (uint16_t i = 0; i < myRA.getCount(); i++)
{
Serial.print("\t");
Serial.print(myRA.getValue(i), 0);
Expand Down
2 changes: 1 addition & 1 deletion examples/ra_hour/ra_hour.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FILE: runningAverageHour.pde
// FILE: runningAverageHour.ino
// AUTHOR: Rob Tillaart
// DATE: 2012-12-30
//
Expand Down
Loading

0 comments on commit b5d01a3

Please sign in to comment.