fft.js

repository·master·Indexed 43 days ago

https://github.com/indutny/fft.js

A 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.

Tokens
2.3K
Snippets
12
Records
13
Agent score
7%

What's inside fft.js

  1. Initialize the FFT instance

    master

    To use fft.js, create a new instance of the FFT class by providing the desired transform size.

    Important Requirements:

    • The size MUST be a power of two.
    • The size MUST be greater than 1.
    const FFT = require('fft.js');
    
    const fft = new FFT(4096);
  2. Perform an Inverse Fourier transform

    master

    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);
  3. Perform a Complex FFT transformation

    master

    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);
  4. Perform a Real FFT transformation

    master

    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:

    1. Create a complex output array using createComplexArray().
    2. Provide a real input array of size fft.size.
    3. Call 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);
  5. Reference: FFT Methods

    master

    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);
  6. Reference: Input/Output helper methods

    master

    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);
  7. Perform a real Fourier transform

    master

    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.
    • The input 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);
  8. Manage complex array formats

    master

    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);
  9. Perform a complex Fourier transform

    master

    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).
    • For a transform of size 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);
  10. Complete a spectrum for symmetry

    master

    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);
  11. Initialize the FFT class

    master

    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);