Plotting a filter as a function of cyclic frequency using freqz()

16 Ansichten (letzte 30 Tage)
Hello,
I'm trying to graph a moving average filter's magnitude response as a function of cyclic frequency. However, when I use freqz(), my plots do not look correct. Here's my code currently:
clear;close all;
fs = 200; %sampling frequency
Ts = 1/200; %sampling time
t = 0:Ts:1;
x = sin(2*pi*2*t) + sin(2*pi*10*t) + sin(2*pi*90*t); %signal
figure(1)
plot(t,x); %unfiltered signal
title('Signal with 2, 10, and 90 Hz');
xlabel('time (s)');
%Difference equation of Hanning Moving Average filter:
yn = zeros(size(t));
ind = 3:length(t);
yn(ind) = (0.25*x(ind)) + (0.5*x(ind-1)) + (0.25*x(ind-2));
%Attempt at creating the moving average equation.
figure(2)
plot(t,yn)
title("Hanning Moving Average")
%Me just plotting the filtered Hanning Moving Average plot with
%freqz():
figure(10)
freqz(yn)
Both the magnitude (dB) and the phase seem to show a lot of sound, but the plot of figure 2 does not. Any help will be appreciated.

Akzeptierte Antwort

Star Strider
Star Strider am 8 Mai 2021
Your approach is correct. The reason the freqz plot did not look correct is that you were passing the filtered signal to it, not the filter coefficients themselves. This approach creates the filter separately, then uses the filter function to filter the signal (since that is essentially what the original ‘yn’ did). The only significant difference is that, and passing the filter coefficients to freqz.
Try this —
fs = 200; %sampling frequency
Ts = 1/200; %sampling time
t = 0:Ts:1;
x = sin(2*pi*2*t) + sin(2*pi*10*t) + sin(2*pi*90*t); %signal
figure(1)
plot(t,x); %unfiltered signal
title('Signal with 2, 10, and 90 Hz');
xlabel('time (s)');
%Difference equation of Hanning Moving Average filter:
yn = zeros(size(t));
ind = 3:length(t);
y = [0.25 0.5 0.25]; % Separate Vector Of Filter Coefficients
yn = filter(y, sum(y), x); % Filter Signal
%Attempt at creating the moving average equation.
figure(2)
plot(t,yn)
title("Hanning Moving Average")
%Me just plotting the filtered Hanning Moving Average plot with
%freqz():
figure(10)
freqz(y,1, 2^16, fs)
The freqz plot now looks correct, displaying the Bode plot of a lowpass filter with a cutoff of about 50 Hz.
In order to see that in detail, after the freqz plot, temporaily add:
set(subplot(2,1,1),'XLim',[0 60])
set(subplot(2,1,2),'XLim',[0 60])
in order to see the -6 dB frequency (at 50 Hz), denoting the filter stopband.
  3 Kommentare
Terry Carney
Terry Carney am 8 Mai 2021
Nevermind, I see it. Thanks again.
Star Strider
Star Strider am 8 Mai 2021
As always, my pleasure!
It’s the length of the fft. (A longer fft adds detail.)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by