Fourier series plotting and improving

close all
syms n t
T = 2;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(4*exp(-1i*w*n*t),t,0,1)+int(-4*exp(-1i*w*n*t),t,1,2));
C0 = limit(C(n),n,0); % C(0)
Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12)];
fprintf('First 13 Harmonics:\n')
disp(Harmonics)
% f(t) using Fourier Series representation
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-100,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,100);
fplot(t,f(t))
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
this my code for this assignment but i want to make it better and suitable for all cases

 Accepted Answer

Hi Faisal
Running the code:
syms n t
T = 2;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(4*exp(-1i*w*n*t),t,0,1)+int(-4*exp(-1i*w*n*t),t,1,2));
C0 = limit(C(n),n,0); % C(0)
Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12)];
%fprintf('First 13 Harmonics:\n')
%disp(Harmonics)
% f(t) using Fourier Series representation
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-100,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,100);
fplot(t,f(t),[0 5])
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
The code returns a square wave, but doesn't seem to be the signal in the problem:
the max and min are +-4, but should be +-1
the fundamental period is 4, but it should be 8.
the switch from high to low should be at t = 2, not t = 1.
I suggest you revisit the code, define T0 and T1 as variables and assign to them the given values, and then carefully rewrite C(n) using T0 and T1 as needed using the definition of x(t). Better yet, consider defining x(t) as an expression defined in the problem, and then use x(t) in expression for C(n). To that end, have a look at the function
doc rectangularPulse

12 Comments

