Cyforanumera
npm

API reference

np.fft

Discrete Fourier transforms on the pocketfft algorithm (the same one NumPy uses). Complex results are complex128. Read them with toTypedArray(), which returns an interleaved Float64Array [re0, im0, re1, im1, ...]. The 1-D functions accept either positional (a, n, axis, norm) or an options object { n, axis, norm }.

NameSummary
np.fft.fft1-D complex DFT and its inverse.
np.fft.rfftDFT of real input, which returns the n / 2 + 1 non-negative frequencies.
np.fft.fftnN-D transforms over axes (by default all of them, or the last s.length).
np.fft.fftfreqSample frequencies for fft/rfft outputs of length n, with sample spacing d.
np.fft.outEvery transform accepts out, either as the last positional parameter or in the options object.
np.fft.hfftFFT of a signal with Hermitian symmetry, given as its first half, which has a real spectrum.
np.fft.rfftnN-D transforms of real input: rfft over the last axis in axes, then fft over the others.
np.fft.fftshiftMoves the zero-frequency term to the centre of the spectrum by rolling each axis in axes (default: all) by shape[ax] // 2.

np.fft.fft

#
np.fft.fft(a, [n], [axis=-1], [norm]) · np.fft.ifft(...)

1-D complex DFT and its inverse.

Parameters

aArrayLike
An NDArray, nested JS array or scalar.
[n]number
Transform length; the input is zero-padded or truncated to fit.
[axis=-1]number
Axis to transform.
[norm]"backward" | "ortho" | "forward"
Scaling convention. Default "backward".

Returns

NDArray (complex128)

Example

TypeScript
const f = np.fft.fft([1, 2, 3, 4]);
f.dtype.name;                       // => "complex128"
Array.from(f.toTypedArray());       // => [10, 0, -2, 2, -2, 0, -2, -2]
Array.from(np.fft.ifft(f).toTypedArray()); // => [1, 0, 2, 0, 3, 0, 4, 0]
Array.from(np.fft.fft([1, 1, 1, 1], { norm: "ortho" }).toTypedArray()); // => [2, 0, 0, 0, 0, 0, 0, 0]
TypeScript declaration
np.fft.fft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray
np.fft.ifft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray

np.fft.rfft

#
np.fft.rfft(a, [n], [axis], [norm]) · np.fft.irfft(...)

DFT of real input, which returns the n / 2 + 1 non-negative frequencies. irfft inverts it back to a real array; its default length is 2 * (m - 1).

Parameters

aArrayLike
An NDArray, nested JS array or scalar.
[n], [axis], [norm]
As for fft.

Returns

NDArray

Example

TypeScript
const spec = np.fft.rfft([1, 2, 3, 4]);
spec.shape;           // => [3]
np.fft.irfft(spec);   // => [1, 2, 3, 4]
TypeScript declaration
np.fft.rfft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray
np.fft.irfft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray

np.fft.fftn

#
np.fft.fftn(a, [options]) · ifftn · fft2 · ifft2

N-D transforms over axes (by default all of them, or the last s.length). fft2/ifft2 default to the last two axes.

Parameters

aArrayLike
An NDArray, nested JS array or scalar.
[options.s]number[]
Output lengths per transformed axis.
[options.axes]number[]
Axes to transform.
[options.norm]string
Scaling convention.

Returns

NDArray (complex128)

Example

TypeScript
np.fft.fft2([[1, 2], [3, 4]]).shape; // => [2, 2]
Array.from(np.fft.fft2([[1, 2], [3, 4]]).toTypedArray()).filter((_, i) => i % 2 === 0); // => [10, -2, -4, 0]
TypeScript declaration
np.fft.fftn(a: ArrayLike, s?: number[] | FftNOptions | null | undefined, axes?: number[] | null | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray

np.fft.fftfreq

#
np.fft.fftfreq(n, [d=1]) · np.fft.rfftfreq(n, [d=1])

Sample frequencies for fft/rfft outputs of length n, with sample spacing d.

Parameters

nnumber
Window length.
[d=1]number
Sample spacing (1 / sample rate).

Returns

NDArray

Example

TypeScript
np.fft.fftfreq(4);       // => [0, 0.25, -0.5, -0.25]
np.fft.rfftfreq(4, 0.5); // => [0, 0.5, 1]
TypeScript declaration
np.fft.fftfreq(n: number, d?: number | undefined): NDArray
np.fft.rfftfreq(n: number, d?: number | undefined): NDArray

np.fft.out

#
np.fft.fft(a, n, axis, norm, out) · np.fft.fftn(a, { s, axes, norm, out }) · …

Every transform accepts out, either as the last positional parameter or in the options object. The result is written into out, and out itself is returned. Multi-axis transforms pass out to every 1-D step, as NumPy does.

Parameters

aArrayLike
An NDArray, nested JS array or scalar.
[out]NDArray
Write the result here and return it. Its shape must match the result along the transformed axis, and the result dtype must cast to it under same_kind.

Returns

NDArray (`out`)

Example

TypeScript
const out = np.zeros([4], { dtype: "complex64" });
np.fft.fft([1, 1, 1, 1], { out }) === out; // => true
Array.from(out.toTypedArray());            // => [4, 0, 0, 0, 0, 0, 0, 0]
const r = np.zeros([2, 2]);
np.fft.irfft2([[4, 0], [0, 0]], { s: [2, 2], out: r }) === r; // => true
r;                                          // => [[1, 1], [1, 1]]
TypeScript declaration
np.fft.fft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray
np.fft.fftn(a: ArrayLike, s?: number[] | FftNOptions | null | undefined, axes?: number[] | null | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray

np.fft.hfft

#
np.fft.hfft(a, [n], [axis=-1], [norm], [out]) · np.fft.ihfft(...)

FFT of a signal with Hermitian symmetry, given as its first half, which has a real spectrum. The default n is 2 * (m - 1). ihfft is the inverse: it takes real input and returns n / 2 + 1 complex values.

Parameters

aArrayLike
An NDArray, nested JS array or scalar.
[n], [axis], [norm]
As for fft.
[out]NDArray
Write the result here and return it. Its shape must match the result along the transformed axis, and the result dtype must cast to it under same_kind.

Returns

NDArray (real for `hfft`, complex for `ihfft`)

Example

TypeScript
np.fft.hfft([1, 2, 3]);                       // => [8, -2, 0, -2]
Array.from(np.fft.ihfft([8, -2, 0, -2]).toTypedArray()); // => [1, 0, 2, 0, 3, 0]
TypeScript declaration
np.fft.hfft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray
np.fft.ihfft(a: ArrayLike, n?: number | FftOptions | null | undefined, axis?: number | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray

np.fft.rfftn

#
np.fft.rfftn(a, [options]) · irfftn · rfft2 · irfft2

N-D transforms of real input: rfft over the last axis in axes, then fft over the others. irfftn inverts this, and the last output length defaults to 2 * (m - 1). rfft2/irfft2 default to the last two axes.

Parameters

aArrayLike
An NDArray, nested JS array or scalar.
[options.s]number[]
Output lengths per transformed axis (-1 keeps the input length).
[options.axes]number[]
Axes to transform.
[options.norm]string
Scaling convention.
[options.out]NDArray
Output array (see out).

Returns

NDArray

Example

TypeScript
const spec = np.fft.rfft2([[1, 2, 3, 4], [5, 6, 7, 8]]);
spec.shape;                                     // => [2, 3]
np.fft.irfft2(spec);                            // => [[1, 2, 3, 4], [5, 6, 7, 8]]
np.fft.rfftn(np.ones([2, 3, 4])).shape;         // => [2, 3, 3]
np.fft.irfftn(np.ones([2, 3]), { axes: [0] }).shape; // => [2, 3]
TypeScript declaration
np.fft.rfftn(a: ArrayLike, s?: number[] | FftNOptions | null | undefined, axes?: number[] | null | undefined, norm?: FftNorm | null | undefined, out?: NDArray | null | undefined): NDArray

np.fft.fftshift

#
np.fft.fftshift(x, [axes]) · np.fft.ifftshift(x, [axes])

Moves the zero-frequency term to the centre of the spectrum by rolling each axis in axes (default: all) by shape[ax] // 2. ifftshift undoes it; the two differ for odd lengths.

Parameters

xArrayLike
An NDArray, nested JS array or scalar.
[axes]number | number[] | null
Axes to shift. Default: all.

Returns

NDArray (same dtype)

Example

TypeScript
np.fft.fftshift([0, 1, 2, -2, -1]);      // => [-2, -1, 0, 1, 2]
np.fft.ifftshift([-2, -1, 0, 1, 2]);     // => [0, 1, 2, -2, -1]
np.fft.fftshift([[0, 1, 2], [3, 4, 5]], 1); // => [[2, 0, 1], [5, 3, 4]]
TypeScript declaration
np.fft.fftshift(x: ArrayLike, axes?: number | number[] | null | undefined): NDArray
np.fft.ifftshift(x: ArrayLike, axes?: number | number[] | null | undefined): NDArray