A basic code on how to add the IIR Filter?


#1

Hi everybody, is there any basic tutorial about adding the IIR filter ? Thank You in advance !


#2

Hi @fabi74,
it is quite straight forward.

Version a)
Create a filter instance for each channel / signal. To set the filter up use the static methods in IIRCoefficients, e.g.:

filter = new IIRFilter (IIRCoefficients::makeHighPass (sampleRate, 1000, 1.0)); // in prepareToPlay

filter->processSamples (buffer.getWritePointer (i), buffer.getNumSamples());    // in processBlock

Caveat: have a separate instance for each channel, otherwise the state of the filter doesn’t match the signal and you get distortions when swapping the channel / each block.

Version b)
Use an IIRFilterAudioSource, should be straight forward as well

HTH


#3

Hi @daniel, thank You very much for Your help ! I’m very very new to C++ and JUCE ( I’m reading pdf and stuff to improve this new interest). One last question, if it is not too much (I really need to understand the first steps).Then in my experiment I need to assign the slider which I’ve already created and named “cutoffSlider” to manage the filter but it’s hard to me to indentify which variable I have to declare in order to manage the IIR filter with the slider ( I’ve tried to look for this example but unfortunately there are few thread for beginners) Thank You in advance my friend Have a lovely day ! :slight_smile:


#4

You can set new coefficients to the filter in the processBlock. It is a good idea to have a member storing the last value of your parameter, so you do nothing, if it wasn’t changed. I’m assuming you have your filters in an array for each channel:

if (*cutOff != lastCutOff) {
    for (auto filter : filters) {
        filter->setCoefficients (IIRCoefficients::makeHighPass (getSampleRate(), *cutOff, 1.0f));
    }
    lastCutOff = *cutOff;
}

Further down the line you may want to look into details like smoothing of the coefficients, because sudden changes can cause distortions. But if you sweep gently, it should sound all right.


#5

Thanks a lot @daniel, I’ll try it !!! Have a good day ! :slight_smile: