forked from davidgrier/pyfab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGH.py
188 lines (158 loc) · 5.41 KB
/
CGH.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
"""CGH.py: compute phase-only holograms for optical traps."""
import numpy as np
from PyQt4 import QtGui, QtCore
from numba import jit
import json
from time import time
class CGH(object):
"""Base class for computing computer-generated holograms.
For each trap, the coordinate r obtained from the fabscreen
is measured relative to the calibrated location rc of the
zeroth-order focal point, which itself is measured relative to
the center of the focal plane. The resulting displacement is
projected onto the coordinate system in the SLM place.
Projection involves a calibrated rotation about z with
a rotation matrix m.
The hologram is computed using calibrated wavenumbers for
the Cartesian coordinates in the SLM plane. These differ from
each other because the SLM is likely to be tilted relative to the
optical axis.
"""
def __init__(self, slm=None):
# Trap properties for current pattern
self.trapdata = []
# SLM geometry
self.slm = slm
self.w = self.slm.width()
self.h = self.slm.height()
# Conversion from SLM pixels to wavenumbers
self._qpp = 2. * np.pi / self.w / 10.
# Effective aspect ratio of SLM pixels
self._alpha = 1.
# Location of optical axis in SLM coordinates
self._rs = QtCore.QPointF(self.w / 2., self.h / 2.)
self.updateGeometry()
# Coordinate transformation matrix for trap locations
self.m = QtGui.QMatrix4x4()
# Location of optical axis in camera coordinates
self._rc = QtGui.QVector3D(320., 240., 0.)
# Orientation of camera relative to SLM
self._theta = 0.
self.updateTransformationMatrix()
@jit(parallel=True)
def quantize(self):
phi = ((128./np.pi) * np.angle(self._psi) + 127.).astype(np.uint8)
return phi.T
@jit(parallel=True)
def compute_one(self, amp, r):
"""Compute phase hologram for one trap with
specified complex amplitude and position
"""
ex = np.exp(self.iqx * r.x() + self.iqxsq * r.z())
ey = np.exp(self.iqy * r.y() + self.iqysq * r.z())
return np.outer(amp * ex, ey, self._buffer)
@jit(parallel=True)
def compute(self):
"""Compute phase hologram for specified traps
"""
start = time()
self._psi.fill(0. + 0j)
for properties in self.trapdata:
r = self.m * properties['r']
amp = properties['amp']
self._psi += self.compute_one(amp, r)
self.slm.data = self.quantize()
self.time = time() - start
def updateGeometry(self):
"""Compute position-dependent properties in SLM plane
and allocate buffers.
"""
shape = (self.w, self.h)
self._buffer = np.zeros(shape, dtype=np.complex_)
self._psi = np.zeros(shape, dtype=np.complex_)
qx = np.arange(self.w) - self.rs.x()
qy = np.arange(self.h) - self.rs.y()
qx = self.qpp * qx
qy = self.alpha * self.qpp * qy
self.iqx = 1j * qx
self.iqy = 1j * qy
self.iqxsq = 1j * qx * qx
self.iqysq = 1j * qy * qy
@property
def rs(self):
return self._rs
@rs.setter
def rs(self, rs):
if isinstance(rs, QtCore.QPointF):
self._rs = rs
else:
self._rs = QtCore.QPointF(rs[0], rs[1])
self.updateGeometry()
self.compute()
@property
def qpp(self):
return self._qpp
@qpp.setter
def qpp(self, qpp):
self._qpp = float(qpp)
self.updateGeometry()
self.compute()
@property
def alpha(self):
return self._alpha
@alpha.setter
def alpha(self, alpha):
self._alpha = float(alpha)
self.updateGeometry()
self.compute()
def updateTransformationMatrix(self):
self.m.setToIdentity()
self.m.translate(-self.rc)
self.m.rotate(self.theta, 0., 0., 1.)
@property
def rc(self):
return self._rc
@rc.setter
def rc(self, rc):
if isinstance(rc, QtGui.QVector3D):
self._rc = rc
else:
self._rc = QtGui.QVector3D(rc[0], rc[1], rc[2])
self.updateTransformationMatrix()
self.compute()
@property
def theta(self):
return self._theta
@theta.setter
def theta(self, theta):
self._theta = float(theta)
self.updateTransformationMatrix()
self.compute()
def setData(self, trapdata):
self.trapdata = trapdata
self.compute()
@property
def calibration(self):
return {'qpp': self.qpp,
'alpha': self.alpha,
'rs': (self.rs.x(), self.rs.y()),
'rc': (self.rc.x(), self.rc.y(), self.rc.z()),
'theta': self.theta}
@calibration.setter
def calibration(self, values):
if not isinstance(values, dict):
return
for attribute, value in values.iteritems():
try:
setattr(self, attribute, value)
except AttributeError:
print('unknown attribute:', attribute)
def serialize(self):
return json.dumps(self.calibration,
indent=2,
separators=(',', ': '),
ensure_ascii=False)
def deserialize(self, s):
values = json.loads(s)
self.calibration = values