(x)
| 1 | const Complex = require("complex.js"); |
| 2 | |
| 3 | function dft(x) { |
| 4 | const N = x.length; |
| 5 | |
| 6 | // Initialize an array with N elements, filled with 0s |
| 7 | return Array(N) |
| 8 | .fill(new Complex(0, 0)) |
| 9 | .map((temp, i) => { |
| 10 | // Reduce x into the sum of x_k * exp(-2*sqrt(-1)*pi*i*k/N) |
| 11 | return x.reduce((a, b, k) => { |
| 12 | return a.add(b.mul(new Complex(0, (-2 * Math.PI * i * k) / N).exp())); |
| 13 | }, new Complex(0, 0)); // Start accumulating from 0 |
| 14 | }); |
| 15 | } |
| 16 | |
| 17 | function cooley_tukey(x) { |
| 18 | const N = x.length; |