Fourier series plotting and improving

14 ビュー (過去 30 日間)
Faisal Al-Wazir
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
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 件のコメント
Faisal Al-Wazir
Faisal Al-Wazir 2022 年 5 月 4 日
thank you paul for your time
Paul
Paul 2022 年 5 月 4 日
編集済み: Paul 2022 年 5 月 6 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by