|
|
5 месяцев назад | |
|---|---|---|
| .github | 1 год назад | |
| examples | 5 месяцев назад | |
| test | 3 лет назад | |
| .arduino-ci.yml | 3 лет назад | |
| CHANGELOG.md | 5 месяцев назад | |
| LICENSE | 5 месяцев назад | |
| README.md | 5 месяцев назад | |
| RunningMedian.cpp | 5 месяцев назад | |
| RunningMedian.h | 5 месяцев назад | |
| keywords.txt | 2 лет назад | |
| library.json | 5 месяцев назад | |
| library.properties | 5 месяцев назад |
Arduino library to determine the running median by means of a circular buffer.
Running Median looks like a running average with a small but important twist. Running average averages the last N samples while the running median takes the last N samples, sort them and take the middle one, or the average of the middle two in case the internal buffer size is even.
Important differences between running average and running median:
The maximum size of the internal buffer is defined by MEDIAN_MAX_SIZE and is set to 255 (since version 0.3.1). The memory allocated currently is in the order of 5 bytes per element plus some overhead, so 255 elements take ~1300 bytes. For an UNO this is quite a bit.
With larger sizes the performance penalty to keep the internal array sorted is large. For most applications a value much lower e.g. 19 is working well, and is performance wise O(100x) faster in sorting than 255 elements.
There are several options that can be configured via defines at compile time, those being:
For printing floats in scientific or engineering format
https://github.com/RobTillaart/printHelpers
#include "RunningMedian.h"
getAverage(nMedian) and getMedianAverage(uint8_t nMedian) differ. When nMedian is odd and count is even or vice versa, the middle N are not perfectly in the middle. By auto-adjusting nMedian (-1 +1) this balance is restored.
Assume an internal size of 7 elements [0..6] then
The example RunningMedian_getMedianAverage.ino shows the difference.
The implementation of getMedianAverage(uint8_t nMedian) is experimental and might change in the future. Idea is taking top and bottom elements only for 50% if needed, however that implies at least 2 extra float multiplications.
It is possible that the name getMedianAverage(uint8_t nMedian) will change in the future to be more descriptive.
Since 0.3.7 the internal sort has been optimized. It is now possible to select between LINEAR (=0) and BINARY (=1) insertion sort. Pre-0.3.7 used linear insertion sort, and the new linear version is slightly optimized. For larger internal arrays the performance gain of BINARY mode is substantial.
| searchMode | value | notes |
|---|---|---|
| LINEAR | 0 | fastest for smaller internal buffers (default) |
| BINARY | 1 | faster for larger internal buffers |
Depends on the board / clock used where the methods are equally fast.
Give it a try, and let me know your experiences.
If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.
Thank you,