-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ROLLING_AVERAGE_FALLOFF to the drop off types.
- Loading branch information
Showing
4 changed files
with
221 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#define MAX_ROLLING_AVERAGE_WINDOW 25 | ||
|
||
class RollingAverage { | ||
public: | ||
RollingAverage() { | ||
// Initialize values array | ||
resize(MAX_ROLLING_AVERAGE_WINDOW); | ||
} | ||
|
||
void resize(uint16_t size) { | ||
windowSize = size; | ||
index = 0; | ||
count = 0; | ||
sum = 0; | ||
for (int i = 0; i < windowSize; i++) { | ||
values[i] = 0.0; | ||
} | ||
} | ||
|
||
float addValue(float value) { | ||
// Subtract the oldest value from the sum | ||
sum -= values[index]; | ||
|
||
// Add the new value to the array and sum | ||
values[index] = value; | ||
sum += value; | ||
|
||
// Move to the next index | ||
index = (index + 1) % windowSize; | ||
|
||
// Keep track of the number of values added | ||
if (count < windowSize) { | ||
count++; | ||
} | ||
|
||
return getAverage(); | ||
} | ||
|
||
float getAverage() { | ||
if(count == 0) { | ||
return 1; | ||
} | ||
// Calculate the average | ||
return sum / count; | ||
} | ||
|
||
private: | ||
uint16_t windowSize = MAX_ROLLING_AVERAGE_WINDOW; | ||
uint16_t index = 0; | ||
uint16_t count = 0; | ||
float values[MAX_ROLLING_AVERAGE_WINDOW]; | ||
float sum = 0.0; | ||
}; |
Oops, something went wrong.