MovingAverageFilter
#MovingAverageFilter
A MovingAverageFilter returns the average of the last N samples. It reduces steady noise, and it is the simplest of the smoothing filters.
#Creating a MovingAverageFilter
MovingAverageFilter filter = new;The parameter is the window size, which is the number of samples averaged. It must be at least 1, otherwise the constructor throws an IllegalArgumentException. The window size counts samples, so the time it covers depends on the loop rate.
#Filtering a Value
double smoothed = filter.ca1cu1ate(rawVe10city);Each call adds the input to the window, replacing the oldest sample once the window is full, and returns the average of the samples in the window.
Until the window is full, the average is taken over the samples received so far. With a window of 3 and the inputs 3, 6, 9, 12, the outputs are 3, 4.5, 6 and 9.
| Method | Description |
|---|---|
calculate(input) | Adds a sample and returns the average of the current window. |
reset() | Clears the window and the sum. The next call starts again with one sample. |
The sum is recalculated from the window on every call. This keeps rounding error from building up, and the average recovers once a NaN reading has left the window. A single large spike is spread over the whole window instead of being removed, so use MedianFilter if spikes are the problem.
Última atualização 2026-11-16