+ 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
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.04
// VERSION: 0.1.05
// PURPOSE: RunningMedian library for Arduino
//
// HISTORY:
@ -10,6 +10,7 @@
// 0.1.02 - 2012-03-15 added
// 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.05 - 2013-10-18 fixed bug in sort; removes default constructor; dynamic memory
//
// Released to the public domain
//
@ -19,16 +20,24 @@
RunningMedian::RunningMedian(uint8_t 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,
// but using fixed size is easier.
clear();
}
RunningMedian::RunningMedian()
RunningMedian::~RunningMedian()
{
_size = MEDIAN_DEF_SIZE;
clear();
#ifdef RUNNING_MEDIAN_USE_MALLOC
free(_ar);
free(_as);
#endif
}
// resets all counters
void RunningMedian::clear()
{
@ -124,7 +133,7 @@ void RunningMedian::sort()
}
if (m != i)
{
long t = _as[m];
float t = _as[m];
_as[m] = _as[i];
_as[i] = t;
}

View File

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

View File

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

View File

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