Main Content

square

Description

x = square(t) generates a square wave with period 2π for the elements of the time array t. square is similar to the sine function but creates a square wave with values of –1 and 1.

example

x = square(t,duty) generates a square wave with specified duty cycle duty. The duty cycle is the percent of the signal period in which the square wave is positive.

example

Examples

collapse all

Create a vector of 100 equally spaced numbers from 0 to 3π. Generate a square wave with a period of 2π.

x(t)={(-1)t/π,t=kπkZsign(sin(t)),tkπkZ

t = linspace(0,3*pi)';
x = square(t);

Plot the square wave and overlay a sine. The generated square wave has a value of 1 for intervals [nπ,(n+1)π) with even n and a value of -1 for intervals [nπ,(n+1)π) with odd n. The wave never has a value of 0.

scatter(t,x,".")
hold on
scatter(t,sin(t),".")
hold off
xlabel("t")
xticks((0:3)*pi)
xticklabels(["0" "\pi" "2\pi" "3\pi"])
grid on

Figure contains an axes object. The axes object with xlabel t contains 2 objects of type scatter.

Repeat the calculation, but now evaluate square(2*t) at 121 equally spaced numbers between -π and 2π. Change the amplitude to 1.15. Plot the wave and overlay a sine with the same parameters. This new wave is negative at t=0 and positive at the endpoints, -π and 2π. The period is now π because the time values increase twice as fast as before, so the period is half as before.

t = linspace(-pi,2*pi,121);
x = 1.15*square(2*t);

scatter(t,x,".")
hold on
scatter(t,1.15*sin(2*t),".")
hold off
xlabel("t")
xticks((-1:2)*pi)
xticklabels(["-\pi" "0" "\pi" "2\pi"])
grid on

Figure contains an axes object. The axes object with xlabel t contains 2 objects of type scatter.

Since R2026a

This example shows how to create square waves with any oscillating period or frequency.

Generate a square wave with the default period of 2π seconds during 10 seconds.

x(t)={(-1)t/π,t=kπkZsign(sin(t)),tkπkZ

Fs = 1000;
tEnd = 10-1/Fs;
t = 1/Fs:1/Fs:tEnd;

x1 = square(t);

Generate a square wave that oscillates with a period T of 2.5 seconds.

xT(t)=x(2πTt)

T = 2.5;
x2 = square(2*pi/T*t);

Generate a square wave that oscillates at a frequency F of 2 Hz.

xF(t)=x(2πFt)

F = 2;
x3 = square(2*pi*F*t);

Compare the square waves. The waves oscillate at the specified period or frequency specification. x1, x2, and x3 oscillates every 6.28, 2.5, and 0.5 seconds, respectively.

strips([x1 x2 x3],tEnd,Fs)
xlabel("Time (seconds)")
yticklabels(["x3" "x2" "x1"])

Figure contains an axes object. The axes object with xlabel Time (seconds) contains an object of type line.

Generate a 30 Hz square wave sampled at 1 kHz for 70 ms. Specify a duty cycle of 37%. Add white Gaussian noise with a variance of 1/100.

t = 0:1/1e3:0.07;
y = square(2*pi*30*t,37) + randn(size(t))/10;

Compute the duty cycle of the wave. Plot the waveform and annotate the duty cycle.

dutycycle(y,t)

Figure Duty Cycle Plot contains an axes object. The axes object with xlabel Time (seconds), ylabel Level (Volts) contains 9 objects of type line. One or more of the lines displays its values using only markers These objects represent signal, mid cross, upper boundary, upper state, lower boundary, mid reference, lower state.

ans = 
0.3639

Input Arguments

collapse all

Time array, specified as a vector, matrix, or N-D array. square operates along the first array dimension of t with size greater than 1.

Data Types: single | double

Duty cycle, specified as a real scalar from 0 to 100.

Data Types: single | double

Output Arguments

collapse all

Square wave, returned as a vector, matrix, or N-D array.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a

expand all