I'm trying to implement a 10th-order Butterworth Low Pass digital filter on my MCU.
To find the coefficients I used Matlab's Filter Design & Analisys Tool. So I create two little scripts: in the first of them I used the Filter Object created by the tool and it seems to works perfectly, but in the second one I tryed to implement the general code based to the Biquad Direct-Form-II using only the coefficients generated by the tool and, in this case, the filter works good except for a signal amplification of about 5E+7!
My code in matlab is the following:
input = AmplitudeF8.';
delayline_lp = [0,0,0,0,0,0,0,0,0,0];
coeff_lp = [-1.7582972612745587, 0.89109671836321969, 2, 1, -1.5932765493727838, 0.71361243648860972, 2, 1, -1.4754804435926461, 0.58691950806119031, 2, 1, -1.4002643388507841, 0.50602253348347193, 2, 1, -1.3637162169443289, 0.46671403035270148, 2, 1];
for k=1:length(output1)
y = output1(k);
for j=1:5 %the biquad sections (order 10 = num_sections = 5)
w = y;
for i=1:2
w = w - (coeff_lp(((j - 1) * 4) + i) * delayline_lp(((j - 1) * 2) + i));
end
y = w;
for i=1:2
y = y + (coeff_lp(((j - 1) * 4) + i + 2) * delayline_lp(((j - 1) * 2) + i));
end
delayline_lp(j * 2) = delayline_lp((j * 2) - 1);
delayline_lp((j * 2) - 1) = w;
end
output2(k) = y;
end
Consider that in the coeff_lp array there are a1,a2,b1,b2,…. since a0=b0=1 for every biquad section.
Why in this case the filtered signal is amplified by a 5E+7 factor? Where I'm wrong?
Best Answer
I found the mistake in my code:
when the Matlab's Filter Design & Analisys Tool generates the cofficients, there are even the Gains for the every biquad section. Before the value goes out from one section, it must be multiplied with this gain.
So the right code is the following:
Then the C-Code for my MCU is (maybe is usefull for someone):
and now it works very well.