|
|
|
@ -24,13 +24,29 @@ T DigitalLPF<T>::apply(const T &sample, float cutoff_freq, float dt) {
@@ -24,13 +24,29 @@ T DigitalLPF<T>::apply(const T &sample, float cutoff_freq, float dt) {
|
|
|
|
|
_output = sample; |
|
|
|
|
return _output; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
float rc = 1.0f/(M_2PI*cutoff_freq); |
|
|
|
|
float alpha = constrain_float(dt/(dt+rc), 0.0f, 1.0f); |
|
|
|
|
alpha = constrain_float(dt/(dt+rc), 0.0f, 1.0f); |
|
|
|
|
_output += (sample - _output) * alpha; |
|
|
|
|
return _output; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
|
T DigitalLPF<T>::apply(const T &sample) { |
|
|
|
|
_output += (sample - _output) * alpha; |
|
|
|
|
return _output; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
|
void DigitalLPF<T>::compute_alpha(float sample_freq, float cutoff_freq) { |
|
|
|
|
if (cutoff_freq <= 0.0f || sample_freq <= 0.0f) { |
|
|
|
|
alpha = 1.0; |
|
|
|
|
} else { |
|
|
|
|
float dt = 1.0/sample_freq; |
|
|
|
|
float rc = 1.0f/(M_2PI*cutoff_freq); |
|
|
|
|
alpha = constrain_float(dt/(dt+rc), 0.0f, 1.0f); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// get latest filtered value from filter (equal to the value returned by latest call to apply method)
|
|
|
|
|
template <class T> |
|
|
|
|
const T &DigitalLPF<T>::get() const { |
|
|
|
@ -61,6 +77,12 @@ void LowPassFilter<T>::set_cutoff_frequency(float cutoff_freq) {
@@ -61,6 +77,12 @@ void LowPassFilter<T>::set_cutoff_frequency(float cutoff_freq) {
|
|
|
|
|
_cutoff_freq = cutoff_freq; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
|
void LowPassFilter<T>::set_cutoff_frequency(float sample_freq, float cutoff_freq) { |
|
|
|
|
_cutoff_freq = cutoff_freq; |
|
|
|
|
_filter.compute_alpha(sample_freq, cutoff_freq); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// return the cutoff frequency
|
|
|
|
|
template <class T> |
|
|
|
|
float LowPassFilter<T>::get_cutoff_freq(void) const { |
|
|
|
@ -72,6 +94,11 @@ T LowPassFilter<T>::apply(T sample, float dt) {
@@ -72,6 +94,11 @@ T LowPassFilter<T>::apply(T sample, float dt) {
|
|
|
|
|
return _filter.apply(sample, _cutoff_freq, dt); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
|
T LowPassFilter<T>::apply(T sample) { |
|
|
|
|
return _filter.apply(sample); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
|
const T &LowPassFilter<T>::get() const { |
|
|
|
|
return _filter.get(); |
|
|
|
|