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

7

u/Thirsteh Aug 29 '13

I'd like to see the five-year-old that understood that snippet! :)

Nice blog post!

5

u/Antagonist360 Aug 29 '13

Thanks! Didn't Gauss come up with the FFT at age 5?

9

u/TheBB Aug 29 '13 edited Aug 29 '13

Geez, I thought you were joking, but it's true.

Edit: Although it was in 1805, so he was at least 27. Maybe you got it mixed up with the arithmetic sum story?

3

u/Antagonist360 Aug 29 '13

I thought I was joking too.