function transformRadix2(real, imag) {
    // Initialization
    var i, j, k;
    if (real.length !== imag.length)
        throw "Mismatched lengths";
    var n = real.length;
    if (n === 1) // Trivial transform
        return;
    var levels = -1;
    for (i = 0; i < 32; i++) {
        if (1 << i === n)
            levels = i; // Equal to log2(n)
    }
    if (levels === -1)
        throw "Length is not a power of 2";
    var cosTable = new Array(n / 2);
    var sinTable = new Array(n / 2);
    for (i = 0; i < n / 2; i++) {
        cosTable[i] = Math.cos(2 * Math.PI * i / n);
        sinTable[i] = Math.sin(2 * Math.PI * i / n);
    }

    // Bit-reversed addressing permutation
    for (i = 0; i < n; i++) {
        j = reverseBits(i, levels);
        if (j > i) {
            var temp = real[i];
            real[i] = real[j];
            real[j] = temp;
            temp = imag[i];
            imag[i] = imag[j];
            imag[j] = temp;
        }
    }

    // Cooley-Tukey decimation-in-time radix-2 FFT
    for (var size = 2; size <= n; size *= 2) {
        var halfsize = size / 2;
        var tablestep = n / size;
        for (i = 0; i < n; i += size) {
            for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) {
                var tpre = real[j + halfsize] * cosTable[k] + imag[j + halfsize] * sinTable[k];
                var tpim = -real[j + halfsize] * sinTable[k] + imag[j + halfsize] * cosTable[k];
                real[j + halfsize] = real[j] - tpre;
                imag[j + halfsize] = imag[j] - tpim;
                real[j] += tpre;
                imag[j] += tpim;
            }
        }
    }

    // Returns the integer whose value is the reverse of the lowest 'bits' bits of the integer 'x'.
    function reverseBits(x, bits) {
        var y = 0;
        for (var i = 0; i < bits; i++) {
            y = (y << 1) | (x & 1);
            x >>>= 1;
        }
        return y;
    }
}
/* 
 * Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector.
 * The vector's length must be a power of 2. Uses the Cooley-Tukey decimation-in-time radix-2 algorithm.
 */
Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector.
The vector's length must be a power of 2. Uses the Cooley-Tukey decimation-in-time radix-2 algorithm.
Computes the discrete Fourier transform ( DFT ) of the given complex vector storing the result back into the vector . The vector ' s length must be a power of 2 . Uses the Cooley - Tukey decimation - in - time radix - 2 algorithm .
function transformRadix2(real, imag) {
    var i, j, k;
    if (real.length !== imag.length)
        throw "Mismatched lengths";
    var n = real.length;
    if (n === 1) 
        return;
    var levels = -1;
    for (i = 0; i < 32; i++) {
        if (1 << i === n)
            levels = i; 
    }
    if (levels === -1)
        throw "Length is not a power of 2";
    var cosTable = new Array(n / 2);
    var sinTable = new Array(n / 2);
    for (i = 0; i < n / 2; i++) {
        cosTable[i] = Math.cos(2 * Math.PI * i / n);
        sinTable[i] = Math.sin(2 * Math.PI * i / n);
    }
    for (i = 0; i < n; i++) {
        j = reverseBits(i, levels);
        if (j > i) {
            var temp = real[i];
            real[i] = real[j];
            real[j] = temp;
            temp = imag[i];
            imag[i] = imag[j];
            imag[j] = temp;
        }
    }
    for (var size = 2; size <= n; size *= 2) {
        var halfsize = size / 2;
        var tablestep = n / size;
        for (i = 0; i < n; i += size) {
            for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) {
                var tpre = real[j + halfsize] * cosTable[k] + imag[j + halfsize] * sinTable[k];
                var tpim = -real[j + halfsize] * sinTable[k] + imag[j + halfsize] * cosTable[k];
                real[j + halfsize] = real[j] - tpre;
                imag[j + halfsize] = imag[j] - tpim;
                real[j] += tpre;
                imag[j] += tpim;
            }
        }
    }
    function reverseBits(x, bits) {
        var y = 0;
        for (var i = 0; i < bits; i++) {
            y = (y << 1) | (x & 1);
            x >>>= 1;
        }
        return y;
    }
}
function transformRadix2 ( real , imag ) { var i , j , k ; if ( real . length !== imag . length ) throw "Mismatched lengths" ; var n = real . length ; if ( n === 1 ) return ; var levels = - 1 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( 1 << i === n ) levels = i ; } if ( levels === - 1 ) throw "Length is not a power of 2" ; var cosTable = new Array ( n / 2 ) ; var sinTable = new Array ( n / 2 ) ; for ( i = 0 ; i < n / 2 ; i ++ ) { cosTable [ i ] = Math . cos ( 2 * Math . PI * i / n ) ; sinTable [ i ] = Math . sin ( 2 * Math . PI * i / n ) ; } for ( i = 0 ; i < n ; i ++ ) { j = reverseBits ( i , levels ) ; if ( j > i ) { var temp = real [ i ] ; real [ i ] = real [ j ] ; real [ j ] = temp ; temp = imag [ i ] ; imag [ i ] = imag [ j ] ; imag [ j ] = temp ; } } for ( var size = 2 ; size <= n ; size *= 2 ) { var halfsize = size / 2 ; var tablestep = n / size ; for ( i = 0 ; i < n ; i += size ) { for ( j = i , k = 0 ; j < i + halfsize ; j ++ , k += tablestep ) { var tpre = real [ j + halfsize ] * cosTable [ k ] + imag [ j + halfsize ] * sinTable [ k ] ; var tpim = - real [ j + halfsize ] * sinTable [ k ] + imag [ j + halfsize ] * cosTable [ k ] ; real [ j + halfsize ] = real [ j ] - tpre ; imag [ j + halfsize ] = imag [ j ] - tpim ; real [ j ] += tpre ; imag [ j ] += tpim ; } } } function reverseBits ( x , bits ) { var y = 0 ; for ( var i = 0 ; i < bits ; i ++ ) { y = ( y << 1 ) | ( x & 1 ) ; x >>>= 1 ; } return y ; } }
Computes the discrete Fourier transform (DFT) of the given complex vector, storing the result back into the vector.
Computes the discrete Fourier transform ( DFT ) of the given complex vector storing the result back into the vector .
// Initialization // Trivial transform // Equal to log2(n) // Bit-reversed addressing permutation // Cooley-Tukey decimation-in-time radix-2 FFT // Returns the integer whose value is the reverse of the lowest 'bits' bits of the integer 'x'.