-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exponential moving average (#17)
* test(test): ✅ add build tests on github workflows #13 * test(test): ✅ fix build test arduino uno env #13 * feat(DataTomeCumulative): ✨ add DataTomeCumulative class add DataTomeCumulative to calculate the cumulative average #9 * feat(DataTomeExpAvg): ✨ add DataTomeExpAvg with unit tests #9 #19
- Loading branch information
1 parent
5ca50c8
commit c0692b1
Showing
18 changed files
with
280 additions
and
104 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
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 |
---|---|---|
|
@@ -58,6 +58,8 @@ | |
"test", | ||
"readme", | ||
"library_metadata", | ||
"DataTome" | ||
"DataTome", | ||
"DataTomeCumulative", | ||
"DataTomeExpAvg" | ||
] | ||
} |
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,59 @@ | ||
// Include lib: | ||
#include <Arduino.h> | ||
#include <DataTome.h> | ||
|
||
// Create an Arithmetic Moving Average object of unsigned int type, | ||
// 10 in size | ||
DataTomeMvAvg<int, long int> test(10); | ||
DataTomeAnalysis<int, long int> test2(10); | ||
DataTomeCumulative<double> test3; | ||
DataTomeExpAvg<double> test4; | ||
|
||
// This variable just generates input for average test | ||
int delta_x = 0; | ||
|
||
void setup() { | ||
// Initialize serial interface | ||
Serial.begin(9600); | ||
} | ||
|
||
void loop() { | ||
// Pushes the input in the moving average object | ||
test.push(delta_x); | ||
test2.push(delta_x); | ||
test3.push(delta_x); | ||
test4.push(delta_x); | ||
|
||
// Generates the next input | ||
delta_x += 3; | ||
if (delta_x > 1000) delta_x = 0; | ||
|
||
// Prints each value stored in the moving average | ||
for (uint8_t i = 0; i < test.size(); i++) { | ||
Serial.print(test[i]); | ||
Serial.print(" "); | ||
} | ||
// Prints the result of the average | ||
Serial.print("= "); | ||
Serial.print(test.get()); | ||
// Prints the value stored in the first and last indexes | ||
Serial.print(" | f: "); | ||
Serial.print(test.front()); | ||
Serial.print(" b: "); | ||
Serial.println(test.back()); | ||
|
||
Serial.print("Analysis: "); | ||
Serial.print(test2.mean()); | ||
Serial.print(" | Std: "); | ||
Serial.print(test2.std()); | ||
Serial.print(" | Median: "); | ||
Serial.println(test2.median()); | ||
|
||
Serial.print("Cumulative: "); | ||
Serial.println(test3.get()); | ||
|
||
Serial.print(" | ExpAvg: "); | ||
Serial.println(test4.get()); | ||
|
||
delay(1000); | ||
} |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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,38 @@ | ||
/*************************************************************** | ||
DataTomeCumulative.h | ||
Created by Alexandre Hiroyuki Yamauchi, September 23, 2024. | ||
***************************************************************/ | ||
|
||
#ifndef DATA_TOME_CUMULATIVE_H | ||
#define DATA_TOME_CUMULATIVE_H | ||
|
||
#include <limits.h> | ||
|
||
// RECOMMENDED: use double type for TypeOfSum | ||
// WARNING: using this class with integer types may result in a loss of | ||
// precision due to cumulative rounding of integer divisions. | ||
template <typename TypeOfSum> | ||
class DataTomeCumulative { | ||
protected: | ||
TypeOfSum _cumulative_average; | ||
unsigned long int _count; | ||
|
||
public: | ||
DataTomeCumulative() : _cumulative_average(0), _count(0) {} | ||
|
||
DataTomeCumulative<TypeOfSum> &push(TypeOfSum input) { | ||
if (_count >= ULONG_MAX) { | ||
_cumulative_average = 0; | ||
_count = 0; | ||
} | ||
_count++; | ||
|
||
_cumulative_average += (input - _cumulative_average) / (_count); | ||
|
||
return *this; | ||
} | ||
|
||
TypeOfSum get() { return _cumulative_average; } | ||
}; | ||
|
||
#endif // DATA_TOME_CUMULATIVE_H |
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,39 @@ | ||
/*************************************************************** | ||
DataTomeExpAvg.h | ||
Created by Alexandre Hiroyuki Yamauchi, August 19, 2024. | ||
***************************************************************/ | ||
|
||
#ifndef DATA_TOME_EXP_AVG_H | ||
#define DATA_TOME_EXP_AVG_H | ||
|
||
#include <limits.h> | ||
|
||
// RECOMMENDED: use double type for TypeOfSum | ||
// WARNING: using this class with integer types may result in a loss of | ||
// precision due to cumulative rounding of integer divisions. | ||
template <typename TypeOfSum> | ||
class DataTomeExpAvg { | ||
protected: | ||
TypeOfSum _exp_avg; | ||
unsigned long int _count; | ||
|
||
public: | ||
DataTomeExpAvg() : _exp_avg(0), _count(0) {} | ||
|
||
DataTomeExpAvg<TypeOfSum> &push(TypeOfSum input) { | ||
if (_count >= ULONG_MAX) { | ||
_exp_avg = 0; | ||
_count = 0; | ||
} | ||
_count++; | ||
|
||
TypeOfSum multiplier = 2 / (_count); | ||
_exp_avg = (input * multiplier) + (_exp_avg * (1 - multiplier)); | ||
|
||
return *this; | ||
} | ||
|
||
TypeOfSum get() { return _exp_avg; } | ||
}; | ||
|
||
#endif // DATA_TOME_EXP_AVG_H |
Oops, something went wrong.