fft
Builtin index · Signal processing
result = fft(x, [scaling=])
Input: x, scaling · Output: result
What it is
x: a length-N real or complex vector. scaling= (optional string: "raw", the default, "amplitude" or "rms"), same conventions as rfft below, requires x to be a Signal. On the FULL (two-sided) spectrum this returns, a real tone occupies TWO conjugate-symmetric bins, and scaling makes each of them independently read the tone's full peak amplitude (or RMS) rather than half of it — correct per bin, but summing both (or integrating a band across the mirror) double-counts that tone's energy. Use rfft instead when a total is what you want.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = fft(x, [scaling=]) | x vectorx: a length-N real or complex vector.scaling string or Signal, optionalscaling= (optional string: "raw", the default, "amplitude" or "rms"), same conventions as rfft below, requires x to be a Signal. | spectrum SignalSignal x | Returns a length-N complex vector (CVec, plain x) or spectrum (Signal x), the discrete Fourier transform. abs gives magnitude, angle phase. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
N = 8
n = 0 to N - 1
x = cos(2 * pi * 2 * n / N) # a pure tone at bin 2
X = fft(x, N)
print("mag = {abs(X)}")
mag = [2.449294e-16, 3.463824e-16, 4, 3.463824e-16, 2.449294e-16, 3.463824e-16, 4, 3.463824e-16]
from numerics-dsp
r = {x = 1, y = 2}
print("r = {r}")
print("r.x = {r.x}")
parts = split("a,b,c", ",")
print("parts = {parts}")
X = fft([1,2,3,4])
print("X = {X}")
sig = signal([1,2,3,4], 100)
print("sig = {sig}")
r = {x = 1, y = 2}
r.x = 1
parts = ("a", "b", "c")
X = [10, -2 + 2j, -2, -2 - 2j]
sig = signal(Fs=100) [1, 2, 3, 4]from tour
err = max(abs(fft(x) - dft(x))) # ~0 to floating precision
from numerics-dsp
N = 512
Fs = 512
t = (0 to N - 1) / Fs
x = sin(2*pi*40*t) + 0.6*sin(2*pi*90*t)
X = fft(x)
mag = abs(X)
half = mag[0:N/2]
pk = findpeaks(half, min_peak_height=20, min_peak_distance=5)
print(pk)
freqs = pk.locations * (Fs / N)
print("Peak bin locations: {pk.locations}")
print("Peak frequencies (Hz): {freqs}")
from tour
How it works
DFT: fft(x) or fft(x, n) (zero-pads/truncates to n samples). fftc is an explicit-complex alias of fft (same function).
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.