# 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 < buffer.getNumSamples(); sample++) { // for each sample

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

result = 0;
for (int i = 0; i < 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.002385;
filterTaps = 0.011910;
filterTaps = 0.026352;
filterTaps = 0.039825;
filterTaps = 0.045351;
filterTaps = 0.039825;
filterTaps = 0.026352;
filterTaps = 0.011910;
filterTaps = 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.

• How did you calculate those taps? What is the cutoff frequency supposed to be? What is the frequency range of your input signal? How did you calculate the frequency content of the filter output? You need to provide much more detail. – MBaz May 2 '16 at 13:11
• You're right, I edited my initial post. Whatever the coefficients are though, I believe my problem is mostly linked to the algorithm rather than the coeffs. – AdrienP May 2 '16 at 13:22

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

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


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

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. The normalized coefficients give this response: R Code Below

 #30506
filterTaps <- c(0.002385,
0.011910,
0.026352,
0.039825,
0.045351,
0.039825,
0.026352,
0.011910,
0.002385)

filterTaps2 <- filterTaps / sum(filterTaps)

freqz(filterTaps)

freqz(filterTaps2)

getNumSamples <- 1000

delayLine <- 0*filterTaps
result <- 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];
}

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

• Thanks for this. I've used the normalized coefficients with the same results, except that now there is not the volume change I mentioned previously. However I see/hear no filtering when feeding the plugin I'm building with white noise. – AdrienP May 2 '16 at 13:26
• @AdrienP see my update. – Peter K. May 2 '16 at 17:28
• Awesome, thank you very much. Can't believe I spent several days (with breaks of course!) with such an obvious mistake waiting to be solved. Very helpful, thanks a lot. By theway, which software are you using to display those nice graphs ? – AdrienP May 2 '16 at 19:33
• @AdrienP : Thanks for the checkmark! I use R Studio since I wanted to learn R and liked the IDE that R Studio provides for it. – Peter K. May 2 '16 at 19:35
• Oh okay - I had not realized those were coming from an IDE. I'll try to find something similar or will code it myself one day. Thank you. – AdrienP May 2 '16 at 19:50