// /// @file LowPassFilter.cpp /// @brief A class to implement a low pass filter #ifndef HAL_DEBUG_BUILD #define AP_INLINE_VECTOR_OPS #pragma GCC optimize("O2") #endif #include "LowPassFilter.h" #include //////////////////////////////////////////////////////////////////////////////////////////// // DigitalLPF, base class //////////////////////////////////////////////////////////////////////////////////////////// template DigitalLPF::DigitalLPF() { // built in initialization output = T(); } // add a new raw value to the filter, retrieve the filtered result template T DigitalLPF::_apply(const T &sample, const float &alpha) { output += (sample - output) * alpha; if (!initialised) { initialised = true; output = sample; } return output; } // get latest filtered value from filter (equal to the value returned by latest call to apply method) template const T &DigitalLPF::get() const { return output; } // Reset filter to given value template void DigitalLPF::reset(const T &value) { output = value; initialised = true; } // Set reset flag such that the filter will be reset to the next value applied template void DigitalLPF::reset() { initialised = false; } template class DigitalLPF; template class DigitalLPF; template class DigitalLPF; //////////////////////////////////////////////////////////////////////////////////////////// // Low pass filter with constant time step //////////////////////////////////////////////////////////////////////////////////////////// // constructor template LowPassFilterConstDt::LowPassFilterConstDt(const float &sample_freq, const float &new_cutoff_freq) { set_cutoff_frequency(sample_freq, new_cutoff_freq); } // change parameters template void LowPassFilterConstDt::set_cutoff_frequency(const float &sample_freq, const float &new_cutoff_freq) { cutoff_freq = new_cutoff_freq; if (sample_freq <= 0) { alpha = 1; } else { alpha = calc_lowpass_alpha_dt(1.0/sample_freq, cutoff_freq); } } // return the cutoff frequency template float LowPassFilterConstDt::get_cutoff_freq(void) const { return cutoff_freq; } // add a new raw value to the filter, retrieve the filtered result template T LowPassFilterConstDt::apply(const T &sample) { return this->_apply(sample, alpha); } template class LowPassFilterConstDt; template class LowPassFilterConstDt; template class LowPassFilterConstDt; //////////////////////////////////////////////////////////////////////////////////////////// // Low pass filter with variable time step //////////////////////////////////////////////////////////////////////////////////////////// template LowPassFilter::LowPassFilter(const float &new_cutoff_freq) { set_cutoff_frequency(new_cutoff_freq); } // change parameters template void LowPassFilter::set_cutoff_frequency(const float &new_cutoff_freq) { cutoff_freq = new_cutoff_freq; } // return the cutoff frequency template float LowPassFilter::get_cutoff_freq() const { return cutoff_freq; } // add a new raw value to the filter, retrieve the filtered result template T LowPassFilter::apply(const T &sample, const float &dt) { const float alpha = calc_lowpass_alpha_dt(dt, cutoff_freq); return this->_apply(sample, alpha); } /* * Make an instances * Otherwise we have to move the constructor implementations to the header file :P */ template class LowPassFilter; template class LowPassFilter; template class LowPassFilter;