This repository regroups 4 moving average filters. These filters have been designed to perform an online smoothing of real-time data (Ex: Data from a sensor). All the following equations have been extracted from Moving Average and Exponential Smoothing.
- Sampling frequency (Hz)
- Duration over which averaging is to be performed (s)
From these two parameters, we can determine the number of points on which we can apply smoothing. (A static assertion in the constructor checks if the subset size computed is null or not)
Until reaching the subset size wanted, we perform a cumulative moving average using the following recursive relationship between the current and the previous mean:
Once the subset has the required size, we perform the simple moving average using the following recursive equation:
-
Sampling frequency (Hz)
-
Duration over which averaging is to be performed (s)
From these two parameters, we can determine the number of points on which we can apply smoothing.
(A static assertion in the constructor checks if the subset size computed is null or not)
Until reaching the subset size wanted, we perform a cumulative moving average using the following recursive relationship between the current and the previous mean:
Once the subset has the required size, we compute its median and make it "slide".
This method may be both more memory and time-consuming than the others because to compute the median we realize a copy of the subset and then sort it.
In the following exponential smoothing filters, we don't exploit their full potential of forecasting more than one step beyond the current signal. Indeed, in our case, we want the smoothed value of the last signal value.
However, we can imagine going further using data fusion to take into account all the forecasted values.
- Data smoothing factor
$\alpha \in [0,1]$ (A static assertion in the constructor checks if it's in the right interval)
We use a boolean flag to check if the data given is the first one or not.
Then, for each new item of data, we compute the smoothed value
- Data smoothing factor
$\alpha \in [0,1]$ (A static assertion in the constructor checks if it's in the right interval) - Trend smoothing factor
$\beta \in [0,1]$ (A static assertion in the constructor checks if it's in the right interval)
We use boolean flags to initialize these two values.
Then, for each new item of data, we compute the smoothed value
- Include the header corresponding to the filter you want to use
#include "SimpleMovingAverage.hpp"
- Type of
Signal
that corresponds to the traits checked inTemporalSmoother
(integers, numerical, Eigen matrices...). You can add an allocator for the filters such as SMA which needs one.
using SimpleMovingAverageType = SimpleMovingAverage<Signal>;
- Instantiate the filter using the inputs given in the sections above:
SimpleMovingAverageType movingFilter{samplingFrequency, subsetDuration};
- When a new item of data is available, use the
operator+()
to filter it.
const auto filteredValue{movingFilter + lastSignalData };
These methods are common to all the filters.