Skip to content

Commit

Permalink
Develop (#27)
Browse files Browse the repository at this point in the history
- update readme.md badges
- update examples
- add two step example
- minor edits
  • Loading branch information
RobTillaart committed Nov 13, 2023
1 parent f26a767 commit dbf3c3c
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 92 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.4] - 2023-10-18
- update readme.md badges
- update examples
- add two step example
- minor edits


## [0.4.3] - 2022-11-23
- add changelog.md
- add RP2040 to build-CI
- add examples
- add getAverageSubset(start, count) - experimental
- update readme.md


## [0.4.2] - 2021-12-28
- update license
- minor edits
Expand Down
69 changes: 58 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
[![Arduino CI](https://github.com/RobTillaart/RunningAverage/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/RunningAverage/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/RunningAverage/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/RunningAverage/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/RunningAverage/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/RunningAverage.svg)](https://github.com/RobTillaart/RunningAverage/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/RunningAverage/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/RunningAverage.svg?maxAge=3600)](https://github.com/RobTillaart/RunningAverage/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/RunningAverage.svg)](https://registry.platformio.org/libraries/robtillaart/RunningAverage)


# RunningAverage
Expand All @@ -21,14 +24,31 @@ The size of the internal buffer can be set in the constructor.

By keeping track of the **\_sum** the runningAverage can be calculated fast (only 1 division)
at any time. This is done with **getFastAverage()**.
However the constant adding and subtracting when adding new elements possibly introduces an ever increasing error.
However the constant adding and subtracting when adding new elements to the RA object possibly
introduces an ever increasing error.
In tests adding up to 1500000 numbers this error was always small. But that is no proof.
In version 0.2.16 a fix was added that uses the calculation of the sum in **getAverage()** to
update the internal **\_sum**.


#### Related

- https://github.com/RobTillaart/Correlation
- https://github.com/RobTillaart/GST - Golden standard test metrics
- https://github.com/RobTillaart/Histogram
- https://github.com/RobTillaart/RunningAngle
- https://github.com/RobTillaart/RunningAverage
- https://github.com/RobTillaart/RunningMedian
- https://github.com/RobTillaart/statHelpers - combinations & permutations
- https://github.com/RobTillaart/Statistic


## Interface

```cpp
#include "RunningAverage.h"
```

### Constructor

- **RunningAverage(uint16_t size)** allocates dynamic memory, one float (4 bytes) per element.
Expand All @@ -40,8 +60,10 @@ No default size (yet).

- **void clear()** empties the internal buffer.
- **void add(float value)** wrapper for **addValue()**
- **void addValue(float value)** adds a new value to the object, if internal buffer is full, the oldest element is removed.
- **void fillValue(float value, uint16_t number)** adds number elements of value. Good for initializing the system to z certain starting average.
- **void addValue(float value)** adds a new value to the object, if the internal buffer is full,
the oldest element is removed.
- **void fillValue(float value, uint16_t number)** adds number elements of value.
Good for initializing the system to a certain starting average.
- **float getValue(uint16_t position)** returns the value at **position** from the additions.
Position 0 is the first one to disappear.
- **float getAverage()** iterates over all elements to get the average, slower but accurate.
Expand All @@ -68,15 +90,15 @@ Needs more than one element to be calculable.
- **uint16_t getCount()** returns the number of slots used of the internal array.


## Partial
## Partial functions

- **void setPartial(uint16_t partial = 0)** use only a part of the internal array.
Allows to change the weight and history factor.
0 ==> use all == default.
- **uint16_t getPartial()** returns the set value for partial.


## Last
## Last functions

These functions get the basic statistics of the last N added elements.
Returns NAN if there are no elements and it will reduce count if there are less than
Expand All @@ -92,7 +114,7 @@ numbers of the whole buffer to notice changes earlier.
Otherwise one should create multiple RunningAverage objects each with its own length,
effectively having multiple copies of the data added.

Note: if called with a value larger or equal to **getCount()** (incl **getSize()**) as
Note: if called with a value larger or equal to **getCount()** (including **getSize()**) as
parameter, the functions will return the statistics of the whole buffer.


Expand All @@ -109,15 +131,40 @@ See examples

## Future

#### must

#### Must

- update documentation, explain better

#### should
- add error handling (important?)
#### Should

- check for optimizations.
- divide by count happens often ...
- clear(bool zero = true) to suppress setting all to 0. ?

#### could
- default size for constructor
#### Could

- create a double based derived class? Template class?
- add error handling (important?).
- investigate **modus()** most frequently occurring value.
- difficult with floats ?
- what to do when on two or more values are on par?

#### Wont

- default size for constructor
- unknown what would be a good choice.
- clear(bool zero = true) to suppress setting all to 0. ?
- makes **addValue()** slightly more complex
- could introduce conflicts due to randomness data?


## Support

If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.

Thank you,


21 changes: 6 additions & 15 deletions RunningAverage.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// FILE: RunningAverage.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.3
// DATE: 2015-July-10
// VERSION: 0.4.4
// DATE: 2011-01-30
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage
//
Expand Down Expand Up @@ -39,7 +39,7 @@ void RunningAverage::clear()
_max = NAN;
for (uint16_t i = _size; i > 0; )
{
_array[--i] = 0.0; // keeps addValue simpler
_array[--i] = 0.0; // keeps addValue simpler
}
}

Expand All @@ -57,7 +57,7 @@ void RunningAverage::addValue(const float value)
_sum += _array[_index];
_index++;

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

// handle min max
if (_count == 0) _min = _max = value;
Expand All @@ -82,7 +82,7 @@ float RunningAverage::getAverage()
{
_sum += _array[i];
}
return _sum / _count; // multiplication is faster ==> extra admin
return _sum / _count; // multiplication is faster ==> extra admin
}


