summaryrefslogtreecommitdiff
path: root/math/transforms/fft.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'math/transforms/fft.cpp')
-rw-r--r--math/transforms/fft.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/math/transforms/fft.cpp b/math/transforms/fft.cpp
new file mode 100644
index 0000000..4540ed8
--- /dev/null
+++ b/math/transforms/fft.cpp
@@ -0,0 +1,20 @@
+using cplx = complex<double>; // Eigene Implementierung ist schneller.
+
+void fft(vector<cplx>& a, bool inverse = 0) {
+ int n = sz(a);
+ for (int i = 0, j = 1; j < n - 1; ++j) {
+ for (int k = n >> 1; k > (i ^= k); k >>= 1);
+ if (j < i) swap(a[i], a[j]);
+ }
+ for (int s = 1; s < n; s *= 2) {
+ double angle = PI / s * (inverse ? -1 : 1);
+ cplx ws(cos(angle), sin(angle));
+ for (int j = 0; j < n; j+= 2 * s) {
+ cplx w = 1;
+ for (int k = 0; k < s; k++) {
+ cplx u = a[j + k], t = a[j + s + k] * w;
+ a[j + k] = u + t;
+ a[j + s + k] = u - t;
+ if (inverse) a[j + k] /= 2, a[j + s + k] /= 2;
+ w *= ws;
+}}}}