RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RollingAverage.h
Go to the documentation of this file.
1#pragma once
2#include "../Common.h"
3#include <vector>
4
5namespace RumPi
6{
14 {
15 protected:
17 float myAverage;
18
20 unsigned int myWindowLength;
21
24
27
29 std::vector<float> myReadings;
30
31 public:
45 RollingAverage(unsigned int windowLength = RUMPI_MAX_NUM_SAMPLES);
46
59 void AddReading(float reading);
60
71 void Reset();
72
85 std::string ToString() const;
86
99 float GetAverage() const;
100
113 unsigned int GetWindowLength() const;
114
127 unsigned int GetCurrentReadingIndex() const;
128
141 unsigned int GetNumberOfValidReadings() const;
142 };
143} // namespace RumPi
Maintains a rolling (moving) average over a fixed-length window of recent readings using a circular b...
Definition: RollingAverage.h:14
unsigned int GetWindowLength() const
Gets the window length (the number of readings averaged over).
Definition: RollingAverage.cpp:126
unsigned int myNumberOfValidReadings
The number of valid readings we have had so far.
Definition: RollingAverage.h:26
std::string ToString() const
Serializes the average, window length, current index, valid-reading count, and every buffered reading...
Definition: RollingAverage.cpp:177
unsigned int myCurrentReadingIndex
The index in the readings we are currently writing to.
Definition: RollingAverage.h:23
unsigned int GetNumberOfValidReadings() const
Gets the number of valid readings recorded so far (grows until the buffer is full,...
Definition: RollingAverage.cpp:160
float GetAverage() const
Gets the current rolling average.
Definition: RollingAverage.cpp:103
unsigned int myWindowLength
The number of readings in total to average.
Definition: RollingAverage.h:20
std::vector< float > myReadings
A circular buffer of recent readings.
Definition: RollingAverage.h:29
float myAverage
Calculated average.
Definition: RollingAverage.h:17
void AddReading(float reading)
Adds a reading to the circular buffer and recomputes the current average over the valid readings.
Definition: RollingAverage.cpp:48
unsigned int GetCurrentReadingIndex() const
Gets the index in the circular buffer that the next reading will be written to.
Definition: RollingAverage.cpp:143
RollingAverage(unsigned int windowLength=RUMPI_MAX_NUM_SAMPLES)
Builds a rolling average over the given window length, defaulting a zero window to one (with a warnin...
Definition: RollingAverage.cpp:20
void Reset()
Resets the average, current index, and valid-reading count back to their initial (empty) state.
Definition: RollingAverage.cpp:84
RumPi is the library's top-level facade.
Definition: AlertManager.h:6
constexpr unsigned int RUMPI_MAX_NUM_SAMPLES
Default rolling-average window length.
Definition: Common.h:59