Create and manage complex arrays
master[ real0, imaginary0, real1, imaginary1, ... ]. The total array length is fft.size * 2.repository·master·Indexed 43 days ago
https://github.com/indutny/fft.jsA high-performance Radix-4 Fast Fourier Transform (FFT) implementation for JavaScript. Version 4.0.4 provides optimized paths for real and complex number transformations, including methods for forward and inverse transforms, spectrum completion, and complex array management.
[ real0, imaginary0, real1, imaginary1, ... ]. The total array length is fft.size * 2.To use fft.js, create a new instance of the FFT class by providing the desired transform size.
Important Requirements:
size MUST be a power of two.size MUST be greater than 1.const FFT = require('fft.js');
const fft = new FFT(4096);Use inverseTransform to convert complex frequency domain data back into the time domain. Both input and output must be complex arrays.
const FFT = require('fft.js');
const f = new FFT(4096);
const out = f.createComplexArray();
const data = f.createComplexArray(); // ... filled with frequency data ...
f.inverseTransform(out, data);To transform data that is already in a complex format, use transform. Both the input and output must be complex arrays.
const FFT = require('fft.js');
const f = new FFT(4096);
const input = new Array(4096).fill(0);
const out = f.createComplexArray();
// Convert real input to complex format first
const data = f.toComplexArray(input);
// Perform complex transform
f.transform(out, data);If your input data consists only of real numbers (no imaginary parts), use realTransform to perform the FFT. This method is approximately 40% faster than a standard complex transform.
Workflow:
createComplexArray().fft.size.realTransform(output, input).Note on Output: realTransform only fills the left half of the output array. If you need the full symmetric spectrum, you must call completeSpectrum(output) immediately after.
const FFT = require('fft.js');
const f = new FFT(4096);
const realInput = new Array(f.size);
// ... fill realInput with data ...
const out = f.createComplexArray();
// Perform the fast real transform
f.realTransform(out, realInput);
// If you need the full symmetric spectrum:
f.completeSpectrum(out);Core transformation methods.
// input: real array (size fft.size)
// output: complex array
fft.realTransform(output, input);
// input: complex array
// output: complex array
fft.transform(output, input);
// input: complex array
// output: complex array
fft.inverseTransform(output, input);Methods for managing data formats and memory allocation.
// Create a complex array of size fft.size * 2
fft.createComplexArray();
// Convert real input to complex array (interleaved)
// Always provide 'storage' for better performance to avoid GC
fft.toComplexArray(realInput, /* optional */ storage);
// Convert complex array to real array (ignores imaginary parts)
// Always provide 'storage' for better performance to avoid GC
fft.fromComplexArray(complexInput, /* optional */ storage);
// Fill the right half of a spectrum array with complex conjugates of the left half
// Useful after realTransform to get the full symmetric spectrum
fft.completeSpectrum(spectrum);Use the realTransform(out, data) method when your input data consists only of real numbers. This is more efficient for real-valued signals.
Important Requirements:
out and data must be different array instances.data is treated as real values. The output out will be a complex spectrum of length 2 * size (interleaved real and imaginary parts).const size = 512;
const fft = new FFT(size);
const data = new Array(size).fill(0); // Real input
const out = fft.createComplexArray();
fft.realTransform(out, data);The FFT class provides helper methods to convert between real and complex array representations:
createComplexArray(): Creates a new array of length 2 * size initialized to zero, suitable for complex data (interleaved real/imaginary).fromComplexArray(complex, [storage]): Converts a complex array (interleaved [r, i, r, i...]) into a real array (length complex.length / 2).toComplexArray(input, [storage]): Converts a real array into a complex array (interleaved [r, 0, r, 0...]).const size = 1024;
const fft = new FFT(size);
// Create a zeroed complex buffer
const complexBuf = fft.createComplexArray();
// Convert interleaved complex [r, i, r, i] to real [r, r]
const realBuf = fft.fromComplexArray(complexBuf);
// Convert real [r, r] back to complex [r, 0, r, 0]
const backToComplex = fft.toComplexArray(realBuf);Use the transform(out, data) method to perform a forward complex Fourier transform.
Important Requirements:
out and data must be different array instances (the method throws an error if they are the same).N, data should contain the input complex values, and out will receive the transformed values. Both arrays should typically be of length 2 * N (interleaved real and imaginary parts).const size = 512;
const fft = new FFT(size);
const data = fft.createComplexArray(); // Fill with data
const out = fft.createComplexArray();
fft.transform(out, data);When performing a real-to-complex transform, the resulting spectrum is symmetric. Use completeSpectrum(spectrum) to fill the second half of the spectrum array to match the full complex size (2 * size). This is useful for operations that expect a full-length complex spectrum.
const size = 512;
const fft = new FFT(size);
const spectrum = fft.createComplexArray();
// ... after realTransform ...
fft.completeSpectrum(spectrum);To use fft.js, instantiate the FFT class by providing the desired transform size. The size must be a power of two and greater than 1. If an invalid size is provided, the constructor will throw an error.
Note that the class performs operations on arrays. For complex transforms, the input and output arrays must have a length of 2 * size to accommodate real and imaginary parts.
const FFT = require('fft.js');
const size = 1024;
const fft = new FFT(size);