Plotting 3 cycles of a periodic signal

16 ビュー (過去 30 日間)
Samuel Courtney
Samuel Courtney 2020 年 11 月 2 日
編集済み: Rena Berman 2021 年 5 月 7 日
Hi all,
Ive been trying to plot 3 cycles of the below function.
z(t) = ( t + 4 if − π ≤ t < 0,
e if 0 ≤ t ≤ π)
this is what ive wroten so far, i know all ive done is plot one cycle and then move it along x axis, but i would rather have another way of doing the same thing.
t0 = -pi:0.00001:pi;
T0 = length(t0);
for i = 1:T0
if t0(i)< 0
z0(i)=t0(i)+4;
else
z0(i)=exp(1);
end
end
t1 = -pi:0.00001:pi;
T1 = length(t1);
for i = 1:T1
if t1(i)< 0
z1(i)=t1(i)+4;
else
z1(i)=exp(1);
end
end
t2 = -pi:0.00001:pi;
T2 = length(t2);
for i = 1:T2
if t2(i)< 0
z2(i)=t2(i)+4;
else
z2(i)=exp(1);
end
end
figure;
plot(t0, z0, 'r');
hold on
plot(t1+2*pi, z1, 'b');
hold on
plot(t2+4*pi,z2, 'g');
  3 件のコメント
John D'Errico
John D'Errico 2020 年 11 月 2 日
When you delete your question, you insult those who made the effort, who spent the time to answer your questin. You hurt Answers itself, because nobody else can learn from the answer.
Stephen23
Stephen23 2020 年 11 月 3 日
Original question by Samuel Courtney retrieved from Google Cache:
"Plotting 3 cycles of a periodic signal"
Hi all,
Ive been trying to plot 3 cycles of the below function.
z(t) = ( t + 4 if − π ≤ t < 0,
e if 0 ≤ t ≤ π)
this is what ive wroten so far, i know all ive done is plot one cycle and then move it along x axis, but i would rather have another way of doing the same thing.
t0 = -pi:0.00001:pi;
T0 = length(t0);
for i = 1:T0
if t0(i)< 0
z0(i)=t0(i)+4;
else
z0(i)=exp(1);
end
end
t1 = -pi:0.00001:pi;
T1 = length(t1);
for i = 1:T1
if t1(i)< 0
z1(i)=t1(i)+4;
else
z1(i)=exp(1);
end
end
t2 = -pi:0.00001:pi;
T2 = length(t2);
for i = 1:T2
if t2(i)< 0
z2(i)=t2(i)+4;
else
z2(i)=exp(1);
end
end
figure;
plot(t0, z0, 'r');
hold on
plot(t1+2*pi, z1, 'b');
hold on
plot(t2+4*pi,z2, 'g');

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

回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 11 月 2 日
No need to create the same values 3 times. Your code can be simplified to the following. You also don't need nearly as many points to create the visualization.
t0 = -pi:0.01:pi;
T0 = length(t0);
for i = 1:T0
if t0(i)< 0
z0(i)=t0(i)+4;
else
z0(i)=exp(1);
end
end
plot(t0, z0, 'r',t0+2*pi, z0, 'b',t0+4*pi,z0, 'g');
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 11 月 2 日
If you use this kind of code structure, then be careful about endpoints. If the period is exactly divisible by the increment, then you end up with a data point at initial + period due to the end point of the first period, and you end up with with a data point at the same location due to the starting point of the second period.
This particular code has a period of 2*pi which is not exactly divisible by 0.01 so the endpoint of the first period is slightly before the start point of the second period so you are saved.
Walter Roberson
Walter Roberson 2020 年 11 月 3 日
編集済み: Walter Roberson 2020 年 11 月 3 日
syms t
Mod = @(A,B) A-floor(A/B)*B
z0 = piecewise(Mod(t, 2*pi)<=pi, exp(1), Mod(t, 2*pi)+4-2*pi)
fourier(z0)
Z0 = double(subs(z0, t, linspace(-pi,5*pi,257))) ;
Z0(end) = [] ;
FZ = fft(Z0) ;

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

カテゴリ

Help Center および File ExchangeView and Analyze Simulation Results についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by