Created
October 21, 2013 02:39
-
-
Save STRd6/7077922 to your computer and use it in GitHub Desktop.
Discrete Fourier transform in CoffeeScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{cos, sin, sqrt} = Math | |
τ = 2 * Math.PI | |
DFT = (series) -> | |
N = series.length | |
rootN = sqrt(N) | |
divRootN = (x) -> x / rootN | |
[0...N].map (k) -> | |
series.map ([x, y], n) -> | |
theta = -τ * k * n / N | |
[ | |
x * cos(theta) + y * sin(theta) | |
x * -sin(theta) + y * -cos(theta) | |
] | |
.reduce((a, b) -> | |
[a[0] + b[0], a[1] + b[1]] | |
, [0, 0]) | |
.map divRootN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment