r/Python • • Aug 28 '13

Understanding the FFT Algorithm (with Python examples)

http://jakevdp.github.io/blog/2013/08/28/understanding-the-fft/
135 Upvotes

13 comments sorted by

View all comments

6

u/Antagonist360 Aug 29 '13 edited Aug 29 '13

There was a Reddit ELI5 post asking about the FFT a while ago that I had commented on and supplied python code for (see below). I ended up copying my response into a blog post.

from cmath import exp,pi

def FFT(X):
    n = len(X)
    w = exp(-2*pi*1j/n)
    if n > 1:
        X = FFT(X[::2]) + FFT(X[1::2])
        for k in xrange(n/2):
            xk = X[k]
            X[k] = xk + w**k*X[k+n/2]
            X[k+n/2] = xk - w**k*X[k+n/2]
    return X  

EDIT woops forgot first two lines of code
EDIT didn't know about cmath exp

4

u/Araucaria Aug 29 '13

...never mind.