Plotting a square wave

How can I plot a square wave of period 2 seconds, with step 0.01 second ,amplitude from -1 to 1 and 50% duty cycle?

3 个评论

Create it first, then plotting it is trivial with
doc plot
stairs will be easier for square wave; it does the "doubling up" needed by plot automagically (or builds the vectors for plot)
duty = 50;
t = 0:0.01:8;
f = 0.5;
x = square(2*pi*f*t,duty);
plot(t,x)
axis([0 8 -1.5 1.5])
grid on

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2017-6-16
编辑:dpb 2017-6-16

0 个投票

If you have Signal Processing TB, then use square function. Without, you'll have to compute the transition spacings ( linspace should be helpful there) and then stairs will do a plot or create the filled-in x- y- vectors including the "doubled-up" values at the breakpoints.

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by