9/20/2016
SquarewaveMATLABsquareMathWorksIndia
square
Squarewave
Syntax
x=square(t)
x=square(t,duty)
Description
x=square(t)[Link](t)issimilarto
sin(t),butcreatesasquarewavewithpeaksof1insteadofasinewave.
x=square(t,duty)generatesasquarewavewithspecifieddutycycle,duty,whichisanumberbetween0and100.
Thedutycycleisthepercentoftheperiodinwhichthesignalispositive.
Examples
collapseall
GenerateSquareWaves
Createavectorof100equallyspacednumbersfrom to
asquarewavewithaperiodof .
.Generate
OpenScript
t=linspace(0,3*pi)';
x=square(t);
[Link] .Thegeneratedsquarewavehasavalueof at
evenmultiplesof andavalueof atoddmultiplesof .Itisnever .
plot(t/pi,x,'.',t/pi,sin(t))
xlabel('t/\pi')
gridon
[Link]
1/4
9/20/2016
SquarewaveMATLABsquareMathWorksIndia
Repeatthecalculation,butnowevaluatesquare(2*t)at121equallyspacednumbersbetween and .Change
theamplitudeto
.[Link]
andpositiveattheendpoints.
t=linspace(pi,2*pi,121);
x=1.15*square(2*t);
plot(t/pi,x,'.',t/pi,1.15*sin(2*t))
xlabel('t/\pi')
gridon
[Link]
2/4
9/20/2016
SquarewaveMATLABsquareMathWorksIndia
DutyCycleofSquareWave
[Link]
dutycycleof37%.AddwhiteGaussiannoisewithavarianceof1/100.
OpenScript
t=0:1/1e3:0.07;
y=square(2*pi*30*t,37)+randn(size(t))/10;
[Link].
dutycycle(y,t)
ans=
0.3639
[Link]
3/4
9/20/2016
SquarewaveMATLABsquareMathWorksIndia
SeeAlso
chirp|cos|diric|gauspuls|pulstran|rectpuls|sawtooth|sin|tripuls
IntroducedbeforeR2006a
[Link]
4/4