Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def DFT_slow(x):
- x = np.asarray(x, dtype=float)
- N = x.shape[0]
- n = np.arange(N)
- k = n.reshape((N, 1))
- M = np.exp(-2j * np.pi * k * n / N)
- return np.dot(M, x)
- def FFT(x):
- x = np.asarray(x, dtype=float)
- N = x.shape[0]
- if N % 2 > 0:
- raise ValueError("size of x must be a power of 2")
- elif N <= 4: # this cutoff should be optimized
- return DFT_slow(x)
- else:
- X_even = FFT(x[::2])
- X_odd = FFT(x[1::2])
- factor = np.exp(-2j * np.pi * np.arange(N) / N)
- return np.concatenate([X_even + factor[:N / 2] * X_odd, X_even + factor[N / 2:] * X_odd])
Advertisement
Add Comment
Please, Sign In to add comment