0% found this document useful (0 votes)
95 views1 page

FFT Test

The document contains C++ code for performing the fast Fourier transform (FFT) and inverse fast Fourier transform (IFFT) on a complex number sequence. It defines functions for recursively splitting the sequence, performing bit reversal sorting, and applying the Fourier and inverse Fourier transforms by calling the splitting and sorting functions.

Uploaded by

rokia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views1 page

FFT Test

The document contains C++ code for performing the fast Fourier transform (FFT) and inverse fast Fourier transform (IFFT) on a complex number sequence. It defines functions for recursively splitting the sequence, performing bit reversal sorting, and applying the Fourier and inverse Fourier transforms by calling the splitting and sorting functions.

Uploaded by

rokia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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