Hello good people. I have tried to implement basic FIR filtering into my audio plugin. I have generated some coefficients with matlab,tried filtering a signal in matlab with those coeffs and everything was fine. Then I’ve tried the same thing in C++ but the results was not same nor close enough. Here are my coefficients,filter is order of 8 so i have 9 of them:
Filter is basic low pass filter designed with fir1 function,cutoff frequency of 800Hz,where sample frequency is 44100Hz.
C++ code is next:
AudioBuffer output = buffer;
const int N = 8; //filter order
for (int channel = 0; channel < getTotalNumInputChannels(); ++channel)
{
output.setSample(channel, 0, filterCoefs[0] * buffer.getSample(channel, 0));
for (int n = 1; n < numSamples; n++)
{
if ((n - N) <= 0) {
double tempSample = 0;
for (int k = 0; k <= n; k++)
tempSample = tempSample + filterCoefs[k] * buffer.getSample(channel, n - k);
output.setSample(channel, n, tempSample);
}
else {
double tempSample = 0;
for (int k = 0; k <= N; k++)
tempSample = tempSample + filterCoefs[k] * buffer.getSample(channel, n - k);
output.setSample(channel, n, tempSample);
}
}
}
buffer = output;
Where buffer is the audio buffer I have in processBlock method of my processor class.
Is there something wrong with that C++ implementation or this whole way of realizing FIR filtering in C++ is wrong way to do that.
Best regards,Cila.
P.S. I have tried with more different coefficients and signals,I never get good results.
Thank you,I will try that later when i get home,I hope I can make it work.But it would be nice if someone can help me with the code I’ve posted,it is driving me crazy.
Hi, sorry to hijack your thread - I’m also attempting to use the FIR methods with my own filter coefficients, but I can’t work out how to use an array of doubles as the coefficients. Would you mind sharing the relevant part of your code? I’m not having much luck with the snippest T4GG gave.
Hello back. I have not forgot about you. Here it is my simple implementation of FIR filtering.
First I have defined some filter coefficients in header file of processor and delay line in length of number of coefficients.
After that, in cpp file in processBlock method: AudioBuffer<FloatType> output = buffer;
for (int channel = 0; channel < getTotalNumInputChannels(); ++channel)
{
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 = 7; i >= 0; i--) { // delayLine being 9 values long
delayLine[i + 1] = delayLine[i];
}
delayLine[0] = buffer.getSample(channel, sample);
result = 0;
for (int i = 0; i < 9; i++) { // for each tap
result = result + delayLine[i] * filterCoefs[i]; // multiply
}
output.setSample(channel, sample, result); // output
}
}
buffer = output;
}
Clearly it works for 9 coefficients only but it can easily be changed to different one, you will figure how easily. Try if that works and suites your needs, and if someone has some remarks please do say.