Expand Down Expand Up @@ -246,10 +246,6 @@ void RunningAverage::setPartial(const uint16_t partial)
}


////////////////////////////////////////////////////////////////
//
// 0.4.1 added.
//
float RunningAverage::getAverageLast(uint16_t count)
{
uint16_t cnt = count;
Expand Down Expand Up @@ -308,11 +304,6 @@ float RunningAverage::getMaxInBufferLast(uint16_t count)
}



////////////////////////////////////////////////////////////////
//
// Experimental 0.4.3
//
float RunningAverage::getAverageSubset(uint16_t start, uint16_t count)
{
if (_count == 0)
Expand All @@ -334,5 +325,5 @@ float RunningAverage::getAverageSubset(uint16_t start, uint16_t count)
}


// -- END OF FILE --
// -- END OF FILE --

12 changes: 6 additions & 6 deletions RunningAverage.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once
//
// FILE: RunningAverage.h
// AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.4.3
// DATE: 2016-dec-01
// AUTHOR: Rob Tillaart
// VERSION: 0.4.4
// DATE: 2011-01-30
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage


#include "Arduino.h"


#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.3"))
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.4"))


class RunningAverage
Expand All @@ -26,7 +26,7 @@ class RunningAverage
void fillValue(const float value, const uint16_t number);
float getValue(const uint16_t position);

float getAverage(); // iterates over all elements.
float getAverage(); // iterates over all elements.
float getFastAverage() const; // reuses previous calculated values.

// return statistical characteristics of the running average
Expand Down Expand Up @@ -76,5 +76,5 @@ class RunningAverage
};


// -- END OF FILE --
// -- END OF FILE --

4 changes: 2 additions & 2 deletions examples/fillValue/fillValue.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void setup(void)
{
Serial.begin(115200);
Serial.print("Demo RunningAverage lib - fillValue ");
Serial.print("Version: ");
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
delay(10);

Expand Down Expand Up @@ -60,5 +60,5 @@ void measure_duration(int n)
}


// -- END OF FILE --
// -- END OF FILE --

7 changes: 4 additions & 3 deletions examples/ra_300/ra_300.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ int samples = 0;
void setup(void)
{
Serial.begin(115200);
Serial.println("Demo RunningAverage lib");
Serial.print("Version: ");
Serial.println();
Serial.println(__FILE__);
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);

myRA.clear();
Expand All @@ -37,5 +38,5 @@ void loop(void)
}


// -- END OF FILE --
// -- END OF FILE --

7 changes: 4 additions & 3 deletions examples/ra_300_last/ra_300_last.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ int samples = 0;
void setup(void)
{
Serial.begin(115200);
Serial.println("Demo RunningAverage lib");
Serial.print("Version: ");
Serial.println();
Serial.println(__FILE__);
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);

myRA.clear();
Expand Down Expand Up @@ -50,5 +51,5 @@ void loop(void)
}


// -- END OF FILE --
// -- END OF FILE --

6 changes: 3 additions & 3 deletions examples/ra_FastAverageTest/ra_FastAverageTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ uint32_t start, stop;
void setup(void)
{
Serial.begin(115200);
Serial.print("\nDemo ");
Serial.println();
Serial.println(__FILE__);
Serial.print("Version: ");
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
myRA.clear(); // explicitly start clean

Expand Down Expand Up @@ -90,5 +90,5 @@ void test(long n)
}


// -- END OF FILE --
// -- END OF FILE --

8 changes: 4 additions & 4 deletions examples/ra_MinMaxBufferTest/ra_MinMaxBufferTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ int samples = 0;
void setup(void)
{
Serial.begin(115200);
Serial.print("\nDemo ");
Serial.println();
Serial.println(__FILE__);
Serial.print("Version: ");
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
myRA.clear(); // explicitly start clean
myRA.clear(); // explicitly start clean

Serial.println("\nCNT\tMIN\tMINBUF\tMAX\tMAXBUF");
}
Expand Down Expand Up @@ -49,5 +49,5 @@ void loop(void)
}


// -- END OF FILE --
// -- END OF FILE --

9 changes: 5 additions & 4 deletions examples/ra_MinMaxTest/ra_MinMaxTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ int samples = 0;
void setup(void)
{
Serial.begin(115200);
Serial.println("\nDemo runningAverageMinMaxTest");
Serial.print("Version: ");
Serial.println();
Serial.println(__FILE__);
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
myRA.clear(); // explicitly start clean
myRA.clear(); // explicitly start clean

Serial.println("\nCNT\tMIN\tAVG\tMAX");
}
Expand Down Expand Up @@ -46,5 +47,5 @@ void loop(void)
}


// -- END OF FILE --
// -- END OF FILE --

7 changes: 4 additions & 3 deletions examples/ra_fillValue_test/ra_fillValue_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ uint32_t start, stop;
void setup(void)
{
Serial.begin(115200);
Serial.print("Demo RunningAverage lib - fillValue ");
Serial.print("Version: ");
Serial.println();
Serial.println(__FILE__);
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
delay(10);

Expand Down Expand Up @@ -68,4 +69,4 @@ void check_statistics()
}


// -- END OF FILE --
// -- END OF FILE --
Loading

0 comments on commit dbf3c3c

Please sign in to comment.