Skip to content

Commit 3c2d7fa

Browse files
committed
FastMovingAverage treat length 0 as 0x7FFFFFFF
1 parent f115e12 commit 3c2d7fa

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

Firmware/FFBoard/Inc/FastAvg.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,20 @@ class FastAvg {
5454
};
5555

5656
/**
57-
* Calculates a moving average of unknown length
57+
* Calculates a moving average of variable length
5858
* Can periodically get and reset a value to average for an unknown amount of data points
5959
* If len != 0 calculates an exponential moving average with length len.
60-
* If len = 0 length equals the current amount of samples.
60+
* If len = 0 length equals the current amount of samples up to 0x7FFFFFFF.
6161
*/
6262
template <class T>
6363
class FastMovingAverage{
6464
public:
65-
FastMovingAverage(int32_t len = 0) : fixedLen(len), count(0){};
65+
FastMovingAverage(int32_t len = 0) : fixedLen(len > 0 ? len : INT32_MAX), count(0){};
6666
~FastMovingAverage(){};
6767

6868
void clear(){
6969
curAvg = 0;
70-
if(!fixedLen)
71-
count=0;
70+
count=0;
7271
}
7372
/**
7473
* Gets current average and clears current average and counter
@@ -89,7 +88,7 @@ class FastMovingAverage{
8988
* Adds a value and returns current average
9089
*/
9190
T addValue(T v){
92-
if(!fixedLen || count < fixedLen)
91+
if(count < fixedLen)
9392
count++;
9493
curAvg += (v - curAvg)/count;
9594
return curAvg;

0 commit comments

Comments
 (0)