# Applying a lowpass filter to a square wave

34 views (last 30 days)
Oscar Kilby on 25 Dec 2020
Answered: Rohit Pappu on 31 Dec 2020
Hello all,
I'm trying to apply a lowpass filter with the gain and phase responses given in the screenshots below to a square wave with frequency of 1khz however im not sure im going about it correctly (code is pasted below). Could someone clarify how filtering works in matlab and how it is possible to implement the conditions shown in the screenshots (especially with regards to the phase response)?
For reference ive been basing my current filter code off this article however im fairly sure i havent accounted for phase response and im not really sure how to. T = 0.001; %sets f0 to 1000hz/1khz
w = 2*pi*(1/T);
%filter parameters
fs = 1/T ;
fpass = 10000;
t = linspace(0 , 3*T, 1000);
max_harmonics = 20; %sets maximum number of sine terms
synth_VT = zeros(size(t));
for n = 1:2:max_harmonics
synth_VT = synth_VT + (3/pi)*((1/n)*sin(n*w*t)) ;
end
filtered_synth_VT = zeros(size(t));
filtered_synth_VT = lowpass(synth_VT, fpass, fs);
plot(t, synth_VT, 'r-', t, filtered_synth_VT, 'b-');

Rohit Pappu on 31 Dec 2020
For implementing an ideal low pass filter, you can refer to this link