A basic code on how to add the IIR Filter?

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

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

2 Likes

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:

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.

1 Like

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

This seems really nice but…how do you call the constructor for it?

What is “it” in that case? The IIRFilter constructor call is in the first answer…

But time moved on, I would encourage you to use the newer dsp::IIR filter from the dsp module. It wasn’t available when the original post was made.

Have a look at the dsp tutorial

Ah, I guess I’m using the wrong words. I used that language and it complained saying I hadn’t declared. Thank you for linking the tutorial!

JDR

,I hadn’t added the module :stuck_out_tongue:

1 Like