Question: I've the following matlab code written by myself, for the low pass filter and the input of x ( t ) . How to get

I've the following matlab code written by myself, for the low pass filter and the input of x(t). How to get the output, in both time domain and frequency domain, in plot? If possible, how to get the frequency domain of the x(t)? How to get the function of the output in both time domain and frequency domain? All by Matlab. Will upvote if good answer, and vice versa, thank you.
% Parameters
f0=20e3; % Fundamental frequency (Hz)
T0=1/ f0;
%How to convert to fundamental angular frequency (rad/s)?
omega0=2* pi * f0;
%Define the x-axis,
t =-2*T0 : 1e-6 : 2*T0; % Time vector for 5 periods
%The dc component was obtained from manual solution
DC_Component =1/2;
% Initialize the signal
%------------------------5 harmonics------------------------------------
AC_Component = zeros(size(t));
%Since n =1,3,5,7,...(Odd Harmonics) until the number of harmonics wanted
%Therefore, Loop from 1 to X, with increment of 2 has syntax as shown
for n =1:2:5
%The information below was copied directly from manual solution
bn =2/(n * pi);
AC_Component = AC_Component + bn * sin(n * omega0* t);
end
% Add the DC component here, because DC component is constant, and don't
% need to be looped! Only the AC component is looped
x_t = DC_Component + AC_Component;
figure; %This "figure" use only once, so that all subplots appear in 1 fugure
subplot(3,1,1)
%How to let the line to be blue, and line width to be 1.5? so that it
%is beautiful?
plot(t, x_t,'r', 'LineWidth', 1.5);
title('Fourier Series Approximation of x(t)');
xlabel('Time (s)');
ylabel('Amplitude');
%Since I want to show the y axis from -1.25 to 1.25, then following
%syntax is used, because I want y=0 to be at center exactly like
%the question given, it's put here not infront of "plot" or it
%won't work
ylim([-1.251.25]);
%------------------------10 harmonics------------------------------------
AC_Component = zeros(size(t));
%Since n =1,3,5,7,...(Odd Harmonics) until the number of harmonics wanted
%Therefore, Loop from 1 to X, with increment of 2 has syntax as shown
for n =1:2:10
%The information below was copied directly from manual solution
bn =2/(n * pi);
AC_Component = AC_Component + bn * sin(n * omega0* t);
end
% Add the DC component here, because DC component is constant, and don't
% need to be looped! Only the AC component is looped
x_t = DC_Component + AC_Component;
subplot(3,1,2)
plot(t, x_t,'g', 'LineWidth', 1.5);
title('Fourier Series Approximation of x(t)');
xlabel('Time (s)');
ylabel('Amplitude');
ylim([-1.251.25]);
%------------------------20 harmonics------------------------------------
AC_Component = zeros(size(t));
%Since n =1,3,5,7,...(Odd Harmonics) until the number of harmonics wanted
%Therefore, Loop from 1 to X, with increment of 2 has syntax as shown
for n =1:2:20
%The information below was copied directly from manual solution
bn =2/(n * pi);
AC_Component = AC_Component + bn * sin(n * omega0* t);
end
% Add the DC component here, because DC component is constant, and don't
% need to be looped! Only the AC component is looped
x_t = DC_Component + AC_Component;
subplot(3,1,3)
plot(t, x_t,'b', 'LineWidth', 1.5);
title('Fourier Series Approximation of x(t)');
xlabel('Time (s)');
ylabel('Amplitude');
ylim([-1.251.25]);
%--------Plotting the Low Pass Filter's Frequency Response Curve----------
% The Cutoff frequency given, in Hz
fc =480e3;
f=0: 10e3 : 1200e3%This really need to trial and error to get the smooth curve
%Inputting the transfer function derived manually,
%Input log10 instead of log10, it produces different result
gain =20*log10(1./(sqrt(1+(f.^2/fc.^2))))
figure;
plot(f, gain);
title('Frequency Response Curve of Low Pass Filter, of f_c =480e3');
xlabel('Frequency (kHz)');
ylabel('Gain (dB)');
grid on;
%--------------------------------------------------------------------------
%Since the fc should give a gain of -3 dB theoretically
%To verify this, fc and -3dB line was plotted
% How to make matlab to display the gain, and fc calculated?
%.6f means 6 floating decimal,
%"%" is where the output would be
%before /n means the unit, so that i don't need to type units
xline(fc,'--r', 'Cutoff Frequency (f_c)', 'LabelVerticalAlignment', 'bottom');
yline(-3,'--r','-3 dB', 'LabelHorizontalAlignment' ,'left');
%Not used: below
%fprintf('Cutoff frequency (fc): %.2f Hz
', fc);
%fprintf('Time constant (tau): %.6f seconds
', tau);
%--------------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Electrical Engineering Questions!