Matlab function freqz gives wrong amplitude in dB

Hello guys,
i want to compute und plot the filter amplitude response and want to compare it with the filterDesigner function from Matlab.
For that i use this Code:
fs = 1200; Ts = 1/fs;
filterCoff = Filter1, %% FIR lOWPASS Filter
%% Plot of the amplitude response in db
figure(1);
plot(w/(2*pi*Ts),mag2db(abs(h)));
xlabel('Normalized Frequency (Hz)')
ylabel('Magnitude');
I tried everythink, but i dont get the same solution as the filterDesigner.

Answers (2)

I do not see any call to freqz, or the designed filter, so I cannot test this.
What you are seeing may be a frequency-axis resolution problem. The freqz function has the option of choosing the length of the Fourier transform used to plot the Bode plot as the second (or third argument, depending on how the filter is defined), and I usually choose 2^16 for this. A higher value results in a more accurate plot with increased frequency resoution.
If the highest frequency in the passband is not the first frequency, the freqz function does not always scale the amplitude axis so that the highest amplitude is always 0 dB, so it may seem as though the filter is amplifying even though it is not. The relative amplitudes are nevertheless correct.
For freqz, also using:
set(subplot(2,1,1), 'YLim',[-130 0])
(in this instance) may make the plots more comparable by improving the amplitude resolution.
.

9 Comments

I do not see the problem —
Filter1 = [0.000178232054708589,
0.000709878618964491,
0.001585843072655027,
0.002791137421116118,
0.004305138743876396,
0.006101942058544247,
0.008150803562238728,
0.010416666666580653,
0.012860761825639317,
0.015441269893612304,
0.018114037662004680,
0.020833333333267912,
0.023552629004535204,
0.026225396772939561,
0.028805904840931838,
0.031250000000016098,
0.033515863104388588,
0.035564724608116896,
0.037361527922819888,
0.038875529245614424,
0.040080823594106516,
0.040956788047822303,
0.041488434612095185,
0.041666666666809973,
0.041488434612095185,
0.040956788047822303,
0.040080823594106516,
0.038875529245614424,
0.037361527922819888,
0.035564724608116896,
0.033515863104388588,
0.031250000000016098,
0.028805904840931838,
0.026225396772939561,
0.023552629004535204,
0.020833333333267912,
0.018114037662004680,
0.015441269893612304,
0.012860761825639317,
0.010416666666580653,
0.008150803562238728,
0.006101942058544247,
0.004305138743876396,
0.002791137421116118,
0.001585843072655027,
0.000709878618964491,
0.000178232054708589];
fs = 1200; Ts = 1/fs;
filterCoeff = Filter1;
[h,w] = freqz(filterCoeff,1, 2^16, fs);
figure(1);
plot(w,mag2db(abs(h)));
xlabel('Normalized Frequency (Hz)');
ylabel('Magnitude (dB)');
grid
ylim([-130 0])
[pks, locs] = findpeaks(mag2db(abs(h)));
Results = table(w(locs),pks, 'VariableNames',{'Freq (Hz)','Amplitude (dB)'})
Results = 22×2 table
Freq (Hz) Amplitude (dB) _________ ______________ 59.052 -31.468 85.199 -41.48 110.75 -48.489 136.08 -53.944 161.31 -58.432 186.47 -62.254 211.59 -65.591 236.68 -68.561 261.76 -71.248 286.81 -73.711 311.86 -76.001 336.89 -78.157 361.91 -80.216 386.92 -82.213 411.92 -84.188 436.91 -86.186
Consider doing this same sort of analysis with the filterDesigner results. I suspect they will be the same.
.
Thank you for your help. But if you compare the graphs you see the differences.
For example at 100 Hz you see that the line in the filterDesigner ends between -80 and -100 db.
But in your plot the graph ends between -100 and - 120 db.
Why is there such a gap?
That is likely just the way the plots are drawn, and that has to do with the frequency resolution of the magnitude plot, and that is determined by the length of the Fourier transform. Higher frequency resolution will yield a more precise plot. You can experiment with that with respect to freqz as:
[h1,w1] = freqz(filterCoeff,1, 2^8, fs);
[h2,w2] = freqz(filterCoeff,1, 2^12, fs);
[h3,w3] = freqz(filterCoeff,1, 2^16, fs);
to see how the plots differ.
.
Okey with [h,w] = freqz(filterCoeff,1, 2^13, fs); i get a similar solution as the filterDesigner. But wich solution is right? The one with the highest resolution?
They are both correct. The only difference is the frequency resolution in the plots, and that has nothing to do with the actual filter performance.
The actual performance of the filter is important only in the context of the result of the signal filtered with it.
I am trying to understand this. But if the frequency resolution increases, shouldn't the absolute value of the gain be almost identical?
The values at any specific frequency will always be the same (within floating-point precision limits). The only difference is in the display and the specific frequencies that are presented in it. That is determined by the length of the fft used to calculate it.
The plot is simply for information to determine that the filter design matches the requirements, and does not at all affect the underlying filter values or how the filter will operate with respect to a specific signal.
First of all thank you for your help. I think i understood. I plot the same filter with 2^12 and 2^16 and the plots are the same. The only Difference is in the length of the vertikal lines.
Is this explanation correct?
The reason Why the vertical lines dont have the same length is just a mathematical issue. Somethink like rounding and decimal point operations. For The performance of the digital filter we have to look on the high points of the individual parabolas.
My pleasure.
Is this explanation correct?
Essentially, yes. The difference is in the fft frequency resolution and therefore the plot resolution. The underlying data is the same.
For The performance of the digital filter we have to look on the high points of the individual parabolas.’
The entire filter is important with respect to the actual filtered signal. The filter is not linear in the stopband, however the variations do not matter because the stopband frequencies are significantly attenuated.
.

Sign in to comment.

Hi,
sorry. This is the whole Code:
fs = 1200; Ts = 1/fs;
filterCoeff = Filter1
[h,w] = freqz(filterCoeff,1);
figure(1);
plot(w/(2*pi*Ts),mag2db(abs(h)));
xlabel('Normalized Frequency (Hz)');
ylabel('Magnitude (dB)');
% The FIR Filter Coefficient:
Filter1 = [
0.000178232054708589,
0.000709878618964491,
0.001585843072655027,
0.002791137421116118,
0.004305138743876396,
0.006101942058544247,
0.008150803562238728,
0.010416666666580653,
0.012860761825639317,
0.015441269893612304,
0.018114037662004680,
0.020833333333267912,
0.023552629004535204,
0.026225396772939561,
0.028805904840931838,
0.031250000000016098,
0.033515863104388588,
0.035564724608116896,
0.037361527922819888,
0.038875529245614424,
0.040080823594106516,
0.040956788047822303,
0.041488434612095185,
0.041666666666809973,
0.041488434612095185,
0.040956788047822303,
0.040080823594106516,
0.038875529245614424,
0.037361527922819888,
0.035564724608116896,
0.033515863104388588,
0.031250000000016098,
0.028805904840931838,
0.026225396772939561,
0.023552629004535204,
0.020833333333267912,
0.018114037662004680,
0.015441269893612304,
0.012860761825639317,
0.010416666666580653,
0.008150803562238728,
0.006101942058544247,
0.004305138743876396,
0.002791137421116118,
0.001585843072655027,
0.000709878618964491,
0.000178232054708589
];
With this Code my frequencies are near to the frequencies of the filterDesigner Tool, but my Amplitude is absolutely different.
Thank you for your help.

Products

Release

R2022a

Asked:

on 2 May 2022

Commented:

on 10 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!