RunMat
GitHub

square — Generate periodic square waves with MATLAB-compatible duty-cycle control.

y = square(t) generates a period-2*pi square wave sampled at t with outputs in {-1, +1}. y = square(t, duty) applies MATLAB-compatible percentage duty-cycle control over [0, 100].

Syntax

Y = square(t)
Y = square(t, duty)

Inputs

NameTypeRequiredDefaultDescription
tNumericArrayYesSample times.
dutyNumericScalarNo50Duty cycle percentage in [0, 100].

Returns

NameTypeDescription
YNumericArraySquare-wave output sampled at t.

Errors

IdentifierWhenMessage
RunMat:square:InvalidInputPrimary input is not numeric-real tensor/scalar compatible.square: expected numeric input
RunMat:square:ComplexUnsupportedPrimary input includes complex values.square: input must be real; complex values are not supported
RunMat:square:DutyInvalidDuty argument is not a real numeric scalar.square: duty must be a real numeric scalar in [0, 100]

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]

Using square with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how square changes the result.

Run a small square example, explain the result, then change one input and compare the output.

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 · butter · 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 · rref

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 is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.