MATLAB: How to plot a square wave centered around the Y-axis in MATLAB

aaroundcancenteredhowiplotSignal Processing Toolboxsquarethewavey axis

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

Best Answer

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)