RunMat
GitHub

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*pi so the waveform is periodic across negative and positive sample times.
  • Uses half-open interval semantics: +1 for phi/(2*pi) < duty/100, -1 otherwise.
  • duty = 0 produces a constant -1 waveform; duty = 100 produces a constant +1 waveform.
  • Integer and logical inputs are promoted to double precision before evaluation.
  • Non-finite sample times (NaN, +Inf, -Inf) propagate as NaN element-wise, matching MATLAB.
  • Complex and text inputs are rejected with a builtin-scoped error.
  • Rejects duty values outside [0, 100] and non-scalar duty arguments.

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.

Signal

blackman · conv · conv2 · deconv · filter · hamming · hann · sawtooth · sinc

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · eig · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · pinv · rank · rcond

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Interpolation

interp1 · interp2 · pchip · ppval · spline

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how square works, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Try RunMat for free

Write code or describe what you want to compute. The sandbox is free, no account required.