RunningMedian.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // FILE: RunningMedian.cpp
  3. // AUTHOR: Rob Tillaart
  4. // VERSION: 0.3.10
  5. // PURPOSE: RunningMedian library for Arduino
  6. #include "RunningMedian.h"
  7. RunningMedian::RunningMedian(const uint8_t size)
  8. {
  9. _size = size;
  10. if (_size < MEDIAN_MIN_SIZE) _size = MEDIAN_MIN_SIZE;
  11. #if !RUNNING_MEDIAN_USE_MALLOC
  12. if (_size > MEDIAN_MAX_SIZE) _size = MEDIAN_MAX_SIZE;
  13. #endif
  14. #if RUNNING_MEDIAN_USE_MALLOC
  15. _values = (float *) malloc(_size * sizeof(float));
  16. _sortIdx = (uint8_t *) malloc(_size * sizeof(uint8_t));
  17. #endif
  18. clear();
  19. }
  20. RunningMedian::~RunningMedian()
  21. {
  22. #if RUNNING_MEDIAN_USE_MALLOC
  23. free(_values);
  24. free(_sortIdx);
  25. #endif
  26. }
  27. // resets all internal counters
  28. void RunningMedian::clear()
  29. {
  30. _count = 0;
  31. _index = 0;
  32. _sorted = false;
  33. for (uint8_t i = 0; i < _size; i++)
  34. {
  35. _sortIdx[i] = i;
  36. }
  37. }
  38. // adds a new value to the data-set
  39. // or overwrites the oldest if full.
  40. void RunningMedian::add(float value)
  41. {
  42. _values[_index++] = value;
  43. if (_index >= _size) _index = 0; // wrap around
  44. if (_count < _size) _count++;
  45. _sorted = false;
  46. }
  47. float RunningMedian::getMedian()
  48. {
  49. if (_count == 0) return NAN;
  50. if (_sorted == false) sort();
  51. if (_count & 0x01) // is it odd sized?
  52. {
  53. return _values[_sortIdx[_count / 2]];
  54. }
  55. return (_values[_sortIdx[_count / 2]] + _values[_sortIdx[_count / 2 - 1]]) / 2;
  56. }
  57. float RunningMedian::getQuantile(float quantile)
  58. {
  59. if (_count == 0) return NAN;
  60. if ((quantile < 0) || (quantile > 1)) return NAN;
  61. if (_sorted == false) sort();
  62. const float index = (_count - 1) * quantile;
  63. const uint8_t lo = floor(index);
  64. const uint8_t hi = ceil(index);
  65. const float qs = _values[_sortIdx[lo]];
  66. const float h = (index - lo);
  67. return (1.0 - h) * qs + h * _values[_sortIdx[hi]];
  68. }
  69. float RunningMedian::getAverage()
  70. {
  71. if (_count == 0) return NAN;
  72. float sum = 0;
  73. for (uint8_t i = 0; i < _count; i++)
  74. {
  75. sum += _values[i];
  76. }
  77. return sum / _count;
  78. }
  79. float RunningMedian::getAverage(uint8_t nMedians)
  80. {
  81. if ((_count == 0) || (nMedians == 0)) return NAN;
  82. // when filling the array for first time
  83. if (_count < nMedians) nMedians = _count;
  84. uint8_t start = ((_count - nMedians) / 2);
  85. uint8_t stop = start + nMedians;
  86. if (_sorted == false) sort();
  87. float sum = 0;
  88. for (uint8_t i = start; i < stop; i++)
  89. {
  90. sum += _values[_sortIdx[i]];
  91. }
  92. return sum / nMedians;
  93. }
  94. // nMedians is the spread, or the middle N
  95. // this version compensated for bias #22
  96. float RunningMedian::getMedianAverage(uint8_t nMedians)
  97. {
  98. // handle special cases.
  99. if ((_count == 0) || (nMedians == 0)) return NAN;
  100. if (_count == 1) return _values[0];
  101. if (_count == 2) return (_values[0] + _values[1]) * 0.5;
  102. // nMedians can not be larger than current nr of elements.
  103. if (_count <= nMedians) return getAverage();
  104. // _count is at least 3 from here
  105. // Eliminate the bias when the nMedians would fall slightly
  106. // to the left or right of the centre.
  107. // If count and nMedians are not both odd or both even reduce
  108. // the spread by 1 to make them the same.
  109. // If nMedians becomes 0 correct this. to 2.
  110. if ((_count & 0x01) != (nMedians & 0x01))
  111. {
  112. --nMedians;
  113. // nmedians can not become 0
  114. if (nMedians == 0) nMedians = 2;
  115. }
  116. uint8_t start = (_count - nMedians) / 2;
  117. uint8_t stop = start + nMedians;
  118. if (_sorted == false) sort();
  119. float sum = 0;
  120. for (uint8_t i = start; i < stop; i++)
  121. {
  122. sum += _values[_sortIdx[i]];
  123. }
  124. return sum / nMedians;
  125. }
  126. float RunningMedian::getElement(const uint8_t n)
  127. {
  128. if ((_count == 0) || (n >= _count)) return NAN;
  129. uint8_t pos = _index + n;
  130. if (pos >= _count) // faster than %
  131. {
  132. pos -= _count;
  133. }
  134. return _values[pos];
  135. }
  136. float RunningMedian::getSortedElement(const uint8_t n)
  137. {
  138. if ((_count == 0) || (n >= _count)) return NAN;
  139. if (_sorted == false) sort();
  140. return _values[_sortIdx[n]];
  141. }
  142. // n can be max <= half the (filled) size
  143. float RunningMedian::predict(const uint8_t n)
  144. {
  145. uint8_t mid = _count / 2;
  146. if ((_count == 0) || (n >= mid)) return NAN;
  147. float med = getMedian(); // takes care of sorting !
  148. if (_count & 0x01) // odd # elements
  149. {
  150. return max(med - _values[_sortIdx[mid - n]], _values[_sortIdx[mid + n]] - med);
  151. }
  152. // even # elements
  153. float f1 = (_values[_sortIdx[mid - n]] + _values[_sortIdx[mid - n - 1]]) / 2;
  154. float f2 = (_values[_sortIdx[mid + n]] + _values[_sortIdx[mid + n - 1]]) / 2;
  155. return max(med - f1, f2 - med) / 2;
  156. }
  157. void RunningMedian::setSearchMode(uint8_t searchMode)
  158. {
  159. if (searchMode == 1) _searchMode = 1;
  160. else _searchMode = 0;
  161. }
  162. uint8_t RunningMedian::getSearchMode()
  163. {
  164. return _searchMode;
  165. }
  166. ////////////////////////////////////////////////////////////
  167. //
  168. // PRIVATE
  169. //
  170. // insertion sort - _searchMode = linear or binary.
  171. void RunningMedian::sort()
  172. {
  173. uint16_t lo = 0;
  174. uint16_t hi = 0;
  175. uint16_t mi = 0;
  176. uint16_t temp = 0;
  177. for (uint16_t i = 1; i < _count; i++)
  178. {
  179. temp = _sortIdx[i];
  180. float f = _values[temp];
  181. // handle special case f is smaller than all elements first.
  182. // only one compare needed, improves linear search too.
  183. if (f <= _values[_sortIdx[0]])
  184. {
  185. hi = 0;
  186. }
  187. else
  188. {
  189. if (_searchMode == 0)
  190. {
  191. hi = i;
  192. // find insertion point with linear search
  193. while ((hi > 0) && (f < _values[_sortIdx[hi - 1]]))
  194. {
  195. hi--;
  196. }
  197. }
  198. else if (_searchMode == 1)
  199. {
  200. // find insertion point with binary search
  201. lo = 0;
  202. hi = i;
  203. // be aware there might be duplicates
  204. while (hi - lo > 1)
  205. {
  206. mi = (lo + hi) / 2;
  207. if (f < _values[_sortIdx[mi]])
  208. {
  209. hi = mi;
  210. }
  211. else
  212. {
  213. lo = mi;
  214. }
  215. }
  216. }
  217. }
  218. // move elements to make space
  219. uint16_t k = i;
  220. while (k > hi)
  221. {
  222. _sortIdx[k] = _sortIdx[k - 1];
  223. k--;
  224. }
  225. // insert at right spot.
  226. _sortIdx[k] = temp;
  227. }
  228. _sorted = true;
  229. // // verify sorted
  230. // for (int i = 0; i < _count; i++)
  231. // {
  232. // if (i%5 == 0) Serial.println();
  233. // Serial.print("\t");
  234. // Serial.print(_values[_sortIdx[i]]);
  235. // }
  236. // Serial.println("\n");
  237. }
  238. /*
  239. split version of pre-0.3.7 sort - bit faster
  240. void RunningMedian::sort()
  241. {
  242. // insertSort
  243. for (uint16_t i = 1; i < _count; i++)
  244. {
  245. uint16_t hi = i;
  246. uint16_t temp = _sortIdx[hi];
  247. float f = _values[temp];
  248. while ((hi > 0) && (f < _values[_sortIdx[hi - 1]]))
  249. {
  250. hi--;
  251. }
  252. // move elements to make space
  253. uint16_t k = i;
  254. while (k > hi)
  255. {
  256. _sortIdx[k] = _sortIdx[k - 1];
  257. k--;
  258. }
  259. // insert at right spot.
  260. _sortIdx[k] = temp;
  261. }
  262. _sorted = true;
  263. // // verify sorted
  264. // for (int i = 0; i < _count; i++)
  265. // {
  266. // if (i%5 == 0) Serial.println();
  267. // Serial.print("\t");
  268. // Serial.print(_values[_sortIdx[i]]);
  269. // }
  270. // Serial.println("\n");
  271. }
  272. */
  273. /*
  274. // straightforward insertion sort - PRE-0.3.7
  275. void RunningMedian::sort()
  276. {
  277. for (uint16_t i = 1; i < _count; i++)
  278. {
  279. uint16_t z = i;
  280. uint16_t temp = _sortIdx[z];
  281. while ((z > 0) && (_values[temp] < _values[_sortIdx[z - 1]]))
  282. {
  283. _sortIdx[z] = _sortIdx[z - 1];
  284. z--;
  285. }
  286. _sortIdx[z] = temp;
  287. }
  288. _sorted = true;
  289. // // verify sorted
  290. // for (int i = 0; i < _count; i++)
  291. // {
  292. // if (i%5 == 0) Serial.println();
  293. // Serial.print("\t");
  294. // Serial.print(_values[_sortIdx[i]]);
  295. // }
  296. // Serial.println("\n");
  297. }
  298. */
  299. // -- END OF FILE --