OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
ossia::moving_average_filter< T, N > Struct Template Reference

Simple moving average over the last N samples. More...

Detailed Description

template<typename T = float, std::size_t N = 8>
struct ossia::moving_average_filter< T, N >

Simple moving average over the last N samples.

O(1) per sample: the window sum is kept incrementally instead of being recomputed. The accumulator is widened to double for float inputs, because a running sum drifts over a long performance.

Until N samples have been seen the average is taken over what is available, so the output is usable from the first sample on.

Public Types

using value_type = T
 
using accumulator_type = std::conditional_t< std::is_same_v< T, float >, double, T >
 Widened so that the incremental sum does not drift.
 

Public Member Functions

OSSIA_INLINE void reset () noexcept
 Forget the history: the next sample restarts the filter.
 
OSSIA_INLINE void assign_parameters (const moving_average_filter &) noexcept
 No runtime parameters: the window size is a template argument.
 
OSSIA_INLINE T operator() (T x) noexcept
 

Public Attributes

std::array< T, N > history {}
 Chronological ring of the window contents.
 
accumulator_type sum {}
 Running sum of the first count entries of the window.
 
std::uint32_t head {}
 Where the next sample goes in history.
 
std::uint32_t count {}
 How many entries of history are valid, saturating at N.