square — Generate a periodic square wave with optional duty cycle.
y = square(t) generates a square wave with period 2*pi at the sample times in t, with values in {-1, +1}. y = square(t, duty) accepts an explicit duty cycle as a percentage in [0, 100]: the output is +1 over the first duty/100 * 2*pi of every period and -1 for the remainder. The default duty cycle is 50% so that square produces the standard symmetric square wave.
How square works
- Operates element-wise on scalars, vectors, matrices, and N-D tensors; preserves input shape.
- Reduces input modulo
2*piso the waveform is periodic across negative and positive sample times. - Uses half-open interval semantics:
+1forphi/(2*pi) < duty/100,-1otherwise. duty = 0produces a constant-1waveform;duty = 100produces a constant+1waveform.- Integer and logical inputs are promoted to double precision before evaluation.
- Non-finite sample times (
NaN,+Inf,-Inf) propagate asNaNelement-wise, matching MATLAB. - Complex and text inputs are rejected with a builtin-scoped error.
- Rejects
dutyvalues outside[0, 100]and non-scalardutyarguments.
Examples
Default 50% duty cycle square wave
t = 0:pi/4:2*pi;
y = square(t)Expected output:
y = [1 1 1 1 -1 -1 -1 -1 1]25% duty cycle square wave
t = 0:pi/4:2*pi;
y = square(t, 25)Expected output:
y = [1 1 -1 -1 -1 -1 -1 -1 1]Constant outputs at the boundaries
y = square([0 pi 2*pi], 100)Expected output:
y = [1 1 1]FAQ
What happens exactly at the duty-cycle boundary?⌄
RunMat uses half-open semantics: the boundary phase belongs to the negative half. With the default 50% duty cycle, square(pi) returns -1. This matches MATLAB's documented behaviour.
How do I produce a constant +1 or -1?⌄
Pass duty = 100 for a constant +1 waveform and duty = 0 for a constant -1 waveform.
Can square accept complex inputs?⌄
No. square is defined on real samples and rejects complex inputs with a builtin-scoped error to match MATLAB.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how square works, line by line, in Rust.
- View square.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.