# DSP Module IIR Filtering sample by sample

**URL:** <https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977>\
**Category:** Audio Plugins\
**Created:** [January 15, 2018, 8:38pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977 "2018-01-15T20:38:25Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 15, 2018, 8:38pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/1 "2018-01-15T20:38:25Z")

</div>

Hello,

I am new to coding and am trying to create a plugin with a stereo Low Pass Filter in it. I am trying to use the dsp module to do this. I have tried to copy from the dsp module demo example. However, I need my filter to process sample by sample so I am trying to use the processSample method. When I do this it doesn’t do any filtering! Would anyone be able to guide me into setting this up pelase?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [January 15, 2018, 8:41pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/2 "2018-01-15T20:41:05Z")

</div>

Can you show your current code?

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 15, 2018, 9:18pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/3 "2018-01-15T21:18:57Z")

</div>

So I create a LPF-L and LPF-R in the private section of my audioprocessor.h

Then in the constructor in the cpp I give these filters coefficients for a given cutoff and sample rate.

Then I use the processSample in the applyDelay function that I have taken from the JuceDemoPlugin.

At the moment I am testing the plugin on a mono channel, and I can’t get it to do any filtering.

 ![37](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/c/c28255765de75acdab10760f1f76e8e8cd656e8f.png)

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 15, 2018, 9:19pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/4 "2018-01-15T21:19:30Z")

</div>

![30](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/d/d8671cef09ae23ed4edc75a8f3bb6bd0e187b1f4.png)

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 15, 2018, 9:20pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/5 "2018-01-15T21:20:35Z")

</div>

![16](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/b/b52cc54fdfdbdeb11801ae38c263b42fffb7bddf.png)

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [January 15, 2018, 9:41pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/6 "2018-01-15T21:41:42Z")

</div>

processSample in the DSP module IIR filter returns a sample, it doesn’t modify the sample passed in. You are not storing the returned sample anywhere.

---

<div class="post-metadata">

**Author:** ![jesusginard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jesusginard/32/10008_2.png) [@jesusginard](https://forum.juce.com/u/jesusginard)\
**Post date:** [January 15, 2018, 10:46pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/7 "2018-01-15T22:46:27Z")

</div>

You also need to set the coefficients in the prepareToPlay. I don’t believe that in the constructor you already have the sample rate value available. This was a headache for me a few months ago.

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 16, 2018, 3:57pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/8 "2018-01-16T15:57:23Z")

</div>

Thank you it is filtering now!

I need to use the setCoefficients() method to set the coefficients right?

First I have to do this to generate the coefficients. Then I pass this into setCoefficients?  
lpfLpointer = dsp::IIR::Coefficients::makeFirstOrderLowPass (getSampleRate(), 5000 );

In the reference guide, the following is given  
’void IIRFilter::setCoefficients ( const IIRCoefficients & newCoefficients )’

How do I apply this method to my lowPassFilterL filter?

Thanks!

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 17, 2018, 2:02pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/9 "2018-01-17T14:02:03Z")

</div>

I know its a noob question, but if anyone could help me it would be much appreciated! I am still struggling with this!

---

<div class="post-metadata">

**Author:** ![danielrudrich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/danielrudrich/32/3735_2.png) [@danielrudrich](https://forum.juce.com/u/danielrudrich)\
**Post date:** [January 17, 2018, 2:22pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/10 "2018-01-17T14:22:22Z")

</div>

Take a look at: [https://juce.com/doc/classdsp\_1\_1IIR\_1\_1Filter](https://juce.com/doc/classdsp_1_1IIR_1_1Filter)  
As you can see, the filters you use have a public attribute called `coefficients`. You can simply replace this pointer with the one you get with `dsp::IIR::Coefficients::makeFirstOrderLowPass (getSampleRate(), 5000 );`

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 17, 2018, 4:36pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/11 "2018-01-17T16:36:46Z")

</div>

Thanks Daniel!

So I’ve written this within my processblock function

if (MajorParamChange)  
{

```
    lowPassFilterL.reset();
    lpfValue = ((*myValueTree->getRawParameterValue("lpfFq")));
    lowPassFilterL.coefficients = dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (getSampleRate(), lpfValue );

```

So in theory if I move my slider it should change the filtering? However it doens’t seem to do anything? Is there something else I need to do? Something I’ve missed? Or have I just implemented this wrong?

---

<div class="post-metadata">

**Author:** ![danielrudrich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/danielrudrich/32/3735_2.png) [@danielrudrich](https://forum.juce.com/u/danielrudrich)\
**Post date:** [January 17, 2018, 4:54pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/12 "2018-01-17T16:54:12Z")

</div>

quick guess: try to dereference both sides:  
`*lowPassFilterL.coefficients = *dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (getSampleRate(), lpfValue );`  
can’t test it right now

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 17, 2018, 5:08pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/13 "2018-01-17T17:08:06Z")

</div>

Unfortunately that doesn’t seem to fix it ☹

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [January 17, 2018, 5:11pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/14 "2018-01-17T17:11:10Z")

</div>

I had the same problem, Fabian’s hint worked for me (click on the topic to get it better formatted):

> [@ProcessorDuplicator - how to?](https://forum.juce.com/t/processorduplicator-how-to/24361/4):
>
> Yes your approach looks good except for a single line. You need to dereference the filter state, as each mono filter has a pointer to the state of the ProcessorDuplicator: auto loCoeff = dsp::IIR::Coefficients\<float\>::makeLowShelf (sampleRate, loFreq, loQ, 0.25); \*filter.get\<0\> ().state = \*loCoeff; auto hiCoeff = dsp::IIR::Coefficients\<float\>::makeHighShelf (sampleRate, hiFreq, hiQ, 0.25); \*filter.get\<1\> ().state = \*hiCoeff; In general, what may be confusing is that some filters in the dsp m…

---

<div class="post-metadata">

**Author:** ![CharlieHogan](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@CharlieHogan](https://forum.juce.com/u/CharlieHogan)\
**Post date:** [January 17, 2018, 5:42pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/15 "2018-01-17T17:42:15Z")

</div>

Sorry I don’t understand that code…

myfiltername.get doesn’t seem to be a valid method?

and also if I try…  
myfiltername.state = \*NewCoefficients;

I got the following errors:

1. ‘state’ is a private member of ‘juce::dsp::IIR::Filter’
2. Assigning to ‘float \*’ from incompatible type ‘ReferencedType’ (aka ‘juce::dsp::IIR::Coefficients’)

Btw I am not using processorduplicator, I have created two mono filters. I need to do sample by sample processing and when you use precessorduplicator you can’t do that.  
[https://juce.com/doc/structdsp\_1\_1ProcessorDuplicator](https://juce.com/doc/structdsp_1_1ProcessorDuplicator)

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [January 17, 2018, 8:33pm UTC](https://forum.juce.com/t/dsp-module-iir-filtering-sample-by-sample/25977/16 "2018-01-17T20:33:04Z")

</div>

I am sorry, I wrongly assumed it would work the same. I looked at the docs, but I would have ended up the same way like you did.

Good luck.
