ema Helper

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

Compute an exponential moving average (EMA) with configurable smoothing factor.

Creates a Computed signal that applies exponential smoothing to source values. Unlike simple moving average, EMA gives more weight to recent values while still considering all historical data with exponentially decaying influence.

Since

v0.1.0

Formula: \(EMA_{new} = \alpha * current + (1 - \alpha) * EMA_{prev}\)

Signal<float> sensor;
auto smoothed = ema(sensor, 0.2f); // 20% current, 80% history

sensor.set(100.0); // smoothed.get() == 100.0  (first value)
sensor.set(200.0); // smoothed.get() == 120.0  (0.2*200 + 0.8*100)
sensor.set(300.0); // smoothed.get() == 156.0  (0.2*300 + 0.8*120)

Note

  • Stateful helper. On first call, EMA is initialized to the first value.

  • alpha = 1.0 disables smoothing (passes through current value).

  • alpha = 2/(N+1) approximates N-period simple moving average.

Warning

Only accepts floating-point types (float, double).

Template Parameters:
size_t MaxDeps = 8

Maximum number of dependents for the computed signal.

Parameters:
Source &source

Source (determines result type).

decltype(source.get()) alpha

Smoothing factor (0 < alpha 1). Higher values = more responsive, less smooth.

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

Optional Computed configuration.

Returns:

Computed signal emitting the exponential moving average.

See Also