0% found this document useful (0 votes)
170 views15 pages

%% PART 1 % N Input ('Desired Number of Plot Points ')

This document provides instructions for laboratory exercises on Fourier series approximation using MATLAB. It includes 3 parts: 1. Approximating a square wave function using Fourier sine series for different values of N (number of plot points) and M (number of Fourier coefficients) to analyze accuracy. 2. Approximating a line function using Fourier sine series for different values of M to compare the outputs. 3. Modifying the code from part 1 to approximate a periodic square wave of period 2 seconds defined over one period. The tasks involve running the MATLAB scripts provided, recording results, and answering questions about the effects of varying N and M on the accuracy of approximations. Screenshots and graphs are to be past

Uploaded by

yelyah serious
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
170 views15 pages

%% PART 1 % N Input ('Desired Number of Plot Points ')

This document provides instructions for laboratory exercises on Fourier series approximation using MATLAB. It includes 3 parts: 1. Approximating a square wave function using Fourier sine series for different values of N (number of plot points) and M (number of Fourier coefficients) to analyze accuracy. 2. Approximating a line function using Fourier sine series for different values of M to compare the outputs. 3. Modifying the code from part 1 to approximate a periodic square wave of period 2 seconds defined over one period. The tasks involve running the MATLAB scripts provided, recording results, and answering questions about the effects of varying N and M on the accuracy of approximations. Screenshots and graphs are to be past

Uploaded by

yelyah serious
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Signals Spectra and Signal Processing

Laboratory Exercise 5
Fourier Series

Objective

Plot signal sequence of fourier series

Material
MATLAB r2020a or any version

Procedures
30. Run the matlab application and open an Editor (ctrl + N)

31. This is for approximating the square wave function using fourier sine series

%% PART 1
% Square Wave Function
N = input(’Desired number of plot points = ’);
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = input(‘Number of fourier coefficients = ’);
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, 'r')
hold on
plot(x,f,'LineWidth',2)
hold on
error = abs(sum-F)
Plot(x, error);

32. Save the script from editor with .m file extension


33. Run the script (ctrl + Enter) and study the output.
34. Write the following scripts. This is for Fourier Approximation of a Line

%% Part 2
% Line Approximated
% MATLAB Code for Fourier Sine Series, Line, and Error
clf;
x = linspace(0,2*pi,100);
sum = 0.*x;
M = input(‘Number of fourier coefficients = ’);
% try M = 4
for j = 1:M
sum = sum + ((1/j)*sin(j*x));
end
F = ((1/2)*(pi-x));
plot(x, sum,’r’);
hold on
plot(x, F, ’LineWidth’, 2);
hold on
error = abs(sum - F);
plot(x, error,’m’)
hold on

35. Save the script from editor with .m file extension


36. Run the script (or ctrl + Enter) and study the output.

Task and Critical Thinking Questions:

Perform the following tasks and record the answers. Provide the screenshots of the scripts used, results,
and workspace, and graph. You can use matlab for pc or matlab for android.
Answer the questions after performing the task
Use minimum command lines and number of scripts as possible.

6. Run Program of Part 1 for other values of N and M. By changing N, the number of points
plotted, and M, the number of coefficients, the accuracy of the approximation changes. What
values of N and M gives the most accurate approximation? Which values give most errors?
Compare the graphs of your results
7. Part 2: Record the function and it’s approximation for the values of M, at M = 10 , M = 50, M =
100 and M = 1000. Compare the output for the given M values.

8. Modify the program of Part 1 to approximate the fourier series for a periodic square wave g(t)
whose period is 2 seconds described analytically over one period as follow:

−1, − 1 ≤ ≤ 0
g(t) =
1, 0≤ ≤1

Paste the screenshots of your answers on a ms word document with the answers to the questions. Use the
format below
LABORATORY EXERCISE NO. 5
___________TITLE__________

Name : ___________________________________________
Section: _______________________________
Task and Critical Thinking Questions
8.

Explanation/answer:______________________________________

9.

10.
LABORATORY EXERCISE NO. 5
FOURIER SERIES

