movingAverage Helper

template<size_t WindowSize, size_t MaxDeps = 8, typename Source>
auto RxESP32::Helpers::Smoothing::movingAverage(Source &source, const typename Computed<decltype(source.get()), 1, MaxDeps>::Options &options = {})

Compute a simple moving average over a fixed-size window.

Creates a Computed signal that maintains a sliding window buffer and calculates the arithmetic mean of values. The buffer fills up to WindowSize before providing full-window averages.

Since

v0.1.0

Signal<float> sensor;
auto smoothed = movingAverage<5>(sensor); // 5-sample window

sensor.set(10); // smoothed.get() == 10 (1 sample)
sensor.set(20); // smoothed.get() == 15 (avg of 10,20)
sensor.set(30); // smoothed.get() == 20 (avg of 10,20,30)
// ... After 5 samples, window is full
sensor.set(50); // smoothed.get() == avg of last 5 values

Note

Stateful helper. Average is computed over actual count until buffer fills (e.g., first 3 values in size-5 window).

Warning

  • Only accepts arithmetic types.

  • WindowSize must be greater than 0.

Template Parameters:
size_t WindowSize

Size of the sliding window (must be > 0).

size_t MaxDeps = 8

Maximum number of dependents for the computed signal.

Parameters:
Source &source

Source (determines result type).

const typename Computed<decltype(source.get()), 1, MaxDeps>::Options &options = {}

Optional Computed configuration.

Returns:

Computed emitting the moving average.

See Also