Skip to content

Commit dbf3c3c

Browse files
authored
Develop (#27)
- update readme.md badges - update examples - add two step example - minor edits
1 parent f26a767 commit dbf3c3c

23 files changed

Lines changed: 247 additions & 92 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.4.4] - 2023-10-18
10+
- update readme.md badges
11+
- update examples
12+
- add two step example
13+
- minor edits
14+
15+
916
## [0.4.3] - 2022-11-23
1017
- add changelog.md
1118
- add RP2040 to build-CI
1219
- add examples
1320
- add getAverageSubset(start, count) - experimental
1421
- update readme.md
1522

16-
1723
## [0.4.2] - 2021-12-28
1824
- update license
1925
- minor edits

README.md

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
[![Arduino CI](https://github.com/RobTillaart/RunningAverage/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
33
[![Arduino-lint](https://github.com/RobTillaart/RunningAverage/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/RunningAverage/actions/workflows/arduino-lint.yml)
44
[![JSON check](https://github.com/RobTillaart/RunningAverage/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/RunningAverage/actions/workflows/jsoncheck.yml)
5+
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/RunningAverage.svg)](https://github.com/RobTillaart/RunningAverage/issues)
6+
57
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/RunningAverage/blob/master/LICENSE)
68
[![GitHub release](https://img.shields.io/github/release/RobTillaart/RunningAverage.svg?maxAge=3600)](https://github.com/RobTillaart/RunningAverage/releases)
9+
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/RunningAverage.svg)](https://registry.platformio.org/libraries/robtillaart/RunningAverage)
710

811

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

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

2933

34+
#### Related
35+
36+
- https://github.com/RobTillaart/Correlation
37+
- https://github.com/RobTillaart/GST - Golden standard test metrics
38+
- https://github.com/RobTillaart/Histogram
39+
- https://github.com/RobTillaart/RunningAngle
40+
- https://github.com/RobTillaart/RunningAverage
41+
- https://github.com/RobTillaart/RunningMedian
42+
- https://github.com/RobTillaart/statHelpers - combinations & permutations
43+
- https://github.com/RobTillaart/Statistic
44+
45+
3046
## Interface
3147

48+
```cpp
49+
#include "RunningAverage.h"
50+
```
51+
3252
### Constructor
3353

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

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

7092

71-
## Partial
93+
## Partial functions
7294

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

78100

79-
## Last
101+
## Last functions
80102

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

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

98120

@@ -109,15 +131,40 @@ See examples
109131

110132
## Future
111133

112-
#### must
134+
135+
#### Must
136+
113137
- update documentation, explain better
114138

115-
#### should
116-
- add error handling (important?)
139+
#### Should
140+
117141
- check for optimizations.
142+
- divide by count happens often ...
118143
- clear(bool zero = true) to suppress setting all to 0. ?
119144

120-
#### could
121-
- default size for constructor
145+
#### Could
146+
122147
- create a double based derived class? Template class?
148+
- add error handling (important?).
149+
- investigate **modus()** most frequently occurring value.
150+
- difficult with floats ?
151+
- what to do when on two or more values are on par?
152+
153+
#### Wont
154+
155+
- default size for constructor
156+
- unknown what would be a good choice.
157+
- clear(bool zero = true) to suppress setting all to 0. ?
158+
- makes **addValue()** slightly more complex
159+
- could introduce conflicts due to randomness data?
160+
161+
162+
## Support
163+
164+
If you appreciate my libraries, you can support the development and maintenance.
165+
Improve the quality of the libraries by providing issues and Pull Requests, or
166+
donate through PayPal or GitHub sponsors.
167+
168+
Thank you,
169+
123170

RunningAverage.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//
22
// FILE: RunningAverage.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.4.3
5-
// DATE: 2015-July-10
4+
// VERSION: 0.4.4
5+
// DATE: 2011-01-30
66
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
77
// URL: https://github.com/RobTillaart/RunningAverage
88
//
@@ -39,7 +39,7 @@ void RunningAverage::clear()
3939
_max = NAN;
4040
for (uint16_t i = _size; i > 0; )
4141
{
42-
_array[--i] = 0.0; // keeps addValue simpler
42+
_array[--i] = 0.0; // keeps addValue simpler
4343
}
4444
}
4545

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

60-
if (_index == _partial) _index = 0; // faster than %
60+
if (_index == _partial) _index = 0; // faster than %
6161

6262
// handle min max
6363
if (_count == 0) _min = _max = value;
@@ -82,7 +82,7 @@ float RunningAverage::getAverage()
8282
{
8383
_sum += _array[i];
8484
}
85-
return _sum / _count; // multiplication is faster ==> extra admin
85+
return _sum / _count; // multiplication is faster ==> extra admin
8686
}
8787

8888

@@ -246,10 +246,6 @@ void RunningAverage::setPartial(const uint16_t partial)
246246
}
247247

248248

249-
////////////////////////////////////////////////////////////////
250-
//
251-
// 0.4.1 added.
252-
//
253249
float RunningAverage::getAverageLast(uint16_t count)
254250
{
255251
uint16_t cnt = count;
@@ -308,11 +304,6 @@ float RunningAverage::getMaxInBufferLast(uint16_t count)
308304
}
309305

310306

311-
312-
////////////////////////////////////////////////////////////////
313-
//
314-
// Experimental 0.4.3
315-
//
316307
float RunningAverage::getAverageSubset(uint16_t start, uint16_t count)
317308
{
318309
if (_count == 0)
@@ -334,5 +325,5 @@ float RunningAverage::getAverageSubset(uint16_t start, uint16_t count)
334325
}
335326

336327

337-
// -- END OF FILE --
328+
// -- END OF FILE --
338329

RunningAverage.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#pragma once
22
//
33
// FILE: RunningAverage.h
4-
// AUTHOR: Rob.Tillaart@gmail.com
5-
// VERSION: 0.4.3
6-
// DATE: 2016-dec-01
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.4.4
6+
// DATE: 2011-01-30
77
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
88
// URL: https://github.com/RobTillaart/RunningAverage
99

1010

1111
#include "Arduino.h"
1212

1313

14-
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.3"))
14+
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.4"))
1515

1616

1717
class RunningAverage
@@ -26,7 +26,7 @@ class RunningAverage
2626
void fillValue(const float value, const uint16_t number);
2727
float getValue(const uint16_t position);
2828

29-
float getAverage(); // iterates over all elements.
29+
float getAverage(); // iterates over all elements.
3030
float getFastAverage() const; // reuses previous calculated values.
3131

3232
// return statistical characteristics of the running average
@@ -76,5 +76,5 @@ class RunningAverage
7676
};
7777

7878

79-
// -- END OF FILE --
79+
// -- END OF FILE --
8080

examples/fillValue/fillValue.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void setup(void)
1717
{
1818
Serial.begin(115200);
1919
Serial.print("Demo RunningAverage lib - fillValue ");
20-
Serial.print("Version: ");
20+
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
2121
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
2222
delay(10);
2323

@@ -60,5 +60,5 @@ void measure_duration(int n)
6060
}
6161

6262

63-
// -- END OF FILE --
63+
// -- END OF FILE --
6464

examples/ra_300/ra_300.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ int samples = 0;
1414
void setup(void)
1515
{
1616
Serial.begin(115200);
17-
Serial.println("Demo RunningAverage lib");
18-
Serial.print("Version: ");
17+
Serial.println();
18+
Serial.println(__FILE__);
19+
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
1920
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
2021

2122
myRA.clear();
@@ -37,5 +38,5 @@ void loop(void)
3738
}
3839

3940

40-
// -- END OF FILE --
41+
// -- END OF FILE --
4142

examples/ra_300_last/ra_300_last.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ int samples = 0;
1414
void setup(void)
1515
{
1616
Serial.begin(115200);
17-
Serial.println("Demo RunningAverage lib");
18-
Serial.print("Version: ");
17+
Serial.println();
18+
Serial.println(__FILE__);
19+
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
1920
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
2021

2122
myRA.clear();
@@ -50,5 +51,5 @@ void loop(void)
5051
}
5152

5253

53-
// -- END OF FILE --
54+
// -- END OF FILE --
5455

examples/ra_FastAverageTest/ra_FastAverageTest.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ uint32_t start, stop;
2020
void setup(void)
2121
{
2222
Serial.begin(115200);
23-
Serial.print("\nDemo ");
23+
Serial.println();
2424
Serial.println(__FILE__);
25-
Serial.print("Version: ");
25+
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
2626
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
2727
myRA.clear(); // explicitly start clean
2828

@@ -90,5 +90,5 @@ void test(long n)
9090
}
9191

9292

93-
// -- END OF FILE --
93+
// -- END OF FILE --
9494

examples/ra_MinMaxBufferTest/ra_MinMaxBufferTest.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ int samples = 0;
1414
void setup(void)
1515
{
1616
Serial.begin(115200);
17-
Serial.print("\nDemo ");
17+
Serial.println();
1818
Serial.println(__FILE__);
19-
Serial.print("Version: ");
19+
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
2020
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
21-
myRA.clear(); // explicitly start clean
21+
myRA.clear(); // explicitly start clean
2222

2323
Serial.println("\nCNT\tMIN\tMINBUF\tMAX\tMAXBUF");
2424
}
@@ -49,5 +49,5 @@ void loop(void)
4949
}
5050

5151

52-
// -- END OF FILE --
52+
// -- END OF FILE --
5353

examples/ra_MinMaxTest/ra_MinMaxTest.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ int samples = 0;
1414
void setup(void)
1515
{
1616
Serial.begin(115200);
17-
Serial.println("\nDemo runningAverageMinMaxTest");
18-
Serial.print("Version: ");
17+
Serial.println();
18+
Serial.println(__FILE__);
19+
Serial.print("RUNNINGAVERAGE_LIB_VERSION: ");
1920
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
20-
myRA.clear(); // explicitly start clean
21+
myRA.clear(); // explicitly start clean
2122

2223
Serial.println("\nCNT\tMIN\tAVG\tMAX");
2324
}
@@ -46,5 +47,5 @@ void loop(void)
4647
}
4748

4849

49-
// -- END OF FILE --
50+
// -- END OF FILE --
5051

0 commit comments

Comments
 (0)