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

81 lines
2.1 KiB
C
Raw Normal View History

2020-11-27 05:33:55 -05:00
#pragma once
//
2011-10-09 16:24:50 -04:00
// FILE: RunningAverage.h
// AUTHOR: Rob.Tillaart@gmail.com
2022-11-23 13:22:02 -05:00
// VERSION: 0.4.3
// DATE: 2016-dec-01
2020-11-27 05:33:55 -05:00
// PURPOSE: Arduino library to calculate the running average by means of a circular buffer
// URL: https://github.com/RobTillaart/RunningAverage
#include "Arduino.h"
2021-01-29 06:31:58 -05:00
2022-11-23 13:22:02 -05:00
#define RUNNINGAVERAGE_LIB_VERSION (F("0.4.3"))
2021-01-29 06:31:58 -05:00
class RunningAverage
2011-10-09 16:24:50 -04:00
{
public:
2021-05-26 10:39:03 -04:00
explicit RunningAverage(const uint16_t size);
~RunningAverage();
2021-05-26 10:39:03 -04:00
void clear();
void add(const float value) { addValue(value); };
2021-11-24 04:03:41 -05:00
void addValue(const float value);
void fillValue(const float value, const uint16_t number);
float getValue(const uint16_t position);
2022-11-23 13:22:02 -05:00
float getAverage(); // iterates over all elements.
float getFastAverage() const; // reuses previous calculated values.
2022-11-23 13:22:02 -05:00
// return statistical characteristics of the running average
2021-05-26 10:39:03 -04:00
float getStandardDeviation() const;
float getStandardError() const;
2022-11-23 13:22:02 -05:00
// returns min/max added to the data-set since last clear
2021-05-26 10:39:03 -04:00
float getMin() const { return _min; };
float getMax() const { return _max; };
2022-11-23 13:22:02 -05:00
// returns min/max from the values in the internal buffer
2021-05-26 10:39:03 -04:00
float getMinInBuffer() const;
float getMaxInBuffer() const;
2022-11-23 13:22:02 -05:00
// return true if buffer is full
2021-05-26 10:39:03 -04:00
bool bufferIsFull() const { return _count == _size; };
2021-11-24 04:03:41 -05:00
float getElement(uint16_t index) const;
2021-05-26 10:39:03 -04:00
uint16_t getSize() const { return _size; }
uint16_t getCount() const { return _count; }
2022-11-23 13:22:02 -05:00
// use not all elements just a part from 0..partial-1
// (re)setting partial will clear the internal buffer.
2021-11-24 04:03:41 -05:00
void setPartial(const uint16_t partial = 0); // 0 ==> use all
2021-05-26 10:39:03 -04:00
uint16_t getPartial() { return _partial; };
2021-11-24 04:03:41 -05:00
2022-11-23 13:22:02 -05:00
// get some stats from the last count additions.
2021-11-24 04:03:41 -05:00
float getAverageLast(uint16_t count);
float getMinInBufferLast(uint16_t count);
float getMaxInBufferLast(uint16_t count);
2022-11-23 13:22:02 -05:00
// Experimental 0.4.3
float getAverageSubset(uint16_t start, uint16_t count);
2021-11-24 04:03:41 -05:00
2011-10-09 16:24:50 -04:00
protected:
2021-05-26 10:39:03 -04:00
uint16_t _size;
uint16_t _count;
uint16_t _index;
uint16_t _partial;
float _sum;
float* _array;
float _min;
float _max;
2011-10-09 16:24:50 -04:00
};
2021-11-24 04:03:41 -05:00
2020-11-27 05:33:55 -05:00
// -- END OF FILE --
2021-11-24 04:03:41 -05:00