SHARE
TWEET

Untitled

a guest Oct 12th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from scipy import array, zeros, signal
  2. from scipy.fftpack import fft, ifft, convolve
  3. def conv(f, g):
  4.   # transform f and g to frequency domain
  5.   F = fft(f)
  6.   G = fft(g)
  7.   # multiply entry-wise
  8.   C = F * G
  9.   # transfer C to time domain
  10.   c = ifft(C)
  11.  
  12.   return c
  13.    
  14. conv(f,g) = [ 34.+0.j  32.+0.j  34.+0.j  40.+0.j]
  15. convolve.convolve(f,g) = [  1.48219694e-322   2.07507571e-322   3.06320700e-322   3.26083326e-322]
  16. signal.convolve(f,g,'same') = [14 26 40 29]
  17.    
  18. def direct(f,g):
  19.     r = zeros(len(f))
  20.     for k in range(len(f)):
  21.         for p in range(len(f)):
  22.             r[k] = r[k] + (f[p] * g[k-p])
  23.     return r
  24.    
  25. f = np.concatenate((f, np.zeros(4)))
  26. g = np.concatenate((g, np.zeros(4)))
RAW Paste Data
Top