Fourier series plotting and improving
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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 ....
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
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
Paul
2022년 5월 4일
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.
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

추가 답변 (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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
