# Fourier series plotting and improving

조회 수: 7(최근 30일)
Faisal Al-Wazir 2022년 5월 2일
편집: Paul 2022년 5월 6일
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 댓글을 달려면 로그인하십시오.

### 채택된 답변

Paul 2022년 5월 2일
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표시숨기기 이전 댓글 수: 11
Paul 2022년 5월 4일
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 댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!