Skip to content

Instantly share code, notes, and snippets.

@STRd6
Created October 21, 2013 02:39
Show Gist options
  • Save STRd6/7077922 to your computer and use it in GitHub Desktop.
Save STRd6/7077922 to your computer and use it in GitHub Desktop.
Discrete Fourier transform in CoffeeScript.
{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