How can I plot Low Pass Butterworth Filter's response graph when its input wave is square wave?

6 views (last 30 days)
Hi everyone,
How can I plot Low Pass Butterworth Filter's response graph when its input wave is square wave?
Could you inform me?

Accepted Answer

Star Strider
Star Strider on 5 Nov 2022
Plot it however you like.
Perhaps —
[z,p,k] = butter(5, 0.05);
[sos,g] = zp2sos(z,p,k);
t = linspace(0, 10, 1E+3);
s = sin(2*pi*t*0.75) > 0;
sf = filtfilt(sos,g,+s);
figure
plot(t, s)
hold on
plot(t,sf)
hold off
See the documentation on filtfilt for details.
.
  2 Comments
Erkam Aydar
Erkam Aydar on 5 Nov 2022
Edited: Erkam Aydar on 5 Nov 2022
Thank you for your answer Star Strider.
I want to design a butterworth filter as fallows and want to see how my filter will respond when I apply a 90 kHz square wave. I cannot do when add your code to my code?. Could you help me?
%Butterworth filter
% Sample freq = 1000 Hz
% Low Pass Filter
% 3 dB ripple at Passband
% Passband is between 0 and 40 Hz
% Rs stopband attenuation is 60 dB
% Stopband stars at 150 Hz
% Nyquist freq = 500 Hz (Sample freq/2).
Wp = 40/500; %passband edge frequency (normalized)
Ws = 150/500; %stopband edge frequency (normalized)
%[n,Wn] = buttord(Wp,Ws,Rp,Rs); % finds the minimum order,
%Rp passband ripple, Rs stopband attenuation.
[n,Wn] = buttord(Wp,Ws,3,60);
%[Z,P,K] = butter(N,Wn,'low')% design butterworth filter
%n:order,Wn:cutoff frequency
% Gives zeroes, poles and gain as a vector.
[z,p,k] = butter(n,Wn);
%Each row of the SOS matrix describes a 2nd order transfer function:
SOS = zp2sos(z,p,k);
figure
freqz(SOS,1024,1000); % Plots frequency response
figure
zplane(z,p); % Plots Z-plane zero-pole
grid on
figure
[B,A] = butter(n,Wn);
freqz(B,A)
Star Strider
Star Strider on 5 Nov 2022
As always, my pleasure!
The stated requirements are going to be difficult, because it is not possible to create a 90 kHz square wave with a 1 kHz sampling frequency. The sampling frequency has to be more that two times the square wave frequency (so >180 kHz) for this to work. For that reason, I chose a 1 MHz sampling frequency. I kept the filter specifications the same, otherwise. Because the filter passband is so low compared to the signal frequency, essentially nothing (only a D-C signal) is passed after some initial transients.
It should be straigtforward to change the sampling frequency and the square wave frequency in this code if you want to use other values (such as a 1 kHz sampling frequency and a 90 Hz square wave). It will also be necessary to change the subplot xlim values in the figure that plots the Fourier transforms.
%Butterworth filter
% Sample freq = 1000 Hz
% Low Pass Filter
% 3 dB ripple at Passband
% Passband is between 0 and 40 Hz
% Rs stopband attenuation is 60 dB
% Stopband stars at 150 Hz
% Nyquist freq = 500 Hz (Sample freq/2).
Fs = 1E+6;
Fn = Fs/2;
Wp = 40/Fn; %passband edge frequency (normalized)
Ws = 150/Fn; %stopband edge frequency (normalized)
%[n,Wn] = buttord(Wp,Ws,Rp,Rs); % finds the minimum order,
%Rp passband ripple, Rs stopband attenuation.
[n,Wn] = buttord(Wp,Ws,3,60);
%[Z,P,K] = butter(N,Wn,'low')% design butterworth filter
%n:order,Wn:cutoff frequency
% Gives zeroes, poles and gain as a vector.
[z,p,k] = butter(n,Wn);
%Each row of the SOS matrix describes a 2nd order transfer function:
[SOS,g] = zp2sos(z,p,k);
figure
freqz(SOS,2^16,Fs); % Plots frequency response
figure
zplane(z,p); % Plots Z-plane zero-pole
grid on
% figure
% [B,A] = butter(n,Wn);
% freqz(B,A)
t = linspace(0, 1E+6-1, 1E+6)/Fs;
Fsqwv = 90E+3; % Square Wave Frequency
s = sin(2*pi*t*Fsqwv) > 0;
Q = 1/t(2)
Q = 1000000
sf = filtfilt(SOS,g,+s);
figure
plot(t, s, 'DisplayName','Original Signal')
hold on
plot(t,sf, 'DisplayName','Filtered Signal')
hold off
grid
L = numel(t);
NFFT = 2^nextpow2(L);
FTsfs = fft([s(:) sf(:)]-mean([s(:) sf(:)]), NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
trfn = FTsfs(:,2) ./ FTsfs(:,1);
figure
subplot(3,1,1)
plot(Fv, abs(FTsfs(Iv,1))*2, 'DisplayName','Original Signal')
grid
legend('Location','best')
xlim([0 1.5E+5])
subplot(3,1,2)
plot(Fv, abs(FTsfs(Iv,2))*2, 'DisplayName','Filtered Signal')
grid
legend('Location','best')
xlim([0 1.5E+5])
subplot(3,1,3)
plot(Fv, abs(trfn(Iv)), 'DisplayName','Filter Transfer Function')
grid
xlim([0 1.5E+5])
legend('Location','best')
sgtitle('Fourier Transforms')
xlabel('Frequency (Hz)')
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!