Basic FIR Filtering with audio buffer in C++ I am willing to implement a basic FIR filter using C++ and JUCE.

I wrote this simple algorithm using the information I found in the DSPGuide, but I only seem to get a variation in volume for all the frequencies, instead of a frequency filtering.

How can I correct it to effectively apply frequency filtering ?

if (channel == 0) {

    float result = 0; // initialisation to 0 of the result

    for (int sample = 0; sample &lt; buffer.getNumSamples(); sample++) { // for each sample

        // moving the samples in the delay line
        for (int i = 0; i &lt; 8; i++) { // delayLine being 9 values long
            delayLine[i + 1] = delayLine[i];
        }
        delayLine[0] = reader[sample];

        result = 0;
        for (int i = 0; i &lt; 9; i++) { // for each tap
            result = result + delayLine[i] * filterTaps[i]; // multiply     
        }
        writeback[sample] = result; // output
    }
} 


Here are the coefficients used, which are supposed to produce a Low-Pass Filter in the audible range :

filterTaps[0] = 0.002385;
    filterTaps[1] = 0.011910;
    filterTaps[2] = 0.026352;
    filterTaps[3] = 0.039825;
    filterTaps[4] = 0.045351;
    filterTaps[5] = 0.039825;
    filterTaps[6] = 0.026352;
    filterTaps[7] = 0.011910;
    filterTaps[8] = 0.002385;


I have generated them using this website http://www.arc.id.au/FilterDesign.html 
Can't remember the cut-off I chose, but it was between 1000 and 5000 kHz with a 44100 Hz sampling rate. These coefficients are just an example, as my initial goal was to make an anti-aliasing filter. Since the filter was not working properly, probably because of the algorithm, I chose another set of coefficients in the audible range to ease the debug process).

Thank you.
Your implementation of the delay line is flawed.  It's just copying the second last sample over each element.

Try this instead:

    for (int i = 7; i &gt;=0; i--) { // delayLine being 9 values long
        delayLine[i + 1] = delayLine[i];
    }




Previous answer below

Your filter seems to be a lossy low pass filter --- even the passband has -15dB attenuation.

Try using these coefficients instead:

0.01156111 0.05773286 0.12773940 0.19304879 0.21983567 0.19304879 0.12773940 0.05773286 0.01156111


They are obtained by normalizing the sum of the coefficients you provided.

Your filter has this response:



The normalized coefficients give this response:





R Code Below

 #30506
filterTaps &lt;- c(0.002385,
0.011910,
0.026352,
0.039825,
0.045351,
0.039825,
0.026352,
0.011910,
0.002385)

filterTaps2 &lt;- filterTaps / sum(filterTaps)

freqz(filterTaps)


freqz(filterTaps2)

getNumSamples &lt;- 1000

reader &lt;- rnorm(getNumSamples,0,1)
writeback &lt;- 0*reader

delayLine &lt;- 0*filterTaps
result &lt;- 0; # initialisation to 0 of the result
for (sample in seq(1,getNumSamples)) 
{ # for each sample

    # moving the samples in the delay line
    for (i in seq(8,1,-1)) 
    { # delayLine being 9 values long
        delayLine[i + 1] = delayLine[i];
    }
    delayLine[1] = reader[sample];

    result = 0;
    for (i in seq(1,9)) 
    { # for each tap
          result = result + delayLine[i] * filterTaps[i]; # multiply     
    }
    writeback[sample] &lt;- result; # output
}