GY-63_MS5611/libraries/RunningMedian/RunningMedian.h

94 lines
2.6 KiB
C
Raw Normal View History

2020-11-27 05:33:55 -05:00
#pragma once
//
2011-10-09 16:24:29 -04:00
// FILE: RunningMedian.h
2021-01-29 06:31:58 -05:00
// AUTHOR: Rob Tillaart
2011-10-09 16:24:29 -04:00
// PURPOSE: RunningMedian library for Arduino
2021-01-29 06:31:58 -05:00
// VERSION: 0.3.3
2020-11-27 05:33:55 -05:00
// URL: https://github.com/RobTillaart/RunningMedian
2011-10-09 16:24:29 -04:00
// URL: http://arduino.cc/playground/Main/RunningMedian
// HISTORY: See RunningMedian.cpp
//
2021-01-29 06:31:58 -05:00
2013-08-17 08:54:10 -04:00
#include "Arduino.h"
2021-01-29 06:31:58 -05:00
#define RUNNING_MEDIAN_VERSION (F("0.3.3"))
// fall back to fixed storage for dynamic version => remove true
#define RUNNING_MEDIAN_USE_MALLOC true
2021-01-29 06:31:58 -05:00
// MEDIAN_MIN_SIZE should at least be 3 to be practical,
#define MEDIAN_MIN_SIZE 3
2011-10-09 16:24:29 -04:00
2021-01-29 06:31:58 -05:00
#ifdef RUNNING_MEDIAN_USE_MALLOC
// max 250 to not overflow uint8_t internal vars
#define MEDIAN_MAX_SIZE 255
#else
// using fixed memory will be limited to 19 elements.
#define MEDIAN_MAX_SIZE 19
#endif
class RunningMedian
2011-10-09 16:24:29 -04:00
{
public:
2020-11-27 05:33:55 -05:00
// # elements in the internal buffer
2021-01-29 06:31:58 -05:00
// odd sizes results in a 'real' middle element and will be a bit faster.
// even sizes takes the average of the two middle elements as median
2020-11-27 05:33:55 -05:00
explicit RunningMedian(const uint8_t size);
~RunningMedian();
// resets internal buffer and var
2021-01-29 06:31:58 -05:00
void clear();
2020-11-27 05:33:55 -05:00
// adds a new value to internal buffer, optionally replacing the oldest element.
2021-01-29 06:31:58 -05:00
void add(const float value);
2020-11-27 05:33:55 -05:00
// returns the median == middle element
2021-01-29 06:31:58 -05:00
float getMedian();
2020-11-27 05:33:55 -05:00
// returns the Quantile
2021-01-29 06:31:58 -05:00
float getQuantile(const float q);
2020-11-27 05:33:55 -05:00
// returns average of the values in the internal buffer
2021-01-29 06:31:58 -05:00
float getAverage();
2020-11-27 05:33:55 -05:00
// returns average of the middle nMedian values, removes noise from outliers
2021-01-29 06:31:58 -05:00
float getAverage(uint8_t nMedian);
2021-01-29 06:31:58 -05:00
float getHighest() { return getSortedElement(_count - 1); };
float getLowest() { return getSortedElement(0); };
2020-11-27 05:33:55 -05:00
// get n'th element from the values in time order
2021-01-29 06:31:58 -05:00
float getElement(const uint8_t n);
2020-11-27 05:33:55 -05:00
// get n'th element from the values in size order
2021-01-29 06:31:58 -05:00
float getSortedElement(const uint8_t n);
2020-11-27 05:33:55 -05:00
// predict the max change of median after n additions
2021-01-29 06:31:58 -05:00
float predict(const uint8_t n);
2020-11-27 05:33:55 -05:00
2021-01-29 06:31:58 -05:00
uint8_t getSize() { return _size; };
2020-11-27 05:33:55 -05:00
// returns current used elements, getCount() <= getSize()
2021-01-29 06:31:58 -05:00
uint8_t getCount() { return _count; };
bool isFull() { return (_count == _size); }
protected:
2021-01-29 06:31:58 -05:00
boolean _sorted; // _sortIdx{} is up to date
uint8_t _size; // max number of values
uint8_t _count; // current number of values
uint8_t _index; // next index to add.
2021-01-29 06:31:58 -05:00
// _values holds the elements themself
// _p holds the index for sorted
#ifdef RUNNING_MEDIAN_USE_MALLOC
2021-01-29 06:31:58 -05:00
float * _values;
uint8_t * _sortIdx;
#else
2021-01-29 06:31:58 -05:00
float _values[MEDIAN_MAX_SIZE];
uint8_t _p[MEDIAN_MAX_SIZE];
#endif
void sort();
2011-10-09 16:24:29 -04:00
};
// END OF FILE