From d8e18a0fc78699cb2b16804873ade635c1600e71 Mon Sep 17 00:00:00 2001 From: Iampete1 Date: Tue, 3 Oct 2023 19:02:50 +0100 Subject: [PATCH] Filter: LowPassFilter2p: constrain cuttoff to 40% of sample rate --- libraries/Filter/LowPassFilter2p.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libraries/Filter/LowPassFilter2p.cpp b/libraries/Filter/LowPassFilter2p.cpp index 8154ab002d..7749387700 100644 --- a/libraries/Filter/LowPassFilter2p.cpp +++ b/libraries/Filter/LowPassFilter2p.cpp @@ -17,13 +17,12 @@ DigitalBiquadFilter::DigitalBiquadFilter() { template T DigitalBiquadFilter::apply(const T &sample, const struct biquad_params ¶ms) { - if(is_zero(params.cutoff_freq) || is_zero(params.sample_freq)) { + if(!is_positive(params.cutoff_freq) || !is_positive(params.sample_freq)) { return sample; } if (!initialised) { reset(sample, params); - initialised = true; } T delay_element_0 = sample - _delay_element_1 * params.a1 - _delay_element_2 * params.a2; @@ -37,7 +36,6 @@ T DigitalBiquadFilter::apply(const T &sample, const struct biquad_params &par template void DigitalBiquadFilter::reset() { - _delay_element_1 = _delay_element_2 = T(); initialised = false; } @@ -49,14 +47,15 @@ void DigitalBiquadFilter::reset(const T &value, const struct biquad_params &p template void DigitalBiquadFilter::compute_params(float sample_freq, float cutoff_freq, biquad_params &ret) { - ret.cutoff_freq = cutoff_freq; + // Keep well under Nyquist limit + ret.cutoff_freq = MIN(cutoff_freq, sample_freq * 0.4); ret.sample_freq = sample_freq; if (!is_positive(ret.cutoff_freq)) { // zero cutoff means pass-thru return; } - float fr = sample_freq/cutoff_freq; + float fr = ret.sample_freq / ret.cutoff_freq; float ohm = tanf(M_PI/fr); float c = 1.0f+2.0f*cosf(M_PI/4.0f)*ohm + ohm*ohm; @@ -103,10 +102,6 @@ float LowPassFilter2p::get_sample_freq(void) const { template T LowPassFilter2p::apply(const T &sample) { - if (!is_positive(_params.cutoff_freq)) { - // zero cutoff means pass-thru - return sample; - } return _filter.apply(sample, _params); }