2
$\begingroup$

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];
        }
        delayLine[0] = reader[sample];

        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] = 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.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – MBaz May 2 '16 at 13:11
  • $\begingroup$ 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. $\endgroup$ – AdrienP May 2 '16 at 13:22
2
$\begingroup$

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 >=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:

enter image description here

The normalized coefficients give this response:

enter image description here


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

reader <- rnorm(getNumSamples,0,1)
writeback <- 0*reader

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];
    }
    delayLine[1] = reader[sample];

    result = 0;
    for (i in seq(1,9)) 
    { # for each tap
          result = result + delayLine[i] * filterTaps[i]; # multiply     
    }
    writeback[sample] <- result; # output
}
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – AdrienP May 2 '16 at 13:26
  • $\begingroup$ @AdrienP see my update. $\endgroup$ – Peter K. May 2 '16 at 17:28
  • $\begingroup$ 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 ? $\endgroup$ – AdrienP May 2 '16 at 19:33
  • $\begingroup$ @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. $\endgroup$ – Peter K. May 2 '16 at 19:35
  • $\begingroup$ 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. $\endgroup$ – AdrienP May 2 '16 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.