RunMat
GitHub

sawtooth — Generate a periodic sawtooth waveform with optional peak position.

y = sawtooth(t) generates a sawtooth wave with period 2*pi at the sample times in t, ranging in [-1, 1]. y = sawtooth(t, xmax) controls the peak position with xmax in [0, 1]: xmax = 1 (the default) produces a rising sawtooth, xmax = 0 produces a falling sawtooth, and intermediate values such as xmax = 0.5 produce a triangle wave whose peak is at the centre of each period.

How sawtooth 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.
  • Returns -1 at the start of each period and approaches +1 just before the period boundary wraps when xmax = 1.
  • Returns a piecewise-linear triangle wave when xmax = 0.5, peaking at 1 at the centre of each period.
  • Returns a pure falling ramp when xmax = 0 and a pure rising ramp when xmax = 1.
  • 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 xmax values outside [0, 1] and non-scalar xmax arguments.

Examples

Default rising sawtooth over two periods

t = 0:pi/2:4*pi;
y = sawtooth(t)

Expected output:

y = [-1 -0.5 0 0.5 -1 -0.5 0 0.5 -1]

Triangle wave via xmax = 0.5

t = linspace(0, 2*pi, 5);
y = sawtooth(t, 0.5)

Expected output:

y = [-1 0 1 0 -1]

Pure falling sawtooth via xmax = 0

y = sawtooth([0 pi 2*pi], 0)

Expected output:

y = [1 0 1]

FAQ

Why does sawtooth(2*pi) return -1 instead of +1?

RunMat reduces the input modulo 2*pi, so 2*pi is folded back to the start of the next period. The default rising sawtooth therefore restarts at -1 exactly on each period boundary, matching MATLAB.

How do I produce a triangle wave?

Pass xmax = 0.5 so the peak sits at the centre of each period and the waveform ramps symmetrically up and back down.

Can sawtooth accept complex inputs?

No. sawtooth 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 · sinc · square

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 sawtooth 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.