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

採用された回答
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
2022 年 5 月 4 日
hello paul
i'm having issues doing the changes you mentioned can you show me how?
Paul
2022 年 5 月 4 日
What exactly are the issues? Perhaps updated code can be posted ....
Faisal Al-Wazir
2022 年 5 月 4 日
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 ?
Faisal Al-Wazir
2022 年 5 月 4 日
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 ?
Faisal Al-Wazir
2022 年 5 月 4 日
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
Paul
2022 年 5 月 4 日
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?
Faisal Al-Wazir
2022 年 5 月 4 日
i really don't know what to change next
the limits should be related to periods which is 2 and 8
Paul
2022 年 5 月 4 日
For 0 <= t <= T0, over what interval does x(t) = 1 and over what interval does x(t) = -1?
Paul
2022 年 5 月 4 日
Faisal,
I misread the problem statement having completely missed the abs(t) in the definition of x(t). I apologize for leading you astray.
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

Faisal Al-Wazir
2022 年 5 月 4 日
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

その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Fourier Analysis and Filtering についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
