8 #include "FourierTransform.h"
14 FourierTransform::FourierTransform() {};
17 FourierTransform::~FourierTransform() {};
27 std::vector< std::complex<double> >
28 FourierTransform::Transform(std::vector< std::complex<double> > input) {
31 int n = NextPowerOfTwo(input.size());
32 vector< std::complex<double> > output(n);
37 for(
int i = 0; i < n; i++) {
38 if(output[i] == 0.0) {
39 int j = BitReverse(n, i);
47 for(
int m = 1; m < n; m *= 2) {
48 complex<double> Wm(polar(1.0, -1.0 *
PI / m));
49 for(
int k = 0; k < n; k += 2 * m) {
51 complex<double> W(1.0);
52 for(
int j = 0; j < m; j++) {
53 complex<double> t = W * output[k+j+m];
54 complex<double> u = output[k+j];
56 output[k+j+m] = u - t;
74 std::vector< std::complex<double> >
75 FourierTransform::Inverse(std::vector< std::complex<double> > input) {
78 std::vector< std::complex<double> > temp(n);
79 for(
int i = 0; i < n; i++) {
80 temp[i] = conj(input[i]);
83 std::vector< std::complex<double> > output =
Transform(temp);
85 for(
int i = 0; i < n; i++) {
86 output[i] = conj(output[i]) / ((double)n);
100 bool FourierTransform:: IsPowerOfTwo(
int n) {
102 if(n % 2 == 1)
return false;
116 int FourierTransform:: lg(
int n) {
137 int FourierTransform::BitReverse(
int n,
int x) {
149 return(
int)(ans * n / 2);
160 int FourierTransform::NextPowerOfTwo(
int n) {
161 if(IsPowerOfTwo(n))
return n;
162 return(
int)pow(2.0, lg(n) + 1);