13 #error "This header is C++-only." 19 #include <type_traits> 23 namespace implementation {
28 template <
typename Value,
typename Scalar>
struct LowPassIIR 33 std::is_floating_point<Scalar>::value,
34 "Filter is designed only for floating-point values. If " 35 "you want fixed-point, you must reimplement it.");
50 explicit LowPassIIR(Scalar cutoff_hz, Value
const &val) noexcept
51 : state(val), time_constant(1.f / (2.f * M_PI * cutoff_hz))
58 reset(Value
const &val) noexcept
62 filter_timestamp_ns = 0;
82 filter_timestamp_ns = timestamp_ns;
87 time_ns_to_s(timestamp_ns - filter_timestamp_ns);
89 Scalar weighted = dt * weight;
90 Scalar alpha = weighted / (time_constant + weighted);
96 state += alpha * (sample - state);
97 filter_timestamp_ns = timestamp_ns;
101 Scalar time_constant;
102 bool initialized{
false};
124 : impl_(cutoff_hz, 0)
149 impl_.addSample(sample, timestamp_ns, weight);
167 return impl_.filter_timestamp_ns;
176 return impl_.initialized;
LowPassIIR(Scalar cutoff_hz, Value const &val) noexcept
Constructor.
Definition: t_lowpass.hpp:50
LowPassIIRFilter(Scalar cutoff_hz) noexcept
Constructor.
Definition: t_lowpass.hpp:123
A very simple low-pass filter, using a "one-pole infinite impulse response" design (one-pole IIR)...
Definition: t_lowpass.hpp:113
void addSample(Value const &sample, timepoint_ns timestamp_ns, Scalar weight=1)
Filter a sample, with an optional weight.
Definition: t_lowpass.hpp:75
void addSample(Scalar sample, timepoint_ns timestamp_ns, Scalar weight=1)
Filter a sample, with an optional weight.
Definition: t_lowpass.hpp:147
Time-keeping: a clock that is steady, convertible to system time, and ideally high-resolution.
void reset(Value const &val) noexcept
Reset the filter to just-created state.
Definition: t_lowpass.hpp:58
timepoint_ns getTimestampNs() const noexcept
Access the time of last update.
Definition: t_lowpass.hpp:165
int64_t timepoint_ns
Integer timestamp type.
Definition: u_time.h:34
bool isInitialized() const noexcept
Access whether we have initialized state.
Definition: t_lowpass.hpp:174
Scalar getState() const noexcept
Access the filtered value.
Definition: t_lowpass.hpp:156
Definition: t_fusion.hpp:25
void reset() noexcept
Reset the filter to just-created state.
Definition: t_lowpass.hpp:132
The shared implementation (between vector and scalar versions) of an IIR low-pass filter...
Definition: t_lowpass.hpp:28