mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ refactor constructor + const where possible
This commit is contained in:
parent
8eb506f62e
commit
6a5296d3f1
@ -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.11
|
// VERSION: 0.1.12
|
||||||
// PURPOSE: RunningMedian library for Arduino
|
// PURPOSE: RunningMedian library for Arduino
|
||||||
//
|
//
|
||||||
// HISTORY:
|
// HISTORY:
|
||||||
@ -17,13 +17,14 @@
|
|||||||
// 0.1.09 - 2014-11-25 float to double (support ARM)
|
// 0.1.09 - 2014-11-25 float to double (support ARM)
|
||||||
// 0.1.10 - 2015-03-07 fix clear
|
// 0.1.10 - 2015-03-07 fix clear
|
||||||
// 0.1.11 - 2015-03-29 undo 0.1.10 fix clear
|
// 0.1.11 - 2015-03-29 undo 0.1.10 fix clear
|
||||||
|
// 0.1.12 - 2015-07-12 refactor constructor + const
|
||||||
//
|
//
|
||||||
// Released to the public domain
|
// Released to the public domain
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "RunningMedian.h"
|
#include "RunningMedian.h"
|
||||||
|
|
||||||
RunningMedian::RunningMedian(uint8_t size)
|
RunningMedian::RunningMedian(const uint8_t size)
|
||||||
{
|
{
|
||||||
_size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE);
|
_size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE);
|
||||||
|
|
||||||
@ -74,9 +75,15 @@ double RunningMedian::getMedian()
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RUNNING_MEDIAN_ALL
|
#ifdef RUNNING_MEDIAN_ALL
|
||||||
double RunningMedian::getHighest() { return getSortedElement(_cnt-1); }
|
double RunningMedian::getHighest()
|
||||||
|
{
|
||||||
|
return getSortedElement(_cnt - 1);
|
||||||
|
}
|
||||||
|
|
||||||
double RunningMedian::getLowest() { return getSortedElement(0); }
|
double RunningMedian::getLowest()
|
||||||
|
{
|
||||||
|
return getSortedElement(0);
|
||||||
|
}
|
||||||
|
|
||||||
double RunningMedian::getAverage()
|
double RunningMedian::getAverage()
|
||||||
{
|
{
|
||||||
@ -105,7 +112,7 @@ double RunningMedian::getAverage(uint8_t nMedians)
|
|||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
double RunningMedian::getElement(uint8_t n)
|
double RunningMedian::getElement(const uint8_t n)
|
||||||
{
|
{
|
||||||
if ((_cnt > 0) && (n < _cnt))
|
if ((_cnt > 0) && (n < _cnt))
|
||||||
{
|
{
|
||||||
@ -114,7 +121,7 @@ double RunningMedian::getElement(uint8_t n)
|
|||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
double RunningMedian::getSortedElement(uint8_t n)
|
double RunningMedian::getSortedElement(const uint8_t n)
|
||||||
{
|
{
|
||||||
if ((_cnt > 0) && (n < _cnt))
|
if ((_cnt > 0) && (n < _cnt))
|
||||||
{
|
{
|
||||||
@ -125,7 +132,7 @@ double RunningMedian::getSortedElement(uint8_t n)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// n can be max <= half the (filled) size
|
// n can be max <= half the (filled) size
|
||||||
double RunningMedian::predict(uint8_t n)
|
double RunningMedian::predict(const uint8_t n)
|
||||||
{
|
{
|
||||||
if ((_cnt > 0) && (n < _cnt/2))
|
if ((_cnt > 0) && (n < _cnt/2))
|
||||||
{
|
{
|
||||||
|
@ -2,7 +2,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.11
|
// VERSION: 0.1.12
|
||||||
// URL: http://arduino.cc/playground/Main/RunningMedian
|
// URL: http://arduino.cc/playground/Main/RunningMedian
|
||||||
// HISTORY: See RunningMedian.cpp
|
// HISTORY: See RunningMedian.cpp
|
||||||
//
|
//
|
||||||
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
#define RUNNING_MEDIAN_VERSION "0.1.11"
|
#define RUNNING_MEDIAN_VERSION "0.1.12"
|
||||||
|
|
||||||
// prepare for dynamic version
|
// prepare for dynamic version
|
||||||
// not tested use at own risk :)
|
// not tested use at own risk :)
|
||||||
@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
// should at least be 5 to be practical
|
// should at least be 5 to be practical
|
||||||
// odd size results in a 'real' middle element.
|
// odd size results in a 'real' middle element.
|
||||||
// even size takes the lower of the two middle elements (TODO)
|
// even size takes the lower of the two middle elements
|
||||||
#define MEDIAN_MIN_SIZE 1
|
#define MEDIAN_MIN_SIZE 1
|
||||||
#define MEDIAN_MAX_SIZE 19 // adjust if needed
|
#define MEDIAN_MAX_SIZE 19 // adjust if needed
|
||||||
|
|
||||||
@ -41,25 +41,25 @@
|
|||||||
class RunningMedian
|
class RunningMedian
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RunningMedian(uint8_t size); // # elements in the internal buffer
|
explicit RunningMedian(const uint8_t size); // # elements in the internal buffer
|
||||||
~RunningMedian(); // destructor
|
~RunningMedian(); // destructor
|
||||||
|
|
||||||
void clear(); // resets internal buffer and var
|
void clear(); // resets internal buffer and var
|
||||||
void add(double value); // adds a new value to internal buffer, optionally replacing the oldest element.
|
void add(const double value); // adds a new value to internal buffer, optionally replacing the oldest element.
|
||||||
double getMedian(); // returns the median == middle element
|
double getMedian(); // returns the median == middle element
|
||||||
|
|
||||||
#ifdef RUNNING_MEDIAN_ALL
|
#ifdef RUNNING_MEDIAN_ALL
|
||||||
double getAverage(); // returns average of the values in the internal buffer
|
double getAverage(); // returns average of the values in the internal buffer
|
||||||
double getAverage(uint8_t nMedian); // returns average of the middle nMedian values, removes noise from outliers
|
double getAverage(uint8_t nMedian); // returns average of the middle nMedian values, removes noise from outliers
|
||||||
double getHighest(); // returns highest element
|
double getHighest(); // returns highest element
|
||||||
double getLowest(); // return lowest element
|
double getLowest(); // return lowest element
|
||||||
|
|
||||||
double getElement(uint8_t n); // get n'th element from the values in time order
|
double getElement(const uint8_t n); // get n'th element from the values in time order
|
||||||
double getSortedElement(uint8_t n); // get n'th element from the values in size order
|
double getSortedElement(const uint8_t n); // get n'th element from the values in size order
|
||||||
double predict(uint8_t n); // predict the max change of median after n additions
|
double predict(const uint8_t n); // predict the max change of median after n additions
|
||||||
|
|
||||||
uint8_t getSize() { return _size; }; // returns size of internal buffer
|
uint8_t getSize() { return _size; }; // returns size of internal buffer
|
||||||
uint8_t getCount() { return _cnt; }; // returns current used elements, getCount() <= getSize()
|
uint8_t getCount() { return _cnt; }; // returns current used elements, getCount() <= getSize()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Loading…
Reference in New Issue
Block a user