![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175288/image.jpeg)
How do I plot a periodic square-wave for 10 cycles, 5 cycles in advance of t=0 and 5 cycles to the right of t=0 using a unit step function?
5 views (last 30 days)
Show older comments
I am trying to plot a square wave using a unit step function.
0 Comments
Accepted Answer
Chaya N
on 19 Oct 2016
Edited: Chaya N
on 19 Oct 2016
A square wave is more accurately an impulse function (that is also periodic), which is the first derivative of a step function. To define the function, you need a time index for the rising edge and another time index for the falling edge.
For example, to plot one cycle of a square wave with a rising edge at t = 0 and a falling edge at t = 0.5, the function would be f[t] = u[t] - u[t-0.5].
Below I am including a very simple code to show two full cycles of your square wave for (-1 < t <= 0) and (0 < t <= 1). Please note the format and sign of the time stamps and how the individual cycles are grouped inside the square brackets (which are not required in the code but only meant as illustration for the grouping here). You can expand upon this and hopefully construct your required wave.
t = linspace(-2, 2, 1000); % This defines the time scale in the plot
f = @(t) [(t > 0) - (t > 0.5)] + [(t > -1) - (t > -0.5)];
stairs(t, f(t));
The plot from the above function looks like the figure below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175288/image.jpeg)
Good luck!
0 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!