Name : Soriano,
Gallano,JohnEdwin
Carlo D.A._________
Section: _______________________________

TASK ANSWERS:
TASK 1:
M=5
%% PART 1
% Square Wave Function
N = 500;
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = 5;
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, 'r')
hold on
plot(x,f,'LineWidth',2)
hold on
error = abs(sum-f)
plot(x, error);
M=39
%% PART 1
% Square Wave Function
N = 500;
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = 39;
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, 'r')
hold on
plot(x,f,'LineWidth',2)
hold on
error = abs(sum-f)
Plot(x, error);
M-100
%% PART 1
% Square Wave Function
N = 500;
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = 100;
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, 'r')
hold on
plot(x,f,'LineWidth',2)
hold on
error = abs(sum-f)
Plot(x, error);
M=645
%% PART 1
% Square Wave Function
N = 500;
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = 645;
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, 'r')
hold on
plot(x,f,'LineWidth',2)
hold on
error = abs(sum-f)
Plot(x, error);
M=3000
%% PART 1
% Square Wave Function
N = 500;
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = 3000;
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
plot(x, sum, 'r')
hold on
plot(x,f,'LineWidth',2)
hold on
error = abs(sum-f)
Plot(x, error);
Answers for task 1:
What values of N and M gives the most accurate approximation?
500 and 3000 are the values of N and M that gives the most accurate approximation.

Which values give most errors?


39 100 645 are the values that give most of the errors.

TASK 2
M=10
%% Part 2
% Line Approximated
% MATLAB Code for Fourier Sine Series, Line, and Error
clf;
x = linspace(0,2*pi,100);
sum = 0.*x;
M = 10;
% try M = 4
for j = 1:M
sum = sum + ((1/j)*sin(j*x));
end
F = ((1/2)*(pi-x));
plot(x, sum,'r');
hold on
plot(x, F, 'LineWidth', 2);
hold on
error = abs(sum - F);
plot(x, error,'m')
hold on

%% Part 2
% Line Approximated
% MATLAB Code for Fourier Sine Series, Line, and Error
clf;
x = linspace(0,2*pi,100);
sum = 0.*x;
M = 50;
% try M = 4
for j = 1:M
sum = sum + ((1/j)*sin(j*x));
end
F = ((1/2)*(pi-x));
plot(x, sum,'r');
hold on
plot(x, F, 'LineWidth', 2);
hold on
error = abs(sum - F);
plot(x, error,'m')
hold on
%% Part 2
% Line Approximated
% MATLAB Code for Fourier Sine Series, Line, and Error
clf;
x = linspace(0,2*pi,100);
sum = 0.*x;
M = 100;
% try M = 4
for j = 1:M
sum = sum + ((1/j)*sin(j*x));
end
F = ((1/2)*(pi-x));
plot(x, sum,'r');
hold on
plot(x, F, 'LineWidth', 2);
hold on
error = abs(sum - F);
plot(x, error,'m')
hold on
%% Part 2
% Line Approximated
% MATLAB Code for Fourier Sine Series, Line, and Error
clf;
x = linspace(0,2*pi,100);
sum = 0.*x;
M = 1000;
% try M = 4
for j = 1:M
sum = sum + ((1/j)*sin(j*x));
end
F = ((1/2)*(pi-x));
plot(x, sum,'r');
hold on
plot(x, F, 'LineWidth', 2);
hold on
error = abs(sum - F);
plot(x, error,'m')
hold on
Questions on task 2
Compare the output for the given M values.
Most of the errors are seen in M=10. As for M=50, compared to M=10, it can be seen that the error has
been reduced. As a progress, the error has been even minimized when M=100. And eventually, there are
no errors from the result when M=1000.

TASK 3
%% PART 1
% Square Wave Function
N = 500;
% for the value of N, try 10
x = linspace(-1,1,N);
f = sign(x);
sum = 0.*x;
M = 115;
% for value of M, try 10
for j = 1:2:M
sum = sum + 4/pi*sin(j*pi*x)/j;
end
clf;
plot(x,f,'LineWidth',2)
hold on
axis ([-1 1 -1.5 1.5]);

You might also like