Filtering Square Wave with Butterworth Filter using Fourier Coefficients
Hello there, I’m trying to filter a square wave with a 2nd order butterworth HPF in order to retrieve the fundamental sine wave with minimal attenuation. When I run my code to display the magnitude spectrum of the input and output coefficients, there is an attenuation of about 1.7dB. Yet when I reconstruct the signal, the resulting sine wave appears to be attenuated much more (about half the size of the input, about 6dB). This leads me to believe there is an issue with the second for loop but I am unable to find it. Any help would be much appreciated.
clear; clc;
T = 0.1; %in ms
F = 1/T; %in kHz
Fs = 1000;
offset = 0;
amp = 1;
duty = 50; %percent duty cycle
t = 0:0.001:0.5;
fc = 12; % in kHz
sq_wave = offset+amp*square(2*pi*F.*t,duty); %general square wave function
N=10; %no. of harmonics
nvec = -N:N;
c_in = zeros(size(nvec));
f = nvec*F;
for n = nvec
m = n+N+1;
c_in(m) = (1/2)*((sin(pi*n/2))/(pi*n/2)); %sinc function
if (n == 0)
c_in(m) = 0.0; %DC Offset
end
end
w = f/fc;
Hf = 1 ./ ((w*1i).^2 + 1.414*(w*1i) + 1);
c_out = c_in .* Hf;
stem(f,abs(c_in),’r’,’LineWidth’,1.5);
hold on
stem(f,abs(c_out),’b’,’LineWidth’,1.5);
hold off
title(‘Magnitude Spectrum of Filter Output and Input’)
A = zeros(2*N+1,length(t));
for n = nvec
m=n+N+1;
A(m,:) = c_out(m) .* exp(1i*2*pi*n*F.*t);
end
sine_wave = sum(A);
%plot(t,real(sine_wave),’b’,t,sq_wave,’r’);Hello there, I’m trying to filter a square wave with a 2nd order butterworth HPF in order to retrieve the fundamental sine wave with minimal attenuation. When I run my code to display the magnitude spectrum of the input and output coefficients, there is an attenuation of about 1.7dB. Yet when I reconstruct the signal, the resulting sine wave appears to be attenuated much more (about half the size of the input, about 6dB). This leads me to believe there is an issue with the second for loop but I am unable to find it. Any help would be much appreciated.
clear; clc;
T = 0.1; %in ms
F = 1/T; %in kHz
Fs = 1000;
offset = 0;
amp = 1;
duty = 50; %percent duty cycle
t = 0:0.001:0.5;
fc = 12; % in kHz
sq_wave = offset+amp*square(2*pi*F.*t,duty); %general square wave function
N=10; %no. of harmonics
nvec = -N:N;
c_in = zeros(size(nvec));
f = nvec*F;
for n = nvec
m = n+N+1;
c_in(m) = (1/2)*((sin(pi*n/2))/(pi*n/2)); %sinc function
if (n == 0)
c_in(m) = 0.0; %DC Offset
end
end
w = f/fc;
Hf = 1 ./ ((w*1i).^2 + 1.414*(w*1i) + 1);
c_out = c_in .* Hf;
stem(f,abs(c_in),’r’,’LineWidth’,1.5);
hold on
stem(f,abs(c_out),’b’,’LineWidth’,1.5);
hold off
title(‘Magnitude Spectrum of Filter Output and Input’)
A = zeros(2*N+1,length(t));
for n = nvec
m=n+N+1;
A(m,:) = c_out(m) .* exp(1i*2*pi*n*F.*t);
end
sine_wave = sum(A);
%plot(t,real(sine_wave),’b’,t,sq_wave,’r’); Hello there, I’m trying to filter a square wave with a 2nd order butterworth HPF in order to retrieve the fundamental sine wave with minimal attenuation. When I run my code to display the magnitude spectrum of the input and output coefficients, there is an attenuation of about 1.7dB. Yet when I reconstruct the signal, the resulting sine wave appears to be attenuated much more (about half the size of the input, about 6dB). This leads me to believe there is an issue with the second for loop but I am unable to find it. Any help would be much appreciated.
clear; clc;
T = 0.1; %in ms
F = 1/T; %in kHz
Fs = 1000;
offset = 0;
amp = 1;
duty = 50; %percent duty cycle
t = 0:0.001:0.5;
fc = 12; % in kHz
sq_wave = offset+amp*square(2*pi*F.*t,duty); %general square wave function
N=10; %no. of harmonics
nvec = -N:N;
c_in = zeros(size(nvec));
f = nvec*F;
for n = nvec
m = n+N+1;
c_in(m) = (1/2)*((sin(pi*n/2))/(pi*n/2)); %sinc function
if (n == 0)
c_in(m) = 0.0; %DC Offset
end
end
w = f/fc;
Hf = 1 ./ ((w*1i).^2 + 1.414*(w*1i) + 1);
c_out = c_in .* Hf;
stem(f,abs(c_in),’r’,’LineWidth’,1.5);
hold on
stem(f,abs(c_out),’b’,’LineWidth’,1.5);
hold off
title(‘Magnitude Spectrum of Filter Output and Input’)
A = zeros(2*N+1,length(t));
for n = nvec
m=n+N+1;
A(m,:) = c_out(m) .* exp(1i*2*pi*n*F.*t);
end
sine_wave = sum(A);
%plot(t,real(sine_wave),’b’,t,sq_wave,’r’); fourier series, signal processing, dsp, fourier coefficients MATLAB Answers — New Questions