2021_MC_42_Lab 10
2021_MC_42_Lab 10
CLO1: USE MODERN TOOL (E.G. MATLAB) FOR SIGNAL REPRESENTATION, VISUALIZATION
AND PROCESSING IN BOTH TIME AND FREQUENCY DOMAIN.
1
For the above signal Fourier co-efficient are calculated in the book as:
𝑎0 =
2
𝑎 𝑛
= 𝑛 sin ( ) and 𝑛 = 0
2 𝜋
𝑛
𝜋 2
There are infinite co-efficient for the above signal. But you can add finite sinusoids to get the signal
which will be somewhat different depending upon number of co-efficient you are selecting.
Task 1:
Create a MATLAB function LS10_1 which will take number of co-efficient as input from the user
and will plot the original signal and approximate signal (by adding sinusoids) on the same axis (not
using subplot) and with different colors. The range should be from -4𝜋 to 4𝜋.
The original square wave can be plotted using the MATLAB command ‘square’. See help to find
further detail.
Take number of co-efficient from user. Use a for loop to calculate 𝑎𝑛 from 1 to user’s entered input.
Also calculate cos 𝑛𝜔𝑜𝑡 (𝜔𝑜 is 1 in this case) and keep on adding them within the same loop. At
the end of the loop add 𝑎0 and this will be the approximate signal of the above signal.
Write down the code of the function and paste the outputs for three different number of co-efficient
e.g. 9, 80 and 900.
function LS10_1(n_coefficients)
t = linspace(-4*pi, 4*pi, 1000);
a0 = 1/2;
w0 = 1; % fundamental frequency
y = a0;
for n = 1:n_coefficients
an = (2/(n*pi)) * sin(n*pi/2);
y = y + an * cos(n*w0*t);
end
figure;
hold on;
plot(t, square(t+1.5), 'r', 'LineWidth', 2); % Original signal
plot(t, y, 'b', 'LineWidth', 2); % Approximate signal
legend('Original Signal', 'Approximate Signal');
title('Fourier Series Approximation');
xlabel('Time');
ylabel('Amplitude');
grid on;
hold off;
end