Skip to content

Instantly share code, notes, and snippets.

@ptomato
Created May 25, 2012 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptomato/2789626 to your computer and use it in GitHub Desktop.
Save ptomato/2789626 to your computer and use it in GitHub Desktop.
Zernike aberrations
import numpy as N
import numpy.fft
from matplotlib import pyplot as P
npoints = 51 # odd number
side = N.linspace(-1, 1, npoints)
x, y = N.meshgrid(side, side)
r, theta = N.hypot(x, y), N.arctan2(y, x)
# Create a gaussian
w0 = 0.5
gaussian = N.array(N.exp(-r ** 2 / w0 ** 2), dtype=complex)
# Apply an aberration
a = 1.0 # deformation in wavelengths
# Formulae for aberrations taken from the Wikipedia page 'Optical aberration'
#z = r * N.cos(theta) # X-tilt
#z = r * N.sin(theta) # Y-tilt
#z = 2 * r ** 2 - 1 # Defocus
#z = r ** 2 * N.cos(2 * theta) # 0-degree astigmatism
#z = r ** 2 * N.sin(2 * theta) # 45-degree astigmatism
z = (3 * r ** 2 - 2) * r * N.cos(theta) # X-coma
#z = (3 * r ** 2 - 2) * r * N.sin(theta) # Y-coma
#z = 6 * r ** 4 - 6 * r ** 2 + 1 # 3rd-order spherical aberration
gaussian *= N.exp(2j * N.pi * a * z) # Apply the aberration to the phase front
# Fourier-transform the gaussian
padding = 200
padded = N.zeros((2 * padding + npoints, 2 * padding + npoints), dtype=complex)
padded[padding:-padding, padding:-padding] = gaussian
farfield = N.fft.fftshift(N.fft.fft2(N.fft.ifftshift(padded)))
farfield = farfield[padding:-padding, padding:-padding]
P.subplot(1, 2, 1)
P.imshow(N.abs(gaussian) ** 2)
P.subplot(1, 2, 2)
P.imshow(N.abs(farfield) ** 2)
P.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment