Skip to content

Instantly share code, notes, and snippets.

@Eight1911
Last active March 28, 2022 20:54
Show Gist options
  • Save Eight1911/b7cc6b8cc9645822f0d340c3a4b056e1 to your computer and use it in GitHub Desktop.
Save Eight1911/b7cc6b8cc9645822f0d340c3a4b056e1 to your computer and use it in GitHub Desktop.
Naive implementation of non-recursive radix-2 fast Fourier transform in Python.
import math
# iterative cooley tukey fft for dyadics
def fft(v):
n, h = len(v), len(v) >> 1
old, new = v[:], [0] * n
sublen, stride = 1, n
while sublen < n:
stride >>= 1
for i in range(stride):
for k in range(0, n, 2*stride):
omega = math.exp(math.pi * k / n)
new[i+(k>>1)] = old[i+k] + omega * old[i+k+stride]
new[i+(k>>1)+h] = old[i+k] - omega * old[i+k+stride]
old, new = new, old
sublen <<= 1
return old
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment