Low pass filter

I wrote a low pass filter class, but it keeps reporting errors, “juce::dsp::IIR::Filter< float>” No member “setCoefficients”.

#include <juce_dsp/juce_dsp.h>

class LowPassFilter
{
public:
LowPassFilter()
{
// Set default parameters
updateFilter();
}

void setCutoffFrequency(float newCutoffFrequency)
{
    cutoffFrequency = newCutoffFrequency;
    updateFilter();
}

float processSample(float inputValue)
{
    return filter.processSample(inputValue);
}

private:
void updateFilter()
{
auto sampleRate = 44100.0; // Set your sample rate here
auto cutoffNormalized = cutoffFrequency / sampleRate;
juce::dsp::ProcessSpec spec{ sampleRate, 512, 2 }; // Adjust buffer size as needed
filter.prepare(spec);
filter.reset();
juce::dsp::IIR::Coefficients::Ptr coefficients = juce::dsp::IIR::Coefficients::makeLowPass(sampleRate, cutoffNormalized);
filter.setCoefficients(*coefficients);
}

float cutoffFrequency = 1000.0; // Default cutoff frequency
juce::dsp::IIR::Filter<float> filter;

};

Well, I think the error is correct, a look at the IIR::Filter class documentation reveals that there is indeed no setCoefficients member function. Setting the coefficients works by assigning the public coefficients member. Note that it’s a ref counted pointer, so you need to dereference it in order to assign something to it. I tend to use the IIR::ArrayCoefficients class for that since it’s allocation free, with it code looks like

auto newCoefficients = juce::dsp::IIR::ArrayCoefficients<float>::makeLowPass (sampleRate, cutoffFrequency);
*filter.coefficients = newCoefficients;

Two further notes: You set a normalised frequency in your code snippet above, while makeLowPass expects a non-normalised frequency. And then if you post code here, just put three backticks (```) above and below your code block and it will be displayed nicely readable in the forum :slight_smile:

1 Like

thank you !