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

Running median over the last N samples. More...

Detailed Description

template<typename T = float, std::size_t N = 5>
struct ossia::median_filter< T, N >

Running median over the last N samples.

Unlike an average, this rejects isolated outliers outright, which is what one wants against the occasional bogus reading of a sensor or a tracker.

Kept as a chronological ring plus a sorted copy of the same window: an insert is a binary search and a small contiguous move, which for the window sizes this is meant for beats any heap-based scheme by a wide margin.

Note
Feeding a NaN breaks the ordering invariant of the sorted window, and with it every subsequent output. Filter them out upstream.

Public Types

using value_type = T
 

Public Member Functions

OSSIA_INLINE void reset () noexcept
 Forget the history: the next sample restarts the filter.
 
OSSIA_INLINE void assign_parameters (const median_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.
 
std::array< T, N > sorted {}
 The same contents, kept sorted over the first count entries.
 
std::uint32_t head {}
 Where the next sample goes in history.
 
std::uint32_t count {}
 How many entries are valid, saturating at N.