Matlab function freqz gives wrong amplitude in dB
Show older comments
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.
1 Comment
hans petersen
on 2 May 2022
Answers (2)
Star Strider
on 2 May 2022
0 votes
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)'})
Consider doing this same sort of analysis with the filterDesigner results. I suspect they will be the same.
.
hans petersen
on 4 May 2022
Star Strider
on 4 May 2022
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.
.
hans petersen
on 4 May 2022
Edited: hans petersen
on 4 May 2022
Star Strider
on 4 May 2022
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.
hans petersen
on 6 May 2022
Star Strider
on 6 May 2022
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.
hans petersen
on 10 May 2022
Star Strider
on 10 May 2022
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.
.
hans petersen
on 3 May 2022
0 votes
Categories
Find more on Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

