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.
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.
Create a vector of 100 equally spaced numbers from to . Generate a square wave with a period of .
t = linspace(0,3*pi)';
x = square(t);
Plot the square wave and overlay a sine. Normalize the x-axis by . The generated square wave has a value of for intervals [) with even and a value of for intervals [) with odd . The wave never has a value of .
plot(t/pi,x,'.-',t/pi,sin(t))
xlabel('t / \pi')
grid on
Repeat the calculation, but now evaluate square(2*t) at 121 equally spaced numbers between and . Change the amplitude to . Plot the wave and overlay a sine with the same parameters. This new wave is negative at and positive at the endpoints, and .
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')
grid on
The square function supports code generation for
graphical processing units (GPUs). You must have MATLAB®
Coder™ and GPU Coder™ to generate CUDA® code.
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window.
Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.