Qu

rfft

Builtin index · Signal processing

result = rfft(x)

Input: x · Output: result

What it is

Real-optimized half-spectrum FFT. x is a length-N real vector.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = rfft(x)
x vectornot documented yet
CVec —DC through Nyquist
Returns a length-floor(N/2)+1 CVec (DC through Nyquist) — a real signal's spectrum is Hermitian-symmetric, so only the non-negative-frequency half is returned.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

s = signal(0 to 999, 1000)     # 1000 samples at 1 kHz, 1 s long


s[3]                # sample 3 — a number

s[100:200]          # samples 100..=200 — a Signal at the same Fs

s[0.1 s : 0.2 s]    # 0.1 s to 0.2 s — a Signal at the same Fs

s[0.5 s]            # the sample NEAREST 0.5 s — a number

s[100 ms : 200 ms]  # identical to s[0.1 s : 0.2 s]; prefixes scale


X = rfft(s)
X[3]                # bin 3 — a complex number

X[440 Hz]           # the bin nearest 440 Hz — exactly spectrum_at(X, 440)

X[20 Hz : 200 Hz]   # that band's bins — a complex vector

X[0.2 kHz]          # same bin as X[200 Hz]; prefixes scale here too

from signal-processing

  half = rfft(x)               # length floor(N/2)+1

  back = irfft(half, length(x))

from numerics-dsp

spectrum = abs(rfft(y))
freqs    = (0 to len(spectrum) - 1) * fs / len(y)
peak     = argmax(spectrum)
print("largest component at {freqs[peak]:.1f} Hz")
output
largest component at 50.0 Hz

from from-zero

t  = linspace(0, 1, 500)                  # one second at 500 Hz

y  = sin(2 * pi * 5 * t) + 0.2 * randn(500, seed = 1)
mag  = abs(rfft(y))                       # 251 bins for 500 samples

f    = linspace(0, 250, len(mag))         # the matching frequency axis

yhat = sin(2 * pi * 5 * t)                # a "model" to compare against

losses = 1 ./ (1 to 40)                   # a decaying training curve

from plotting

How it works

rfft(x, [scaling=]) -- scaling= picks the normalisation stamped on the returned Spectrum: "raw" (default, the unnormalised DFT sum), "amplitude" (a bin reads the peak amplitude of the tone at that frequency) or "rms" (that tone's RMS value). Requires a Signal input -- the convention is metadata carried on Value::Spectrum, and a bare CVec has nowhere to record it, so asking for a scaling on plain numbers is refused rather than silently dropped.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.