fork download
  1. import numpy as np
  2. from scipy.fftpack import fft, ifft, rfft
  3. from scipy.signal import fftconvolve
  4. import matplotlib.pyplot as plt
  5. #-----------------------------------------
  6.  
  7. # cyclic convolution
  8. def convolute_cyc (f, g):
  9. Nf = len(f)
  10. Ng = len(g)
  11. Nc = Nf
  12. c = np.zeros_like(f, dtype= float)
  13.  
  14. for n in xrange(Nc):
  15. for m in xrange(Nf):
  16. c[n] += f[m] * g[n-m]
  17. #end for
  18.  
  19. return c
  20.  
  21. # acyclic convolution
  22. def convolute_acyc (f, g):
  23. Nf = len(f)
  24. Ng = len(g)
  25. Nc = Nf + Ng - 1
  26. c = np.zeros(shape= (Nc), dtype= float)
  27.  
  28. for n in xrange(Nc):
  29. for m in xrange(Nf):
  30. if m <= n and m > n - Ng:
  31. c[n] += f[m] * g[n-m]
  32. #endif
  33. #endfor
  34. #endfor
  35.  
  36. return c
  37. #----------------------------------------
  38. # convolution example
  39.  
  40. T0 = 1.
  41. a0 = 1.
  42.  
  43. def F (t):
  44. if np.abs(t) <= T0:
  45. return 1.
  46. #else:
  47. return 0.
  48.  
  49. def G (t):
  50. if t > 0:
  51. return np.exp(-a0 * t)
  52. # else:
  53. return 0.
  54.  
  55. # exact convolution
  56. def C (t):
  57. if -T0 < t <= T0:
  58. return ( 1. - np.exp(-a0 * (t + T0)) ) / a0
  59. elif t > T0:
  60. return ( np.exp(a0 * T0) - np.exp(-a0 * T0) ) * np.exp(-a0*t) / a0
  61. # else:
  62. return 0.
  63.  
  64. LL = 6*T0 # boundary of x-axis
  65. Npt = 2**10 # nr of points on x-axis
  66. X = np.linspace(-LL,LL, Npt) # x-axis
  67.  
  68. # arrays
  69. f = np.asarray( map(F, X), dtype= float)
  70. g = np.asarray( map(G, X), dtype= float)
  71. c_exact = np.asarray( map(C, X), dtype= float)
  72.  
  73. # cyclic convolution
  74. c_cyc = convolute_cyc(f, g)
  75.  
  76. # non-cyclic convolution
  77. c_acyc = convolute_acyc(f, g)
  78.  
  79. # non-cyclic convolution by zero-padding
  80. NPAD = Npt-1 # length of zero-padding
  81. f_pad = np.concatenate( (f, [0.]*NPAD ) ) # (f | zero-pad of length NPAD)
  82. g_pad = np.concatenate( (g, [0.]*NPAD ) ) # (g | zero-pad of length NPAD)
  83. c_fft = ifft( fft(f_pad) * fft(g_pad) ) # convolution using fftpack
  84.  
  85. #----------------------------------------
  86. # plotting
  87.  
  88. YMAX = max(c_exact)
  89. plt.plot(X, f, 'g-', lw= 1, label= "F")
  90. plt.plot(X, g, 'r-', lw= 1, label= "G")
  91. plt.plot(X, c_exact, 'b-', lw= 2, label= "exact conv.")
  92. plt.plot((X[0], X[-1]), (YMAX, YMAX), 'k--')
  93. plt.ylim([0, 1.5*YMAX])
  94. plt.legend()
  95. plt.show()
  96.  
  97. plt.plot(c_fft.real, 'g-', lw= 1, label= "FFT conv.")
  98. plt.plot(c_exact, 'b-', lw= 2, label= "exact conv.")
  99. plt.legend()
  100. plt.show()
  101.  
  102. plt.plot(c_acyc.real, 'g-', lw= 1, label= "conv. acyclic")
  103. plt.plot(c_exact, 'b-', lw= 2, label= "exact conv.")
  104. plt.legend()
  105. plt.show()
Runtime error #stdin #stdout #stderr 0.5s 54224KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 4, in <module>
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1102, in <module>
    rcParams = rc_params()
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 949, in rc_params
    fname = matplotlib_fname()
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 770, in matplotlib_fname
    configdir = _get_configdir()
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 635, in _get_configdir
    return _get_config_or_cache_dir(_get_xdg_config_dir())
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 612, in _get_config_or_cache_dir
    return _create_tmp_config_dir()
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 544, in _create_tmp_config_dir
    tempdir = os.path.join(tempdir, 'matplotlib-%s' % getpass.getuser())
  File "/usr/lib/python2.7/getpass.py", line 158, in getuser
    return pwd.getpwuid(os.getuid())[0]
KeyError: 'getpwuid(): uid not found: 20120'