mramine364

DFT_slow and FFT

Jul 1st, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def DFT_slow(x):
  4.     x = np.asarray(x, dtype=float)
  5.     N = x.shape[0]
  6.     n = np.arange(N)
  7.     k = n.reshape((N, 1))
  8.     M = np.exp(-2j * np.pi * k * n / N)
  9.     return np.dot(M, x)
  10.  
  11. def FFT(x):
  12.     x = np.asarray(x, dtype=float)
  13.     N = x.shape[0]
  14.     if N % 2 > 0:
  15.         raise ValueError("size of x must be a power of 2")
  16.     elif N <= 4:  # this cutoff should be optimized
  17.         return DFT_slow(x)
  18.     else:
  19.         X_even = FFT(x[::2])
  20.         X_odd = FFT(x[1::2])
  21.         factor = np.exp(-2j * np.pi * np.arange(N) / N)
  22.         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