2015-09-05 06:14:02 -04:00
|
|
|
//
|
|
|
|
// FILE: ra_FastAverageTest.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:33:55 -05:00
|
|
|
// VERSION: 0.2.0
|
2015-09-05 06:14:02 -04:00
|
|
|
// DATE: 2015-sep-04
|
|
|
|
//
|
|
|
|
// PUPROSE: demo to see if different average algorithm give different result
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "RunningAverage.h"
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
RunningAverage myRA(16);
|
2015-09-05 06:14:02 -04:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
float avg = 0;
|
|
|
|
float favg = 0;
|
|
|
|
float diff = 0;
|
2017-07-26 17:28:55 -04:00
|
|
|
float maxDiff = 0;
|
2015-09-05 06:14:02 -04:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
uint32_t start, stop;
|
|
|
|
|
2015-09-05 06:14:02 -04:00
|
|
|
void setup(void)
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.print("\nDemo ");
|
|
|
|
Serial.println(__FILE__);
|
|
|
|
Serial.print("Version: ");
|
|
|
|
Serial.println(RUNNINGAVERAGE_LIB_VERSION);
|
|
|
|
myRA.clear(); // explicitly start clean
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
measure_duration();
|
2015-09-05 06:14:02 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
void loop()
|
2015-09-05 06:14:02 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
test(1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void measure_duration()
|
|
|
|
{
|
|
|
|
myRA.fillValue(100, 16);
|
|
|
|
start = micros();
|
|
|
|
favg = myRA.getFastAverage();
|
|
|
|
stop = micros();
|
|
|
|
Serial.print("getFastAverage: ");
|
|
|
|
Serial.println(stop - start);
|
|
|
|
delay(10);
|
2015-09-05 06:14:02 -04:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
myRA.fillValue(100, 16);
|
|
|
|
start = micros();
|
|
|
|
favg = myRA.getAverage();
|
|
|
|
stop = micros();
|
|
|
|
Serial.print(" getAverage: ");
|
|
|
|
Serial.println(stop - start);
|
|
|
|
delay(10);
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void test(long n)
|
|
|
|
{
|
|
|
|
Serial.println("\nCNT\tAVG\t\tFASTAVG\t\tDIFF\t\tMAXDIFF");
|
|
|
|
|
|
|
|
for (long i = 0; i < n; i++)
|
2015-09-05 06:14:02 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
long rn = random(0, 1000);
|
|
|
|
myRA.addValue(rn * 0.001);
|
|
|
|
if ( i % 1000 == 0)
|
|
|
|
{
|
|
|
|
// the order of the next two lines is important as getAverage() resets the _sum
|
|
|
|
// used by the getFastAverage();
|
|
|
|
favg = myRA.getFastAverage();
|
|
|
|
avg = myRA.getAverage();
|
|
|
|
diff = abs(avg - favg);
|
|
|
|
if (diff > maxDiff) maxDiff = diff;
|
|
|
|
|
|
|
|
Serial.print(i);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(avg, 7);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(favg, 7);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(diff, 7);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(maxDiff, 7);
|
|
|
|
Serial.println();
|
|
|
|
}
|
2015-09-05 06:14:02 -04:00
|
|
|
}
|
2020-11-27 05:33:55 -05:00
|
|
|
delay(100);
|
2015-09-05 06:14:02 -04:00
|
|
|
}
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|