Advertisement
slash0t

fft

Aug 1st, 2024 (edited)
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #define comp complex<double>
  2. void fft(vector<comp>&a, bool inv) {
  3.     int n = a.size();
  4.     if (n == 1) {
  5.         return;
  6.     }
  7.  
  8.     vector<comp> a0(n / 2), a1(n / 2);
  9.     for (int i = 0; i < n / 2; i++) {
  10.         a0[i] = a[2 * i];
  11.         a1[i] = a[2 * i + 1];
  12.     }
  13.     fft(a0, 0);
  14.     fft(a1, 0);
  15.  
  16.     double angle = 2 * M_PI / n;
  17.     comp w(1), wn(cos(angle), sin(angle));
  18.  
  19.     for (int i = 0; i < n / 2; i++) {
  20.         a[i] = a0[i] + w * a1[i];
  21.         a[i + n / 2] = a0[i] - w * a1[i];
  22.         w *= wn;
  23.     }
  24.  
  25.     if (inv) {
  26.         for (int i = 0; i < n; i++) a[i] /= n;
  27.         reverse(++a.begin(), a.end());
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement