mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ version 0.1.07
+ added correct median for even element count + fix bug in 0.1.06
This commit is contained in:
parent
ff52d49117
commit
0acddd3f2b
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: RunningMedian.cpp
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// VERSION: 0.1.06
|
||||
// VERSION: 0.1.07
|
||||
// PURPOSE: RunningMedian library for Arduino
|
||||
//
|
||||
// HISTORY:
|
||||
@ -12,6 +12,7 @@
|
||||
// 0.1.04 - 2013-10-17 added getAverage(uint8_t) - kudo's to Sembazuru
|
||||
// 0.1.05 - 2013-10-18 fixed bug in sort; removes default constructor; dynamic memory
|
||||
// 0.1.06 - 2013-10-19 faster sort, dynamic arrays, replaced sorted float array with indirection array
|
||||
// 0.1.07 - 2013-10-19 add correct median if _cnt is even.
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
@ -52,9 +53,8 @@ void RunningMedian::clear()
|
||||
// or overwrites the oldest if full.
|
||||
void RunningMedian::add(float value)
|
||||
{
|
||||
_idx++;
|
||||
_ar[_idx++] = value;
|
||||
if (_idx >= _size) _idx = 0; // wrap around
|
||||
_ar[_idx] = value;
|
||||
if (_cnt < _size) _cnt++;
|
||||
_sorted = false;
|
||||
}
|
||||
@ -64,7 +64,8 @@ float RunningMedian::getMedian()
|
||||
if (_cnt > 0)
|
||||
{
|
||||
if (_sorted == false) sort();
|
||||
return _ar[_p[_cnt/2]];
|
||||
if (_cnt & 0x01) return _ar[_p[_cnt/2]];
|
||||
else return (_ar[_p[_cnt/2]] + _ar[_p[_cnt/2 - 1]]) / 2.0;
|
||||
}
|
||||
return NAN;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
// FILE: RunningMedian.h
|
||||
// AUTHOR: Rob dot Tillaart at gmail dot com
|
||||
// PURPOSE: RunningMedian library for Arduino
|
||||
// VERSION: 0.1.06
|
||||
// VERSION: 0.1.07
|
||||
// URL: http://arduino.cc/playground/Main/RunningMedian
|
||||
// HISTORY: See RunningMedian.cpp
|
||||
//
|
||||
@ -19,7 +19,7 @@
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#define RUNNING_MEDIAN_VERSION "0.1.06"
|
||||
#define RUNNING_MEDIAN_VERSION "0.1.07"
|
||||
|
||||
// prepare for dynamic version
|
||||
// not tested use at own risk :)
|
||||
|
Loading…
Reference in New Issue
Block a user