Skip to content

Instantly share code, notes, and snippets.

@mikezink

mikezink/FFT.py Secret

Created November 4, 2019 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikezink/4e8d9ff00d955f3cde56d8522e16f58c to your computer and use it in GitHub Desktop.
Save mikezink/4e8d9ff00d955f3cde56d8522e16f58c to your computer and use it in GitHub Desktop.
# Recursive FFT function
import numpy as np
from timeit import Timer
def FFT(x):
N = len(x)
if N <= 1: return x
even = FFT(x[0::2])
odd = FFT(x[1::2])
T = [np.exp(-2j * np.pi * k / N) * odd[k] for k in range(N // 2)]
return [even[k] + T[k] for k in range(N // 2)] + \
[even[k] - T[k] for k in range(N // 2)]
N = 1024
x = np.random.random(N)
t = Timer(lambda: FFT(x))
print('Elapsed time: {} s'.format(str(t.timeit(number=1))))
# FFT example using the Numpy fftpack
import numpy as np
from timeit import Timer
N = 10000
x = np.arange(N)
t = Timer(lambda: np.fft.fft(x))
print('Elapsed time: {} s'.format(str(t.timeit(number=1))))
# FFT example using the SciPy fftpack
import scipy
from scipy.fftpack import fft
from timeit import Timer
N = 10000
x = scipy.arange(N)
t = Timer(lambda: fft(x))
print('Elapsed time: {} s'.format(str(t.timeit(number=1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment