+ added comments in the header file

+ removed the default constructor, user should be explicit define size
+ created a destructor
+ added support for dynamic allocated internal buffers (not tested)
+ fixed bug in sort routine (swap)
+ patched testsketches
This commit is contained in:
Rob Tillaart 2013-10-19 00:26:50 +02:00
parent 287d2436f4
commit 79c57bc888
4 changed files with 47 additions and 25 deletions

View File

@ -1,7 +1,7 @@
// //
// FILE: RunningMedian.cpp // FILE: RunningMedian.cpp
// AUTHOR: Rob dot Tillaart at gmail dot com // AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.04 // VERSION: 0.1.05
// PURPOSE: RunningMedian library for Arduino // PURPOSE: RunningMedian library for Arduino
// //
// HISTORY: // HISTORY:
@ -10,6 +10,7 @@
// 0.1.02 - 2012-03-15 added // 0.1.02 - 2012-03-15 added
// 0.1.03 - 2013-09-30 added _sorted flag, minor refactor // 0.1.03 - 2013-09-30 added _sorted flag, minor refactor
// 0.1.04 - 2013-10-17 added getAverage(uint8_t) - kudo's to Sembazuru // 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
// //
// Released to the public domain // Released to the public domain
// //
@ -19,16 +20,24 @@
RunningMedian::RunningMedian(uint8_t size) RunningMedian::RunningMedian(uint8_t size)
{ {
_size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE); _size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE);
#ifdef RUNNING_MEDIAN_USE_MALLOC
_ar = (float *) malloc(_size * sizeof(float));
_as = (float *) malloc(_size * sizeof(float));
#endif
// array's could be allocated by malloc here, // array's could be allocated by malloc here,
// but using fixed size is easier. // but using fixed size is easier.
clear(); clear();
} }
RunningMedian::RunningMedian() RunningMedian::~RunningMedian()
{ {
_size = MEDIAN_DEF_SIZE; #ifdef RUNNING_MEDIAN_USE_MALLOC
clear(); free(_ar);
free(_as);
#endif
} }
// resets all counters // resets all counters
void RunningMedian::clear() void RunningMedian::clear()
{ {
@ -124,7 +133,7 @@ void RunningMedian::sort()
} }
if (m != i) if (m != i)
{ {
long t = _as[m]; float t = _as[m];
_as[m] = _as[i]; _as[m] = _as[i];
_as[i] = t; _as[i] = t;
} }

View File

@ -4,7 +4,7 @@
// FILE: RunningMedian.h // FILE: RunningMedian.h
// AUTHOR: Rob dot Tillaart at gmail dot com // AUTHOR: Rob dot Tillaart at gmail dot com
// PURPOSE: RunningMedian library for Arduino // PURPOSE: RunningMedian library for Arduino
// VERSION: 0.1.04 // VERSION: 0.1.05
// URL: http://arduino.cc/playground/Main/RunningMedian // URL: http://arduino.cc/playground/Main/RunningMedian
// HISTORY: See RunningMedian.cpp // HISTORY: See RunningMedian.cpp
// //
@ -19,34 +19,41 @@
#include <inttypes.h> #include <inttypes.h>
#define RUNNING_MEDIAN_VERSION "0.1.04" #define RUNNING_MEDIAN_VERSION "0.1.05"
// should at least be 5 to be practical // prepare for dynamic version
#define MEDIAN_MIN_SIZE 1 // not tested use at own risk :)
#define MEDIAN_MAX_SIZE 19 // #define RUNNING_MEDIAN_USE_MALLOC
#define MEDIAN_DEF_SIZE 5
// conditional compile to minimize lib // conditional compile to minimize lib
// by removeing a lot of functions.
#define RUNNING_MEDIAN_ALL #define RUNNING_MEDIAN_ALL
// should at least be 5 to be practical
// odd size results in a 'real' middle element.
#define MEDIAN_MIN_SIZE 1
#define MEDIAN_MAX_SIZE 19 // can be adjusted if needed
class RunningMedian class RunningMedian
{ {
public: public:
RunningMedian(uint8_t); RunningMedian(uint8_t size); // # elements in the internal buffer
RunningMedian(); ~RunningMedian(); // destructor
void clear(); void clear(); // resets internal buffer and var
void add(float); void add(float value); // adds a new value to internal buffer, optionally replacing the oldest element.
float getMedian(); float getMedian(); // returns the median == middle element
#ifdef RUNNING_MEDIAN_ALL #ifdef RUNNING_MEDIAN_ALL
float getAverage(); float getAverage(); // returns average of the values in the internal buffer
float getAverage(uint8_t); float getAverage(uint8_t nMedian); // returns average of the middle nMedian values, removes noise from outliers
float getHighest(); float getHighest(); // returns highest element
float getLowest(); float getLowest(); // return lowest element
uint8_t getSize(); uint8_t getSize(); // returns size of internal buffer
uint8_t getCount(); uint8_t getCount(); // returns current used elements, getCount() <= getSize()
#endif #endif
protected: protected:
@ -54,8 +61,14 @@ protected:
uint8_t _size; uint8_t _size;
uint8_t _cnt; uint8_t _cnt;
uint8_t _idx; uint8_t _idx;
#ifdef RUNNING_MEDIAN_USE_MALLOC
float * _ar;
float * _as;
#else
float _ar[MEDIAN_MAX_SIZE]; float _ar[MEDIAN_MAX_SIZE];
float _as[MEDIAN_MAX_SIZE]; float _as[MEDIAN_MAX_SIZE];
#endif
void sort(); void sort();
}; };

View File

@ -1,7 +1,7 @@
#include <RunningMedian.h> #include <RunningMedian.h>
RunningMedian samples = RunningMedian(); RunningMedian samples = RunningMedian(5);
RunningMedian samples2 = RunningMedian(9); RunningMedian samples2 = RunningMedian(9);
void setup() void setup()

View File

@ -1,7 +1,7 @@
// //
// FILE: RunningMedian2.ino // FILE: RunningMedian2.ino
// AUTHOR: Rob Tillaart ( kudos to Sembazuru) // AUTHOR: Rob Tillaart ( kudos to Sembazuru)
// VERSION: 0.1.00 // VERSION: 0.1.01
// PURPOSE: demo // PURPOSE: demo
// DATE: 2013-10-17 // DATE: 2013-10-17
// URL: // URL:
@ -11,7 +11,7 @@
#include "RunningMedian.h" #include "RunningMedian.h"
RunningMedian samples = RunningMedian(); RunningMedian samples = RunningMedian(15);
long count = 0; long count = 0;