FFT: How could this algorithm be modified to return to coefficient representation?

StackOverflow https://stackoverflow.com/questions/15565934

  •  29-03-2022
  •  | 
  •  

Pregunta

The following is a base-2 implementation of the Cooley-Tukey FFT algorithm (found on Rosetta Code). After one run of FFT, the data array will go from coefficient to point-value representation. How do you convert back to coefficient?

#include <complex>
#include <iostream>
#include <valarray>

const double PI = 3.141592653589793238460;

typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

// Cooley–Tukey FFT (in-place)
void fft(CArray& x)
{
    const size_t N = x.size();
    if (N <= 1) return;

    // divide
    CArray even = x[std::slice(0, N/2, 2)];
    CArray  odd = x[std::slice(1, N/2, 2)];

    // conquer
    fft(even);
    fft(odd);

    // combine
    for (size_t k = 0; k < N/2; ++k)
    {
        Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
        x[k    ] = even[k] + t;
        x[k+N/2] = even[k] - t;
    }
}

int main()
{
    const Complex test[] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };
    CArray data(test, 8);

    fft(data);

    for (int i = 0; i < 8; ++i)
    {
        std::cout << data[i] << "\n";
    }
    return 0;
}
¿Fue útil?

Solución

Compute an inverse FFT

Change

-2 * PI * k / N

to

2 * PI * k / N

And after doing the inverse FFT, scale the outputs by 1/N

Otros consejos

Added to the Rosetta Code

// inverse fft (in-place)
void ifft(CArray& x)
{
    // conjugate the complex numbers
    std::transform(&x[0], &x[x.size()], &x[0], std::conj<double>);

    // forward fft
    fft( x );

    // conjugate the complex numbers again
    std::transform(&x[0], &x[x.size()], &x[0], std::conj<double>);

    // scale the numbers
    x /= x.size();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top