Guest User

Untitled

a guest
Mar 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. """
  5. fft2 playground.
  6. """
  7.  
  8. # Initialise an empty array
  9. field = np.empty([29, 481])
  10.  
  11. # Set the wave amplitude
  12. amp = 2.5
  13.  
  14. # Create the synthetic sinusoidal field
  15. for n, i in enumerate(np.linspace(0, 2 * np.pi, field.shape[0])):
  16. for m, j in enumerate(np.linspace(0, 2 * np.pi, field.shape[1])):
  17. field[n, m] = amp * np.sin(j)
  18.  
  19. # Perform 2D fourier and shift the result to centre
  20. f = np.fft.fft2(field)
  21. fshift = np.fft.fftshift(f)
  22.  
  23. # Calculate the magnitude and phase spectra
  24. magnitude_spectrum = 20*np.log(np.abs(fshift))
  25. phase_spectrum = np.angle(fshift)
  26.  
  27. # Reconstruct the initial field
  28. f_ishift = np.fft.ifftshift(fshift)
  29. re_field = np.abs(np.fft.ifft2(f_ishift))
  30.  
  31. # Plot
  32. fig = plt.figure()
  33.  
  34. fig.add_subplot(411)
  35. plt.imshow(field, cmap='gray')
  36. plt.title('Field'), plt.xticks([]), plt.yticks([])
  37. plt.colorbar()
  38.  
  39. fig.add_subplot(412)
  40. plt.imshow(magnitude_spectrum, cmap='gray')
  41. plt.title('Magnitude spectrum'), plt.xticks([]), plt.yticks([])
  42.  
  43. fig.add_subplot(413)
  44. plt.imshow(phase_spectrum, cmap='gray')
  45. plt.title('Phase spectrum'), plt.xticks([]), plt.yticks([])
  46.  
  47. fig.add_subplot(414)
  48. plt.imshow(re_field, cmap='gray')
  49. plt.title('Reconstructed field'), plt.xticks([]), plt.yticks([])
  50. plt.colorbar()
  51.  
  52. plt.show()
Add Comment
Please, Sign In to add comment