Skip to content

Commit

Permalink
Update RunningMedian.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
happybono committed Feb 6, 2022
1 parent 651e823 commit 3be5a55
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions FinedustMonitor/RunningMedian.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: RunningMedian.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: 0.3.4
// PURPOSE: RunningMedian library for Arduino
//
// HISTORY:
Expand All @@ -26,9 +26,10 @@
// 0.2.2 2021-01-03 add Arduino-CI + unit tests
// 0.3.0 2021-01-04 malloc memory as default storage
// 0.3.1 2021-01-16 Changed size parameter to 255 max
// 0.3.2 2021-01-21 replaced bubbleSort by insertionSort
// 0.3.2 2021-01-21 replaced bubbleSort by insertionSort
// --> better performance for large arrays.
// 0.3.3 2021-01-22 better insertionSort (+ cleanup test code)
// 0.3.3 2021-01-22 better insertionSort (+ clean up test code)
// 0.3.4 2021-12-28 update library.json, readme, license, minor edits


#include "RunningMedian.h"
Expand Down Expand Up @@ -95,19 +96,19 @@ float RunningMedian::getMedian()
}


float RunningMedian::getQuantile(float q)
float RunningMedian::getQuantile(float quantile)
{
if (_count == 0) return NAN;
if ((q < 0) || (q > 1)) return NAN;

if ((quantile < 0) || (quantile > 1)) return NAN;

if (_sorted == false) sort();
const float id = (_count - 1) * q;
const uint8_t lo = floor(id);
const uint8_t hi = ceil(id);
const float qs = _values[_sortIdx[lo]];
const float h = (id - lo);

const float index = (_count - 1) * quantile;
const uint8_t lo = floor(index);
const uint8_t hi = ceil(index);
const float qs = _values[_sortIdx[lo]];
const float h = (index - lo);

return (1.0 - h) * qs + h * _values[_sortIdx[hi]];
}
Expand Down Expand Up @@ -187,7 +188,7 @@ float RunningMedian::predict(const uint8_t n)

void RunningMedian::sort()
{
// insertSort
// insertSort
for (uint16_t i = 1; i < _count; i++)
{
uint16_t z = i;
Expand All @@ -202,4 +203,6 @@ void RunningMedian::sort()
_sorted = true;
}


// -- END OF FILE --

0 comments on commit 3be5a55

Please sign in to comment.