13#error "This header is C++-only."
30 template <
typename Value,
typename Scalar>
struct LowPassIIR
34 static_assert(std::is_floating_point<Scalar>::value,
35 "Filter is designed only for floating-point values. If "
36 "you want fixed-point, you must reimplement it.");
52 : state(val), time_constant(
static_cast<Scalar>(1.f / (2.f * M_PI * cutoff_hz)))
59 reset(Value
const &val)
noexcept
63 filter_timestamp_ns = 0;
81 filter_timestamp_ns = timestamp_ns;
87 Scalar weighted = dt * weight;
88 Scalar alpha = weighted / (time_constant + weighted);
94 state += alpha * (sample - state);
95 filter_timestamp_ns = timestamp_ns;
100 bool initialized{
false};
145 impl_.
addSample(sample, timestamp_ns, weight);
163 return impl_.filter_timestamp_ns;
172 return impl_.initialized;
A very simple low-pass filter, using a "one-pole infinite impulse response" design (one-pole IIR).
Definition: m_lowpass_float.hpp:112
void addSample(Scalar sample, timepoint_ns timestamp_ns, Scalar weight=1)
Filter a sample, with an optional weight.
Definition: m_lowpass_float.hpp:143
LowPassIIRFilter(Scalar cutoff_hz) noexcept
Constructor.
Definition: m_lowpass_float.hpp:121
Scalar getState() const noexcept
Get the filtered value.
Definition: m_lowpass_float.hpp:152
timepoint_ns getTimestampNs() const noexcept
Get the time of last update.
Definition: m_lowpass_float.hpp:161
bool isInitialized() const noexcept
Get whether we have initialized state.
Definition: m_lowpass_float.hpp:170
void reset() noexcept
Reset the filter to newly-created state.
Definition: m_lowpass_float.hpp:128
int64_t timepoint_ns
Integer timestamp type.
Definition: u_time.h:70
static double time_ns_to_s(time_duration_ns ns)
Convert nanoseconds duration to double seconds.
Definition: u_time.h:90
Wrapper header for <math.h> to ensure pi-related math constants are defined.
C++-only functionality in the Math helper library.
Definition: m_documentation.hpp:15
The shared implementation (between vector and scalar versions) of an IIR low-pass filter.
Definition: m_lowpass_float.hpp:31
void addSample(Value const &sample, timepoint_ns timestamp_ns, Scalar weight=1)
Filter a sample, with an optional weight.
Definition: m_lowpass_float.hpp:76
void reset(Value const &val) noexcept
Reset the filter to newly-created state.
Definition: m_lowpass_float.hpp:59
LowPassIIR(Scalar cutoff_hz, Value const &val) noexcept
Constructor.
Definition: m_lowpass_float.hpp:51
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.