RIT VEXU Core API
Loading...
Searching...
No Matches
MovingAverage Class Reference

#include <moving_average.h>

Inheritance diagram for MovingAverage:
Filter

Public Member Functions

 MovingAverage (int buffer_size)
 
 MovingAverage (int buffer_size, double starting_value)
 
void add_entry (double n) override
 
double get_value () const override
 
int get_size () const
 

Detailed Description

MovingAverage

A moving average is a way of smoothing out noisy data. For many sensor readings, the noise is roughly symmetric around the actual value. This means that if you collect enough samples those that are too high are cancelled out by the samples that are too low leaving the real value.

The MovingAverage class provides a simple interface to do this smoothing from our noisy sensor values.

WARNING: because we need a lot of samples to get the actual value, the value given by the MovingAverage will 'lag' behind the actual value that the sensor is reading. Using a MovingAverage is thus a tradeoff between accuracy and lag time (more samples) vs. less accuracy and faster updating (less samples).

Constructor & Destructor Documentation

◆ MovingAverage() [1/2]

MovingAverage::MovingAverage ( int buffer_size)

Create a moving average calculator with 0 as the default value

Parameters
buffer_sizeThe size of the buffer. The number of samples that constitute a valid reading

◆ MovingAverage() [2/2]

MovingAverage::MovingAverage ( int buffer_size,
double starting_value )

Create a moving average calculator with a specified default value

Parameters
buffer_sizeThe size of the buffer. The number of samples that constitute a valid reading
starting_valueThe value that the average will be before any data is added

Member Function Documentation

◆ add_entry()

void MovingAverage::add_entry ( double n)
overridevirtual

Add a reading to the buffer Before: [ 1 1 2 2 3 3] => 2 ^ After: [ 2 1 2 2 3 3] => 2.16 ^

Parameters
nthe sample that will be added to the moving average.

Implements Filter.

◆ get_size()

int MovingAverage::get_size ( ) const

How many samples the average is made from

Returns
the number of samples used to calculate this average

◆ get_value()

double MovingAverage::get_value ( ) const
overridevirtual

Returns the average based off of all the samples collected so far

Returns
the calculated average. sum(samples)/numsamples

How many samples the average is made from

Returns
the number of samples used to calculate this average

Implements Filter.


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