How can I plot a square wave centered around the Y-axis in MATLAB?

Illustration
Kathryn - 2021-03-16T09:46:12+00:00
Question: How can I plot a square wave centered around the Y-axis in MATLAB?

I want to  plot a square wave centered around the Y-axis.  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

The following code sample  plots a square wave with an amplitude of 1 between -0.5 to +0.5 and 0 else where.
 
 
t = -0.5:0.01:0.5;

y = square(2*pi*.25*t,100);

plot(t,y)

tl = -1:0.01:-0.5;

yl = square(0*pi*.25*tl,0);

tr = 0.5:0.01:1;

yr = square(0*pi*.25*tl,0);

tg = [tl t tr];

yg = [yl y yr];

plot(tg,yg)


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!