Plotting 3 cycles of a periodic signal

14 views (last 30 days)
Samuel Courtney
Samuel Courtney on 2 Nov 2020
Edited: Rena Berman on 7 May 2021
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 Comments
John D'Errico
John D'Errico on 2 Nov 2020
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 on 3 Nov 2020
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');

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 2 Nov 2020
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 Comments
Walter Roberson
Walter Roberson on 2 Nov 2020
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 on 3 Nov 2020
Edited: Walter Roberson on 3 Nov 2020
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) ;

Sign in to comment.

Categories

Find more on View and Analyze Simulation Results in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!