Advertisement
Guest User

Untitled

a guest
May 15th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. from math import *
  2. import matplotlib.pyplot as plt
  3. import matplotlib.image as mpimg
  4. import numpy as np
  5. from numpy import ma
  6. from numpy.fft import *
  7.  
  8. fig1 = plt.figure(1)
  9. sub1 = fig1.add_subplot(111)
  10.  
  11. fig2 = plt.figure(2)
  12. sub2 = fig2.add_subplot(111)
  13.  
  14. test_num = 3
  15.  
  16. if test_num == 1:
  17. ## square_bump = [0]*100 + [1]*20 + [0]*100
  18. ## square_bumps = ([0]*10+[1]*5)*10+[0]*10
  19. X = np.arange(-7,7.05,0.1)
  20.  
  21. ## sinusoidal = [sin(3*x) for x in X]
  22. pulse = [cos(2*pi*3*x)*exp(-pi*x**2) for x in X]
  23.  
  24. L = pulse
  25.  
  26. sub1.plot(X, L, 'b-')
  27.  
  28. F = fft(L)
  29. sub2.plot(X, list(map(lambda x: abs(x)**2,F)))
  30.  
  31. elif test_num == 2:
  32. src_img = mpimg.imread("Lenna.png")
  33. img_red = src_img[:,:,:]
  34.  
  35. sub1.imshow(src_img)
  36. sub2.imshow(img_red, cmap='Greys_r')
  37. ## sub2.set_cmap('Greys')
  38.  
  39. fig3 = plt.figure(3)
  40. sub3 = fig3.add_subplot(111)
  41.  
  42. F = fftn(src_img)
  43. F_red = fft2(src_img[:,:,0])
  44. F_green = fft2(src_img[:,:,1])
  45. F_blue = fft2(src_img[:,:,2])
  46. ## fftshift(F)
  47. sub3.imshow(np.log10( np.abs( F ) ))#, cmap='Greys_r')
  48.  
  49. ## F_masked = F
  50. ## xx, yy = np.meshgrid( *[1 if abs(x-256)<=128 else 0 for x in range(512)]*2 )
  51. ## mask = xx*yy
  52. ## mask = [[1]*512]*128 + [[1 if abs(x-256)>128 else 0 for x in range(512)]]*256 + [[1]*512]*128
  53. ## mask = np.array(mask)#.reshape(512,512)
  54. foo = np.logical_and(np.arange(512)>=50, np.arange(512)<=462)
  55. bar = [1 if 100 < abs(k-256) < 200 else 0 for k in range(512)]
  56. mask = np.logical_and.outer(foo, foo)
  57. ## mask = np.logical_and.outer( *[np.logical_or(np.arange(512) < 100, np.arange(512) > 412)]*2 )
  58. mask = np.reshape(np.repeat(mask, 3, 1), mask.shape+(3,))
  59.  
  60. F_masked = np.multiply( 1-mask, F )#*(cos(pi/4)+sin(pi/4)*1j) + np.multiply( 1-mask, F )
  61. ## F_masked = F * -1#(cos(pi/4)+sin(pi/4)*1j)/2 + F/2
  62.  
  63. fig4 = plt.figure(4)
  64. sub4 = fig4.add_subplot(111)
  65. sub4.imshow(np.log10( np.abs( F_masked )+1 ), cmap='Greys_r')
  66.  
  67. fig5 = plt.figure(5)
  68. sub5 = fig5.add_subplot(111)
  69.  
  70. iF = ifftn(F_masked)
  71. ## fftshift(F2)
  72. sub5.imshow( abs(iF), cmap='Greys_r')
  73.  
  74. elif test_num == 3:
  75. X = np.arange(-7,7.05,0.1)
  76. Y = [floor(x)-x for x in X]
  77. ## Y = X
  78. ## Y = [.5*x + cos(3*x) for x in X]
  79. ## Y = [ceil(x)*2 + cos(x*pi)*(2*(floor(x)%2)-1) for x in X]
  80. ## Y = [(x//pi)*2 + cos(x%pi + pi) for x in X]
  81.  
  82. sub1.plot(X, Y, 'b-')
  83.  
  84. F = fft(Y)
  85. ## F2 = ifft(Y)
  86. sub2.plot(X, list(map(lambda x: abs(x),F)))
  87. ## sub2.plot(X, list(map(lambda x: 125*abs(x),F2)))
  88.  
  89. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement