npm.io
1.0.3 • Published yesterday

discrete-fourier-transform

Licence
Unlicense
Version
1.0.3
Deps
0
Size
10 kB
Vulns
0
Weekly
0

discrete-fourier-transform

DFT and FFT for complex (or real) signals, with no dependencies.

Two implementations:

  • dft / idft — direct O(n²) transforms that work for any length.
  • fft / ifft — iterative radix-2 Cooley-Tukey, O(n log n), for power-of-two lengths.

Signals are arrays of { re, im }. Real-number arrays are accepted anywhere and are promoted to complex automatically.

The forward transform uses the exp(-2πi·kn/N) sign convention and the inverse divides by N, matching NumPy so the bins line up.

Install

npm install discrete-fourier-transform

Usage

import { dft, idft, fft, ifft, magnitude } from "discrete-fourier-transform";

dft([1, 1, 1, 1]);
// [{re:4,im:0}, {re:0,im:0}, {re:0,im:0}, {re:0,im:0}]

// round trip
const x = [3, 1, 4, 1, 5, 9, 2];
idft(dft(x)); // ~= x

// fast path for power-of-two lengths
ifft(fft([0, 1, 2, 3, 4, 5, 6, 7]));

// magnitude spectrum
magnitude([1, 0, -1, 0]);

API

  • dft(signal) / idft(spectrum) — direct transforms, any length.
  • fft(signal) / ifft(spectrum) — radix-2 transforms; throws if the length is not a power of two.
  • magnitude(signal)|X[k]| per bin.
  • complexify(signal) — promote reals to { re, im }.
  • complex(x) — coerce a number or partial object to a full complex value.

License

Released into the public domain under the Unlicense.