0% found this document useful (0 votes)
13 views4 pages

Generating Square Waves in MATLAB

The document describes the square wave function in MATLAB. It generates a square wave with peaks of ±1 instead of a sine wave. It can generate square waves with different periods and duty cycles specified as a percentage between 0 and 100. Examples are given to generate square waves and illustrate how to specify the period and duty cycle.

Uploaded by

chandan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Generating Square Waves in MATLAB

The document describes the square wave function in MATLAB. It generates a square wave with peaks of ±1 instead of a sine wave. It can generate square waves with different periods and duty cycles specified as a percentage between 0 and 100. Examples are given to generate square waves and illustrate how to specify the period and duty cycle.

Uploaded by

chandan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like