RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact

Explore

  • RunMat for academia
  • RunMat vs MATLAB Online
  • Benchmarks

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

LicensePrivacy
/
See all docs
Builtin Reference
    • blackman
    • butter
    • buttord
    • cheb2ord
    • conv
    • conv2
    • deconv
    • downsample
    • envelope
    • filter
    • filtfilt
    • fir1
    • freqz
    • gauspuls
    • hamming
    • hann
    • hilbert
    • periodogram
    • pulstran
    • pwelch
    • rectpuls
    • resample
    • sawtooth
    • sinc
    • spectrogram
    • square
    • tripuls
    • unwrap
    • upsample
    • zplane

spectrogram — Compute a short-time Fourier transform spectrogram.

spectrogram(x) computes a short-time Fourier transform (STFT) of a signal. Optional arguments select the segment window, overlap, DFT length or explicit frequency grid, sampling frequency, frequency range, and PSD or power-spectrum scaling.

Syntax

s = spectrogram(x, win, nOverlap, freqSpec, Fs, freqRange, spectrumType)
[s, f, t, ps, fc, tc] = spectrogram(x, win, nOverlap, freqSpec, Fs, freqRange, spectrumType)

Inputs

NameTypeRequiredDefaultDescription
xNumericArrayYes—Input signal vector.
winAnyNo[]Hamming-window length, window vector, or [] for the default window.
nOverlapNumericScalarNo[]Number of overlapped samples between adjacent segments.
freqSpecNumericArrayNo[]DFT length or explicit frequency vector.
FsNumericScalarNo—Sampling frequency in Hz.
freqRangeStringScalarNo—Frequency range selector: `onesided`, `twosided`, or `centered`.
spectrumTypeStringScalarNopsdSpectral estimate scaling: `psd` or `power`.

Returns

NameTypeDescription
sNumericArrayComplex short-time Fourier transform matrix.
fNumericArrayFrequencies in radians/sample or Hz when fs is supplied.
tNumericArraySegment midpoint sample indices or time instants when fs is supplied.
psNumericArrayPower spectral density or power spectrum estimate for each segment.
fcNumericArrayCenter frequency matrix for reassigned-output compatibility.
tcNumericArrayCenter time matrix for reassigned-output compatibility.

Returned values from spectrogram depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:spectrogram:ArgCountThe argument count is outside supported forms.spectrogram: expected spectrogram(x, [win, [nOverlap, [freqSpec, [Fs, [freqRange_or_spectrumType]]]]])
RunMat:spectrogram:InvalidSignalInput signal is not a numeric vector.spectrogram: x must be a nonempty numeric vector
RunMat:spectrogram:InvalidWindowWindow input is invalid.spectrogram: win must be [] or a nonempty finite real vector or positive integer length
RunMat:spectrogram:InvalidOverlapOverlap is invalid.spectrogram: nOverlap must be a nonnegative integer smaller than the window length
RunMat:spectrogram:InvalidFreqSpecFrequency specification is invalid.spectrogram: freqSpec must be a positive integer or finite real frequency vector with at least two elements
RunMat:spectrogram:InvalidFsSampling frequency is invalid.spectrogram: Fs must be a positive finite scalar
RunMat:spectrogram:InvalidOptionFrequency range, spectrum type, or display option is invalid.spectrogram: expected 'onesided', 'twosided', 'centered', 'psd', 'power', 'reassigned', or 'yaxis'
RunMat:spectrogram:InternalOutput tensor construction fails internally.spectrogram: internal error

How spectrogram works

  • s = spectrogram(x) returns the complex STFT matrix with time increasing across columns and frequency increasing down rows.
  • [s, f, t, ps] = spectrogram(...) also returns the frequency vector, segment midpoint vector, and PSD or power-spectrum matrix.
  • win may be a positive scalar length, a finite real window vector, or []. [] uses a Hamming window sized to divide the signal into about eight 50%-overlapped sections.
  • nOverlap defaults to half the window length and must be less than the window length.
  • freqSpec defaults to max(256, 2^nextpow2(length(win))). A real frequency vector with at least two elements may be supplied to evaluate explicit frequencies.
  • Omitting Fs uses normalized radians/sample and segment midpoint times divided by 2*pi. Passing Fs as [] uses a 1 Hz sample rate.
  • Real inputs default to one-sided output; complex inputs default to two-sided output and reject 'onesided'.
  • The optional range selector accepts 'onesided', 'twosided', or 'centered'. Range selectors are not combined with explicit frequency vectors.
  • The optional spectrum type accepts 'psd' (default) or 'power'; 'power' uses coherent-gain normalization.
  • 'yaxis' is accepted as a plotting-orientation no-op for numeric execution. 'reassigned' is rejected until true energy-center reassignment is implemented.
  • The documented signal storage class is single or double. Typed integer signals, windows, overlap counts, frequency specifications, and sample rates are role-specific RunMat extensions. Any value entering binary64 rejects if it would round.

Examples

STFT of a sine wave

fs = 1000;
t = (0:999) / fs;
x = sin(2*pi*100*t);
[s, f, tt, ps] = spectrogram(x, hamming(128), 64, 256, fs);

Use scalar window length

[s, f, t] = spectrogram(randn(1,1024), 128, 64, 256);

Centered complex STFT

x = exp(1i*2*pi*(0:127)/16);
[s, f, t] = spectrogram(x, 32, 16, 64, 'centered');

Power spectrum scaling

[s, f, t, p] = spectrogram(randn(1,512), 64, 32, 128, 'power');

Explicit normalized frequencies

w = linspace(0, pi, 64);
[s, wout, t] = spectrogram(randn(1,512), hamming(128), 64, w);

Using spectrogram with coding agents

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

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

FAQ

What is the relationship between s and ps?⌄

s is the complex STFT. ps is derived from abs(s).^2 using PSD or power-spectrum normalization and one-sided doubling when appropriate.

Does spectrogram plot when no outputs are requested?⌄

RunMat currently returns values using normal output-count semantics instead of opening a plot. The 'yaxis' token is accepted so scripts can continue numeric execution.

Does RunMat implement reassigned spectrogram energy centers?⌄

No. RunMat rejects 'reassigned' rather than returning unreassigned coordinate grids.

Related Math functions

Signal

blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · square · tripuls · unwrap · upsample · zplane

Elementwise

abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · typecast · uint16 · uint32 · uint8

Trigonometry

acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh

Reduction

all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var

Structure

bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref · vecnorm

Optim

coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimoptions · optimset · quad · secondordercone

Ops

cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftn · fftshift · ifft · ifft2 · ifftn · ifftshift

Interpolation

griddedInterpolant · interp1 · interp1q · interp2 · pchip · ppval · spline

Discrete

lcm · primes

Ode

ode15s · ode23 · ode45

Poly

polyder · polyfit · polyint · polyval · roots

Open-source implementation

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

  • View the source for spectrogram in Rust on GitHub
  • Learn how the RunMat 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 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.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • How spectrogram works
  • Examples
  • STFT of a sine wave
  • Use scalar window length
  • Centered complex STFT
  • Power spectrum scaling
  • Explicit normalized frequencies
  • Using spectrogram with coding agents
  • FAQ
  • Related Math functions
  • Signal
  • Elementwise
  • Trigonometry
  • Reduction
  • Structure
  • Rounding
  • Factor
  • Solve
  • Optim
  • Ops
  • Symbolic
  • Fft
  • Interpolation
  • Discrete
  • Ode
  • Poly
  • Open-source implementation
  • About RunMat