sgarizvi

Discrete Fourier Transform

May 11th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <complex>
  2. #include <vector>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. typedef complex<double> cdouble;
  8.  
  9. const double PI2  = 2 * 3.141593654;
  10.  
  11. vector<cdouble> fft(const vector<double>& input)
  12. {
  13.     int N = input.size();
  14.  
  15.     vector<cdouble> output(N);
  16.  
  17.     for (int k=0; k<N; k++)
  18.     {
  19.         cdouble out_val(0,0);
  20.  
  21.         for(int n=0; n<N; n++)
  22.         {
  23.             out_val += cdouble(input[n],0.0) * cdouble(cos( (PI2 * k * n) / N ), sin( (PI2 * k * n) / N ));
  24.         }
  25.  
  26.         output[k] = out_val;
  27.     }
  28.     return output;
  29. }
  30.  
  31. vector<cdouble> fft(const vector<double>& input, int N)
  32. {
  33.     vector<cdouble> output(N);
  34.  
  35.     for (int k=0; k<N; k++)
  36.     {
  37.         cdouble out_val(0,0);
  38.  
  39.         for(int n=0; n<input.size(); n++)
  40.         {
  41.             out_val += cdouble(input[n],0.0) * cdouble(cos( (PI2 * k * n) / N ), sin( (PI2 * k * n) / N ));
  42.         }
  43.  
  44.         output[k] = out_val;
  45.     }
  46.     return output;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment