RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::RollingAverage Class Reference

Maintains a rolling (moving) average over a fixed-length window of recent readings using a circular buffer. More...

#include <RollingAverage.h>

Public Member Functions

 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 warning) and sizing the circular buffer accordingly. More...
 
void AddReading (float reading)
 Adds a reading to the circular buffer and recomputes the current average over the valid readings. More...
 
void Reset ()
 Resets the average, current index, and valid-reading count back to their initial (empty) state. More...
 
std::string ToString () const
 Serializes the average, window length, current index, valid-reading count, and every buffered reading to a string. More...
 
float GetAverage () const
 Gets the current rolling average. More...
 
unsigned int GetWindowLength () const
 Gets the window length (the number of readings averaged over). More...
 
unsigned int GetCurrentReadingIndex () const
 Gets the index in the circular buffer that the next reading will be written to. More...
 
unsigned int GetNumberOfValidReadings () const
 Gets the number of valid readings recorded so far (grows until the buffer is full, then stays at the window length). More...
 

Protected Attributes

float myAverage
 Calculated average. More...
 
unsigned int myWindowLength
 The number of readings in total to average. More...
 
unsigned int myCurrentReadingIndex
 The index in the readings we are currently writing to. More...
 
unsigned int myNumberOfValidReadings
 The number of valid readings we have had so far. More...
 
std::vector< float > myReadings
 A circular buffer of recent readings. More...
 

Detailed Description

Maintains a rolling (moving) average over a fixed-length window of recent readings using a circular buffer.

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ RollingAverage()

RumPi::RollingAverage::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 warning) and sizing the circular buffer accordingly.

Author
Eddie O'Hagan
Date
8/13/2026
RollingAverage average(5);
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
Parameters
[in]windowLengthThe number of recent readings to average over.

Member Function Documentation

◆ AddReading()

void RumPi::RollingAverage::AddReading ( float  reading)

Adds a reading to the circular buffer and recomputes the current average over the valid readings.

Author
Eddie O'Hagan
Date
8/13/2026
average.AddReading(42.0f);
Parameters
[in]readingThe new reading to add.

◆ GetAverage()

float RumPi::RollingAverage::GetAverage ( ) const

Gets the current rolling average.

Author
Eddie O'Hagan
Date
8/13/2026
float average = rollingAverage.GetAverage();
Returns
The current average of the valid readings, or NaN if none have been taken yet.

◆ GetCurrentReadingIndex()

unsigned int RumPi::RollingAverage::GetCurrentReadingIndex ( ) const

Gets the index in the circular buffer that the next reading will be written to.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int index = rollingAverage.GetCurrentReadingIndex();
Returns
The current reading index.

◆ GetNumberOfValidReadings()

unsigned int RumPi::RollingAverage::GetNumberOfValidReadings ( ) const

Gets the number of valid readings recorded so far (grows until the buffer is full, then stays at the window length).

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int count = rollingAverage.GetNumberOfValidReadings();
Returns
The number of valid readings.

◆ GetWindowLength()

unsigned int RumPi::RollingAverage::GetWindowLength ( ) const

Gets the window length (the number of readings averaged over).

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int windowLength = rollingAverage.GetWindowLength();
Returns
The window length.

◆ Reset()

void RumPi::RollingAverage::Reset ( )

Resets the average, current index, and valid-reading count back to their initial (empty) state.

Author
Eddie O'Hagan
Date
8/13/2026
average.Reset();

◆ ToString()

std::string RumPi::RollingAverage::ToString ( ) const

Serializes the average, window length, current index, valid-reading count, and every buffered reading to a string.

Author
Eddie O'Hagan
Date
8/13/2026
std::string data = rollingAverage.ToString();
Returns
A multi-line string describing the rolling average's state.

Member Data Documentation

◆ myAverage

float RumPi::RollingAverage::myAverage
protected

Calculated average.

◆ myCurrentReadingIndex

unsigned int RumPi::RollingAverage::myCurrentReadingIndex
protected

The index in the readings we are currently writing to.

◆ myNumberOfValidReadings

unsigned int RumPi::RollingAverage::myNumberOfValidReadings
protected

The number of valid readings we have had so far.

◆ myReadings

std::vector<float> RumPi::RollingAverage::myReadings
protected

A circular buffer of recent readings.

◆ myWindowLength

unsigned int RumPi::RollingAverage::myWindowLength
protected

The number of readings in total to average.


The documentation for this class was generated from the following files: