Plotting 3 cycles of a periodic signal

16 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 View 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