summaryrefslogtreecommitdiff
path: root/math/transforms/fft.cpp
diff options
context:
space:
mode:
authormzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
committermzuenni <michi.zuendorf@gmail.com>2022-06-27 17:19:28 +0200
commit5ab8a5088b729a9953b8dff1b2a985dc8fb2098b (patch)
treeed40d6936c0e9eee40ba62751cbf99ecddbaddc2 /math/transforms/fft.cpp
parentadabbad9c51cf7cd3874bfde8eac1fbcf84fec10 (diff)
updated tcr
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;
+}}}}