-
Notifications
You must be signed in to change notification settings - Fork 0
/
miniscope_utils.py
62 lines (52 loc) · 2.11 KB
/
miniscope_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
def make_lenslet_surface(Xlist, Ylist, Rlist, xrng, yrng, samples,aperR):
# Takes in Xlist, Ylist and Rlist: floating point center and radius values for each lenslet
# xrng and yrng: x and y range (tuple) over which to define grid
# samples: tuple of number of samplex in x and y, respectively
#
# Outputs: T, the aperture thickness function.
import numpy as np
assert np.shape(Xlist) == np.shape(Ylist), 'Xlist and Ylist must be same shape'
Nlenslets = np.shape(Xlist)[0]
if np.shape(Rlist) == ():
Rlist = np.tile(Rlist, np.shape(Xlist))
else:
assert np.shape(Rlist) == np.shape(Xlist), 'Xlist must be either a scalar or list matching shapes of Xlist and Ylist'
assert type(xrng) is tuple, 'xrng must be tuple'
assert type(yrng) is tuple, 'yrng must be tuple'
assert type(samples) is tuple, 'samples must be tuple'
T = np.zeros(samples)
xg = np.linspace(xrng[0], xrng[1], samples[1])
yg = np.linspace(yrng[0], yrng[1], samples[0])
px = xg[1] - xg[0]
py = yg[1] - yg[0]
xg, yg = np.meshgrid(xg,yg)
for n in range(Nlenslets):
sph = np.real(np.sqrt(0j+Rlist[n]**2 - (xg-Xlist[n])**2 - (yg-Ylist[n])**2))-Rlist[n]+10
T = np.maximum(T,sph)
aper = np.sqrt(xg**2+yg**2) <= aperR
return T-np.min(T[aper]), aper, px, py
def prop_field(Ui, z, lam, Fx, Fy):
import numpy as np
from numpy import fft
Hf = np.exp(1j*2*np.pi*z/lam*np.sqrt(1-(lam*Fx)**2 - (lam*Fy)**2))
R = np.sqrt(Fx**2 + Fy**2)
roi = R>1/lam
Hf[roi] = 0
Uf = fft.fftshift(fft.fft2(fft.ifftshift(Ui)))
#plt.imshow(np.imag(Uf))
Up = Uf*Hf
uf = fft.fftshift(fft.ifft2(fft.ifftshift(Up)))
return uf
def project_to_aperture(x_list, y_list, aperR, mode='snap'):
lr = np.sqrt(x_list**2+y_list**2)
lout = lr<aperR
if mode is 'delete':
x_out = x_list[lout]
y_out = y_list[lout]
elif mode is 'snap':
ya = np.arctan2(y_list, x_list)
lr = np.minimum(lr, aperR)
y_out = lr * np.sin(ya)
x_out = lr * np.cos(ya)
return x_out, y_out