FFT Test

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include <complex>

#include <cmath>
#include <utility>

typedef std::complex<double> complex_t;

void F(int N, int q, complex_t* x)


// N : sequence length
// q : block start point (initial value is 0)
// x : input/output sequence
{
const int m = N/2;
const double theta0 = 2*M_PI/N;

if (N > 1) {
for (int p = 0; p < m; p++) {
const complex_t wp = complex_t(cos(p*theta0), -sin(p*theta0));
const complex_t a = x[q + p + 0];
const complex_t b = x[q + p + m];
x[q + p + 0] = a + b;
x[q + p + m] = (a - b) * wp;
}
F(N/2, q + 0, x); // even position components
F(N/2, q + m, x); // odd position components
}
}

void bit_reverse(int N, complex_t* x) // bit reversal sorting


// N : sequence length
// x : input/output sequence
{
for (int i = 0, j = 1; j < N-1; j++) {
for (int k = N >> 1; k > (i ^= k); k >>= 1);
if (i < j) std::swap(x[i], x[j]); // swap x[i] and x[j]
}
}

void fft(int N, complex_t* x) // Fourier transform


// N : sequence length
// x : input/output sequence
{
F(N, 0, x);
bit_reverse(N, x);
}

void ifft(int N, complex_t* x) // Inverse Fourier transform


// N : sequence length
// x : input/output sequence
{
for (int p = 0; p < N; p++) x[p] = conj(x[p]);
F(N, 0, x);
bit_reverse(N, x);
for (int k = 0; k < N; k++) x[k] = conj(x[k])/double(N);
}

You might also like