# Can't get consistent magnitude response in MATLAB

I have designed a notch filter with MATLAB filter designer app. Then I generated the filter design function which gives me the filter object:

function Hd = filter_notch

Fs=500; Fnotch=50; BW=1; Apass=1;

[b, a] = iirnotch(Fnotch/(Fs/2), BW/(Fs/2), Apass);
Hd     = dfilt.df2(b, a);


Now, I use freqz() to plot the response. However I get 2 different responses.

Mehtod 1. Direct plot with freqz(filter_object)

>> freqz(filter_notch);


Method 2. Manual plot with h=freqz(filter_object, Physical_frequencies, F_sample)

>> fs=500;
>> f_range=linspace(0,fs/2,2048);
>> h=freqz(filter_notch,f_range,fs);
>> hdb=20*log10(abs(h));
>> plot(f_range,hdb)


you can see the notch attenuations are different with above 2 methods, roughly -25dB vs -15dB. Why is that?

From MATLAB documents:

 h = freqz(___,f,fs) returns the frequency response vector, h, at the
physical frequencies supplied in f.

• It's not obvious from the first screenshot that the blue plot (magnitude) is extending down to -25 dB since it's obscured by the phase plot. Aug 22, 2017 at 22:22
• @jaket Good point, but I have verified that. It's -25dB. Attached the new shot to question. Aug 22, 2017 at 22:37
• also apply this figure,freqz(b,a,4096); to see in detail where the notch dip is actually going: use the last parameter as large as necessary to get a sample from the notch dip as close as possible... Aug 22, 2017 at 23:52
• @Fat32 Same -25dB Aug 23, 2017 at 0:01
• then its your second plot which is problematic. try configuring its parameters from its documentation etc... Aug 23, 2017 at 0:17

As already mentioned by @Fat32 and @Florent Ecochard what your observing is a resolution issue with the produced plots. By default (e.g. using freqz without additional input arguments) 8192 points are used to plot the filter response; using less points, as done in you method 2, results in the observed behavior.

See the example code and figures below for an illustration:

%% Define Notch Filter
%
Fs     = 500;
Fnotch =  50;
BW     =   1;
Apass  =   1;

[b, a] = iirnotch(Fnotch/(Fs/2), BW/(Fs/2), Apass);
Hd     = dfilt.df2(b, a);

%% Plot Response

% method 1
% -> FREQZ default settings
%
[H1,F1] = freqz(Hd);
F1      = F1 * (Fs/(2*pi));  % convert to Hz / account for frequency normalization

% method 2
% -> Use frequency axis of 2048 points.
%
fs      = 500;
f_range = linspace(0,fs/2,2048);

[H2,F2] = freqz(Hd,f_range,fs);

% method 3
% -> Use frequency axis of 8192 points (same number as used in example 1).
%
fs      = 500;
f_range = linspace(0,fs/2,8192);

[H3,F3] = freqz(Hd,f_range,fs);

figure;
plot(F1,20*log10(abs(H1)),...
F2,20*log10(abs(H2)),...
F3,20*log10(abs(H3)) ...
)
xlabel('f / Hz')
ylabel('|Hd| / dB')
legend('method 1 (freqz default)','method 2 (2048 points)','method 3 (8192 points)')


This then results in the following plots:

• Thank you. This is a lot of difference... so asymptotically, the notch attenuation is infinite, am I right? Aug 24, 2017 at 18:02
• Yes, if you evaluate the filter response around Fnotch (e.g. using [H,F] = freqz(Hd,[Fnotch-1 Fnotch Fnotch+1],Fs)) you will find that it is equal to zero at Fnotch. The difference in the plots is purely a plotting problem; if you select the frequency axes to include Fnotch (and given that you use enough points) the plots will look the same. Try changing the number of points to 2001, 8001, 10001 points etc. to see what happens. Aug 24, 2017 at 19:47
• Very very insightful. But if the signal I am going to filter with this notch has say 500 samples, the filter would act like a 500 point filter, correct? Aug 24, 2017 at 20:11
• No, filter length and signal length are not related. As this is an IIR filter it (theoretically) has infinite length; to determine the effective impulse response length you can use the 'impzlength' command. The length of the filtered signal will be 500 samples irrespective of the filter impulse response length. Aug 24, 2017 at 20:25