2015-10-30 12:32:59 -04:00
|
|
|
//
|
|
|
|
// FILE: runningMedianTest1.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// PURPOSE: test functionality
|
|
|
|
// DATE: 2013-10-28
|
2020-11-27 05:33:55 -05:00
|
|
|
// URL: https://github.com/RobTillaart/RunningMedian
|
2021-12-28 04:25:17 -05:00
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
|
|
|
|
#include <RunningMedian.h>
|
|
|
|
|
2021-12-28 04:25:17 -05:00
|
|
|
|
|
|
|
// 50 consecutive samples from Sharp distance sensor model GP2Y0A710K0F while stationary.
|
2015-10-30 12:32:59 -04:00
|
|
|
const int sourceData[] =
|
2021-12-28 04:25:17 -05:00
|
|
|
{
|
2015-10-30 12:32:59 -04:00
|
|
|
300, 299, 296, 343, 307, 304, 303, 305, 300, 340,
|
|
|
|
308, 305, 300, 304, 311, 304, 300, 300, 304, 304,
|
|
|
|
284, 319, 306, 304, 300, 302, 305, 310, 306, 304,
|
|
|
|
308, 300, 299, 304, 300, 305, 307, 303, 326, 311,
|
|
|
|
306, 304, 305, 300, 300, 307, 302, 305, 296, 300
|
|
|
|
};
|
2021-12-28 04:25:17 -05:00
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
const int sourceSize = (sizeof(sourceData)/sizeof(sourceData[0]));
|
|
|
|
|
|
|
|
RunningMedian samples = RunningMedian(sourceSize);
|
|
|
|
|
2021-12-28 04:25:17 -05:00
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
2021-12-28 04:25:17 -05:00
|
|
|
while (!Serial); // Wait for serial port to connect. Needed for Leonardo + MKR1010.
|
2015-10-30 12:32:59 -04:00
|
|
|
delay(1000); // Simply to allow time for the ERW versions of the IDE time to automagically open the Serial Monitor. 1 second chosen arbitrarily.
|
|
|
|
Serial.print(F("Running Median Version: "));
|
|
|
|
Serial.println(RUNNING_MEDIAN_VERSION);
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
#ifdef RUNNING_MEDIAN_USE_MALLOC
|
|
|
|
Serial.println(F("Dynamic version using malloc() enabled"));
|
|
|
|
#else
|
|
|
|
Serial.print(F("Static version, will always allocate an array of "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.print(MEDIAN_MAX_SIZE, DEC);
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.println(F(" floats."));
|
|
|
|
#endif
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
test1();
|
|
|
|
|
|
|
|
Serial.println("\ndone..\n");
|
2015-10-30 12:32:59 -04:00
|
|
|
}
|
|
|
|
|
2021-12-28 04:25:17 -05:00
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-28 04:25:17 -05:00
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
void test1()
|
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
uint32_t start = 0;
|
|
|
|
uint32_t stop = 0;
|
|
|
|
float result = 0;
|
2015-10-30 12:32:59 -04:00
|
|
|
|
|
|
|
Serial.print(F("Requested median array size = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(sourceSize);
|
|
|
|
Serial.print(F(" Actual allocated size = "));
|
|
|
|
Serial.println(samples.getSize());
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.println();
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
// 50 iterations !!
|
|
|
|
for (uint8_t i = 0; i <= (sourceSize - 1); i++)
|
2015-10-30 12:32:59 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.print(F("Loop number : "));
|
|
|
|
Serial.println(i + 1);
|
2015-10-30 12:32:59 -04:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
start = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
samples.add(sourceData[i]);
|
2020-11-27 05:33:55 -05:00
|
|
|
stop = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F("Time to add the next element to the array = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(stop - start);
|
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
|
|
|
|
Serial.println(F("Cumulative source data added:"));
|
|
|
|
Serial.print(F(" "));
|
2020-11-27 05:33:55 -05:00
|
|
|
for (uint8_t j = 0; j <= i; j++)
|
2015-10-30 12:32:59 -04:00
|
|
|
{
|
|
|
|
Serial.print(sourceData[j]);
|
|
|
|
Serial.print(F(" "));
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.println(F("Unsorted accumulated array:"));
|
|
|
|
Serial.print(F(" "));
|
2020-11-27 05:33:55 -05:00
|
|
|
for (uint8_t j = 0; j < samples.getCount(); j++)
|
2015-10-30 12:32:59 -04:00
|
|
|
{
|
|
|
|
Serial.print(samples.getElement(j));
|
|
|
|
Serial.print(F(" "));
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
start = micros();
|
|
|
|
result = samples.getSortedElement(0);
|
|
|
|
stop = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F("Time to sort array and return element number zero = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(stop - start);
|
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
|
|
|
|
Serial.println(F("Sorted accumulated array:"));
|
|
|
|
Serial.print(F(" "));
|
2020-11-27 05:33:55 -05:00
|
|
|
for (uint8_t j = 0; j < samples.getCount(); j++)
|
2015-10-30 12:32:59 -04:00
|
|
|
{
|
|
|
|
Serial.print(samples.getSortedElement(j));
|
|
|
|
Serial.print(F(" "));
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
|
|
|
|
start = micros();
|
|
|
|
result = samples.getMedian();
|
|
|
|
stop = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F("getMedian() result = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(result);
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F("Time to execute getMedian() = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(stop - start);
|
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
|
2020-11-27 05:33:55 -05:00
|
|
|
start = micros();
|
|
|
|
result = samples.getAverage();
|
|
|
|
stop = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F("getAverage() result = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(result);
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F("Time to execute getAverage() = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(stop - start);
|
|
|
|
|
2015-10-30 12:32:59 -04:00
|
|
|
|
|
|
|
Serial.println(F("getAverage(x) results where:"));
|
2020-11-27 05:33:55 -05:00
|
|
|
for (uint8_t j = 1; j <= samples.getCount(); j++)
|
2015-10-30 12:32:59 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
start = micros();
|
|
|
|
result = samples.getAverage(j);
|
|
|
|
stop = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F(" x = "));
|
|
|
|
Serial.print(j);
|
|
|
|
Serial.print(F(" => "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.print(result);
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F(" Time to execute = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(stop - start);
|
2015-10-30 12:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println(F("predict(x) results where:"));
|
2020-11-27 05:33:55 -05:00
|
|
|
for (uint8_t j = 1; j <= (samples.getCount() / 2); j++)
|
2015-10-30 12:32:59 -04:00
|
|
|
{
|
2020-11-27 05:33:55 -05:00
|
|
|
start = micros();
|
|
|
|
result = samples.predict(j);
|
|
|
|
stop = micros();
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F(" x = "));
|
|
|
|
Serial.print(j);
|
|
|
|
Serial.print(F(" => "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.print(result);
|
2015-10-30 12:32:59 -04:00
|
|
|
Serial.print(F(" Time to execute = "));
|
2020-11-27 05:33:55 -05:00
|
|
|
Serial.println(stop - start);
|
2015-10-30 12:32:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println();
|
|
|
|
Serial.println();
|
|
|
|
}
|
2020-11-27 05:33:55 -05:00
|
|
|
}
|
|
|
|
|
2021-12-28 04:25:17 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|