Hello,
I'm tryung to implement Fast Fourier Transformation. And I'm not sure if I understand it correctly.

As I understand when I transform (for example) simple sine wave by FFT, and than make inversion I should get exactly the same sine wave. Am I right?

But in some way it doesn't work for me. I found that code with the friend google:

Qt Code:
  1. #include <complex>
  2. #include <iostream>
  3. #include <valarray>
  4. #include <QVector>
  5. using namespace std;
  6.  
  7. const double PI = 3.141592653589793238460;
  8.  
  9. typedef std::complex<double> Complex;
  10. typedef std::valarray<Complex> CArray;
  11.  
  12. // Cooley–Tukey FFT (in-place, divide-and-conquer)
  13. // Higher memory requirements and redundancy although more intuitive
  14. void fft(CArray& x)
  15. {
  16. const size_t N = x.size();
  17. if (N <= 1) return;
  18.  
  19. // divide
  20. CArray even = x[std::slice(0, N/2, 2)];
  21. CArray odd = x[std::slice(1, N/2, 2)];
  22.  
  23. // conquer
  24. fft(even);
  25. fft(odd);
  26.  
  27. // combine
  28. for (size_t k = 0; k < N/2; ++k)
  29. {
  30. Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
  31. x[k ] = even[k] + t;
  32. x[k+N/2] = even[k] - t;
  33. }
  34. }
  35.  
  36. // inverse fft (in-place)
  37. void ifft(CArray& x)
  38. {
  39. // conjugate the complex numbers
  40. x = x.apply(std::conj);
  41.  
  42. // forward fft
  43. fft( x );
  44.  
  45. // conjugate the complex numbers again
  46. x = x.apply(std::conj);
  47.  
  48. // scale the numbers
  49. x /= x.size();
  50. }
  51.  
  52. int main()
  53. {
  54. const Complex test[] = { 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 };
  55. CArray data(test, 8);
  56.  
  57. // forward fft
  58. fft(data);
  59.  
  60. std::cout << "fft" << std::endl;
  61. for (int i = 0; i < 10; ++i)
  62. {
  63. std::cout << data[i] << std::endl;
  64. }
  65.  
  66. // inverse fft
  67. ifft(data);
  68.  
  69. std::cout << std::endl << "ifft" << std::endl;
  70. for (int i = 0; i < 10; ++i)
  71. {
  72. std::cout << data[i] << std::endl;
  73. }
  74. return 0;
  75. }
To copy to clipboard, switch view to plain text mode 


So as I understand by ifft(data) I should receive the same as it was on the beggining, but the results are quite different. Can anyone help me?
For any help thanks in advance.
Best Regards