hello paul
i'm having issues doing the changes you mentioned can you show me how?
What exactly are the issues? Perhaps updated code can be posted ....
i tried mashing solutions
clc
clear all
syms n t
T1 = 2;
T = 8;
x1 = ones(1,T1); % 1 i.e., 0<t<T1
x2 = -1*ones(1,T/2-T1); %-1 i.e., T1<t<T0/2
x = [x1 x2];
x = [x x];
tt = 0:length(x)-1;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(4*exp(-1i*w*n*t),t,0,1)+int(-4*exp(-1i*w*n*t),t,1,2));
C0 = limit(C(n),n,0); % C(0)
Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12) C(13) C(14) C(15) C(16) C(17) C(18) C(19) C(20) C(21) C(22) C(23) C(24) C(25) C(26) C(27) C(28) C(29) C(30) C(31) C(32)];
fprintf('First n Harmonics:\n')
disp(Harmonics)
% f(t) using Fourier Series representation
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-100,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,100);
fplot(t,f(t))
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
Running the new code
clc
clear all
syms n t
T1 = 2;
T = 8;
Commented out the lines below because they aren't used for anything
% x1 = ones(1,T1); % 1 i.e., 0<t<T1
% x2 = -1*ones(1,T/2-T1); %-1 i.e., T1<t<T0/2
% x = [x1 x2];
% x = [x x];
% tt = 0:length(x)-1;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(4*exp(-1i*w*n*t),t,0,1)+int(-4*exp(-1i*w*n*t),t,1,2));
C0 = limit(C(n),n,0); % C(0)
% Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12) C(13) C(14) C(15) C(16) C(17) C(18) C(19) C(20) C(21) C(22) C(23) C(24) C(25) C(26) C(27) C(28) C(29) C(30) C(31) C(32)];
% fprintf('First n Harmonics:\n')
% disp(Harmonics)
% f(t) using Fourier Series representation
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-100,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,100);
Changed the limits of fplot() to better see the result
fplot(t,f(t),[0 16])
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
Now the fundamental period of the constructed f(t) is 8, which is good. But the amplitude is still 4 and the switch points aren't correct, are they? So there are still problems with the computation of C(n). Again, carefuly review the expression for C(n) and verify that the function that is being integrated and the limits of integration are consistent with the problem statement. in other words, why does the expression for C(n) have the terms 4 and -4? Why are the limits of integration 0-1 and 1-2 ?
clc
clear all
syms n t
k=32
T0=8;
T = 8;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(1*exp(-1i*w*n*t),t,0,4)+int(-1*exp(-1i*w*n*t),t,4,8));
C0 = limit(C(n),n,0); % C(0)
Harmonics = [C0 C(1:k)];
fprintf('First 4 Harmonics:\n')
disp(Harmonics)
% f(t) using Fourier Series representation
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-100,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,100)
fplot(t,f(t))
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
is it good now ?
another answer i got was
clc
clear all
syms n t
T = 8;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(1*exp(-1i*w*n*t),t,0,2)+int(-1*exp(-1i*w*n*t),t,2,6)+int(1*exp(-1i*w*n*t),t,6,8));
C0 = limit(C(n),n,0); % C(0)
Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12) C(13) C(14) C(15) C(16) C(17) C(18) C(19) C(20) C(21) C(22) C(23) C(24) C(25) C(26) C(27) C(28) C(29) C(30) C(31) C(32)];
fprintf('First 13 Harmonics:\n')
Harmonics = Harmonics*2; %an = cn *2
disp(Harmonics)
% f(t) using Fourier Series representation
L = 4; % change values accordingly to 8, 16 32
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-L,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,L);
fplot(t,f(t))
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
Using the code from this comment:
syms n t
% k=32;
T0=8;
T = 8;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(1*exp(-1i*w*n*t),t,0,4)+int(-1*exp(-1i*w*n*t),t,4,8));
C0 = limit(C(n),n,0); % C(0)
% Harmonics = [C0 C(1:k)];
% fprintf('First 4 Harmonics:\n')
% disp(Harmonics)
% f(t) using Fourier Series representation
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-100,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,100);
fplot(t,f(t),[0 16])
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
Now the amplitude looks correct. Do you think the swith point is correct. According to the prolbme statement, at what time after t = 0 shoudl the signal switch from 1 to -1? Why does code use limits of integration of 0-4 and 4-8? Where did the 4 and 8 come from?
i really don't know what to change next
the limits should be related to periods which is 2 and 8
For 0 <= t <= T0, over what interval does x(t) = 1 and over what interval does x(t) = -1?
Faisal,
I misread the problem statement having completely missed the abs(t) in the definition of x(t). I apologize for leading you astray.
The code you posted in this comment appears to be correct.
clc
clear all
syms n t
T = 8;
w = 2*pi/T;
% Exponential Fourier series
C(n) = (1/T)*(int(1*exp(-1i*w*n*t),t,0,2)+int(-1*exp(-1i*w*n*t),t,2,6)+int(1*exp(-1i*w*n*t),t,6,8));
C0 = limit(C(n),n,0); % C(0)
% Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12) C(13) C(14) C(15) C(16) C(17) C(18) C(19) C(20) C(21) C(22) C(23) C(24) C(25) C(26) C(27) C(28) C(29) C(30) C(31) C(32)];
% fprintf('First 13 Harmonics:\n')
% Harmonics = Harmonics*2; %an = cn *2
% disp(Harmonics)
% f(t) using Fourier Series representation
L = 40; % change values accordingly to 8, 16 32
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-L,-1)+C0+symsum(C(n)*exp(1i*w*n*t),n,1,L);
fplot(t,f(t),[-16 16])
xlabel('t')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on
thank you paul for your time
You're welcome. And I hope I didn't waste too much of your time.
I would write the code like this:
syms n t
T0 = 8; % as defined in the problem
w = 2*pi/T0;
T1 = 2; % % as defined in the problem
% define one period of x(t) valide only on from -T0/2 to T0/2
% this way works, but using piecewise maps directly to the problem
% statement
% x(t) = (2*rectangularPulse(-T1,T1,t) - 1)*rectangularPulse(-T0/2,T0/2,t);
x(t) = piecewise(abs(t)<=T1,1,-1)*rectangularPulse(-T0/2,T0/2,t);
% Exponential Fourier series
C(n) = (1/T0)*int(x(t)*exp(-1i*w*n*t),t,-T0/2,T0/2);
C0 = (1/T0)*int(x(t)*exp(-1i*w*0*t),t,-T0/2,T0/2); % C(0)
C(n) = piecewise(n == 0, C0, C(n));
% Harmonics = [C0 C(1) C(2) C(3) C(4) C(5) C(6) C(7) C(8) C(9) C(10) C(11) C(12) C(13) C(14) C(15) C(16) C(17) C(18) C(19) C(20) C(21) C(22) C(23) C(24) C(25) C(26) C(27) C(28) C(29) C(30) C(31) C(32)];
% fprintf('First 13 Harmonics:\n')
% Harmonics = Harmonics*2; %an = cn *2
% disp(Harmonics)
% f(t) using Fourier Series representation
L = 40; % change values accordingly to 8, 16 32
f(t) = symsum(C(n)*exp(1i*w*n*t),n,-L,L);
% fplot f(t) and a couple of periods of x(t)
figure
fplot(t,f(t),[-16 16])
hold on
fplot(x(t) + x(t+T0) + x(t - T0),[-8 8])
xlabel('t (msec)')
ylabel('f(t)')
title('f(t) using Fourier Series Coefficients')
grid on

Sign in to comment.

More Answers (0)

Categories

Asked:

on 2 May 2022

Edited:

on 6 May 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!