forked from brandondube/prysm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polarization.py
533 lines (398 loc) · 15 KB
/
polarization.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
"Jones and Mueller Calculus"
from prysm.mathops import np
from prysm.conf import config
from prysm import propagation
from prysm.coordinates import make_xy_grid,cart_to_polar
import functools
# supported functions for jones_decorator
supported_propagation_funcs = ['focus','unfocus','focus_fixed_sampling','angular_spectrum']
U = np.array([[1, 0, 0, 1],
[1, 0, 0, -1],
[0, 1, 1, 0],
[0, 1j, -1j, 0]]) / np.sqrt(2)
def _empty_pol_vector(shape=None):
"""Returns an empty array to populate with jones vector elements.
Parameters
----------
shape : list, optional
shape to prepend to the jones vector array. shape = [32,32] returns an array of shape [32,32,2,1]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a [2] array.
Returns
-------
numpy.ndarray
The empty array of specified shape
"""
if shape is None:
shape = (2)
else:
shape = (*shape,2,1)
return np.zeros(shape, dtype=config.precision_complex)
def linear_pol_vector(angle, degrees=True):
"""Returns a linearly polarized jones vector at a specified angle
Parameters
----------
angle : float
angle that the linear polarization is oriented with respect to the horizontal axis
shape : list, optional
shape to prepend to the jones vector array. shape = [32,32] returns an array of shape [32,32,2,1]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a [2] array.
Returns
-------
numpy.ndarray
linear jones vector
"""
if degrees:
angle = angle * np.pi / 180
cost = np.cos(angle)
sint = np.sin(angle)
if hasattr(angle,'ndim'):
pol_vector = _empty_pol_vector(shape=angle.shape)
pol_vector[...,0,0] = cost
pol_vector[...,1,0] = sint
else:
pol_vector = _empty_pol_vector(shape=None)
pol_vector[0] = cost
pol_vector[1] = sint
return pol_vector
def circular_pol_vector(handedness='left',shape=None):
"""Returns a circularly polarized jones vector
Parameters
----------
shape : list, optional
shape to prepend to the jones vector array. shape = [32,32] returns an array of shape [32,32,2,1]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a [2] array.
Returns
-------
numpy.ndarray
circular jones vector
"""
pol_vector = _empty_pol_vector(shape=shape)
pol_vector[...,0] = 1/np.sqrt(2)
if handedness == 'left':
pol_vector[...,1] = 1j/np.sqrt(2)
elif handedness == 'right':
pol_vector[...,1] = -1j/np.sqrt(2)
else:
raise ValueError(f"unknown handedness {handedness}, use 'left' or 'right''")
return pol_vector
def _empty_jones(shape=None):
"""Returns an empty array to populate with jones matrix elements.
Parameters
----------
shape : list
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
numpy.ndarray
The empty array of specified shape
"""
if shape is None:
shape = (2, 2)
else:
shape = (*shape, 2, 2)
return np.zeros(shape, dtype=config.precision_complex)
def jones_rotation_matrix(theta, shape=None):
"""A rotation matrix for rotating the coordinate system transverse to propagation.
source: https://en.wikipedia.org/wiki/Rotation_matrix.
Parameters
----------
theta : float
angle in radians to rotate the jones matrix with respect to the x-axis.
shape : list
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
numpy.ndarray
2D rotation matrix
"""
jones = _empty_jones(shape=shape)
cost = np.cos(theta)
sint = np.sin(theta)
jones[..., 0, 0] = cost
jones[..., 0, 1] = sint
jones[..., 1, 0] = -sint
jones[..., 1, 1] = cost
return jones
def linear_retarder(retardance, theta=0, shape=None):
"""Generates a homogenous linear retarder jones matrix.
Parameters
----------
retardance : float
phase delay experienced by the slow state in radians.
theta : float
angle in radians the linear retarder is rotated with respect to the x-axis.
Defaults to 0.
shape : list
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
retarder : numpy.ndarray
numpy array containing the retarder matrices
"""
retphasor = np.exp(1j*retardance)
jones = _empty_jones(shape=shape)
jones[..., 0, 0] = 1
jones[..., 1, 1] = retphasor
retarder = jones_rotation_matrix(-theta) @ jones @ jones_rotation_matrix(theta)
return retarder
def linear_diattenuator(alpha, theta=0, shape=None):
"""Generates a homogenous linear diattenuator jones matrix.
Parameters
----------
alpha : float
Fraction of the light that passes through the partially transmitted channel.
If 1, this is an unpolarizing plate. If 0, this is a perfect polarizer
theta : float
angle in radians the linear retarder is rotated with respect to the x-axis.
Defaults to 0.
shape : list
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
diattenuator : numpy.ndarray
numpy array containing the diattenuator matrices
"""
assert (alpha >= 0) and (alpha <= 1), f"alpha cannot be less than 0 or greater than 1, got: {alpha}"
jones = _empty_jones(shape=shape)
jones[..., 0, 0] = 1
jones[..., 1, 1] = alpha
diattenuator = jones_rotation_matrix(-theta) @ jones @ jones_rotation_matrix(theta)
return diattenuator
def half_wave_plate(theta=0, shape=None):
"""Make a half wave plate jones matrix. Just a wrapper for linear_retarder.
Parameters
----------
theta : float
angle in radians the linear retarder is rotated with respect to the x-axis.
Defaults to 0.
shape : list
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
linear_retarder
a linear retarder with half-wave retardance
"""
return linear_retarder(np.pi, theta=theta, shape=shape)
def quarter_wave_plate(theta=0, shape=None):
"""Make a quarter wave plate jones matrix. Just a wrapper for linear_retarder.
Parameters
----------
theta : float
angle in radians the linear retarder is rotated with respect to the x-axis.
Defaults to 0.
shape : list, optional
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
linear_retarder
a linear retarder with quarter-wave retardance
"""
return linear_retarder(np.pi / 2, theta=theta, shape=shape)
def linear_polarizer(theta=0, shape=None):
"""Make a linear polarizer jones matrix. Just a wrapper for linear_diattenuator.
Returns
-------
theta : float
angle in radians the linear retarder is rotated with respect to the x-axis.
Defaults to 0.
shape : list
shape to prepend to the jones matrix array. shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices. Defaults to None, which returns a 2x2 array.
Returns
-------
linear_diattenuator
a linear diattenuator with unit diattenuation
"""
return linear_diattenuator(0, theta=theta, shape=shape)
def vector_vortex_retarder(charge, shape, retardance=np.pi, theta=0):
"""generate a phase-only spatially-varying vector vortex retarder (VVR)
This model follows Eq (7) in D. Mawet. et al. (2009)
https://opg.optica.org/oe/fulltext.cfm?uri=oe-17-3-1902&id=176231 (open access)
Parameters
----------
charge : float
topological charge of the vortex, typically an interger
shape : tuple of int
shape of the VR array
retardance : float
phase difference between the ordinary and extraordinary modes, by default np.pi or half a wave
theta : float, optional
angle in radians to rotate the vortex by, by default 0
Returns
-------
_type_
_description_
"""
vvr_lhs = _empty_jones(shape=[shape,shape])
vvr_rhs = _empty_jones(shape=[shape,shape])
# create the dimensions
x,y = make_xy_grid(shape,diameter=1)
r,t = cart_to_polar(x,y)
t *= charge
# precompute retardance
cost = np.cos(t)
sint = np.sin(t)
jcosr = -1j*np.cos(retardance/2)
jsinr = np.sin(retardance/2)
# build jones matrices
vvr_lhs[...,0,0] = cost
vvr_lhs[...,0,1] = sint
vvr_lhs[...,1,0] = sint
vvr_lhs[...,1,1] = -cost
vvr_lhs *= jsinr
vvr_rhs[...,0,0] = jcosr
vvr_rhs[...,0,0] = jcosr
vvr = vvr_lhs + vvr_rhs
vvr = jones_rotation_matrix(-theta) @ vvr @ jones_rotation_matrix(theta)
return vvr
def broadcast_kron(a,b):
"""broadcasted kronecker product of two N,M,...,2,2 arrays. Used for jones -> mueller conversion
In the unbroadcasted case, this output looks like
out = [a[0,0]*b,a[0,1]*b]
[a[1,0]*b,a[1,1]*b]
where out is a N,M,...,4,4 array. I wrote this to work for generally shaped kronecker products where the matrix
is contained in the last two axes, but it's only tested for the Nx2x2 case
Parameters
----------
a : numpy.ndarray
N,M,...,2,2 array used to scale b in kronecker product
b : numpy.ndarray
N,M,...,2,2 array used to form block matrices in kronecker product
Returns
-------
out
N,M,...,4,4 array
"""
return np.einsum('...ik,...jl',a,b).reshape([*a.shape[:-2],int(a.shape[-2]*b.shape[-2]),int(a.shape[-1]*b.shape[-1])])
def jones_to_mueller(jones, broadcast=True):
"""Construct a Mueller Matrix given a Jones Matrix. From Chipman, Lam, and Young Eq (6.99).
Parameters
----------
jones : ndarray with final dimensions 2x2
The complex-valued jones matrices to convert into mueller matrices
broadcast : bool
Whether to use the experimental `broadcast_kron` to compute the conversion in a broadcast fashion, by default True
Returns
-------
M : np.ndarray
Mueller matrix
"""
if broadcast:
jprod = broadcast_kron(np.conj(jones), jones)
else:
jprod = np.kron(np.conj(jones), jones)
M = np.real(U @ jprod @ np.linalg.inv(U))
return M
def pauli_spin_matrix(index, shape=None):
"""Generates a pauli spin matrix used for Jones matrix data reduction. From CLY Eq 6.108.
Parameters
----------
index : int
0 - the identity matrix
1 - a linear half-wave retarder oriented horizontally
2 - a linear half-wave retarder oriented 45 degrees
3 - a circular half-wave retarder
shape : list, optional
shape to prepend to the jones matrix array.
shape = [32,32] returns an array of shape [32,32,2,2]
where the matrix is assumed to be in the last indices.
Default returns a 2x2 array
Returns
-------
jones
pauli spin matrix of index specified
"""
jones = _empty_jones(shape=shape)
assert index in (0, 1, 2, 3), f"index should be 0,1,2, or 3. Got {index}"
if index == 0:
jones[..., 0, 0] = 1
jones[..., 1, 1] = 1
elif index == 1:
jones[..., 0, 0] = 1
jones[..., 1, 1] = -1
elif index == 2:
jones[..., 0, 1] = 1
jones[..., 1, 0] = 1
elif index == 3:
jones[..., 0, 1] = -1j
jones[..., 1, 0] = 1j
return jones
def pauli_coefficients(jones):
"""Compute the pauli coefficients of a jones matrix.
Parameters
----------
jones : numpy.ndarray
complex jones matrix to decompose
Returns
-------
c0,c1,c2,c3
complex coefficients of pauli matrices
"""
c0 = (jones[..., 0, 0] + jones[..., 1, 1]) / 2
c1 = (jones[..., 0, 0] - jones[..., 1, 1]) / 2
c2 = (jones[..., 0, 1] + jones[..., 1, 0]) / 2
c3 = 1j*(jones[..., 0, 1] - jones[..., 1, 0]) / 2
return c0, c1, c2, c3
def jones_adapter(prop_func):
"""wrapper around prysm.propagation functions to support polarized field propagation
There isn't anything particularly special about polarized field propagation. We simply
leverage the independence of the 4 "polarized" components of an optical system expressed
as a Jones matrix
J = [
[J00,J01],
[J10,J11]
]
The elements of this matrix can be propagated as incoherent wavefronts to express the polarized
response of an optical system. All `jones_adapter` does is call a given propagation function
4 times, one for each element of the Jones matrix.
Parameters
----------
prop_func : callable
propagation function to decorate
Returns
-------
callable
decorated propagation function
"""
@functools.wraps(prop_func)
def wrapper(*args,**kwargs):
# this is a function
wavefunction = args[0]
if len(args) > 1:
other_args = args[1:]
else:
other_args = ()
if wavefunction.ndim == 2:
# pass through non-jones case
return prop_func(*args,**kwargs)
J00 = wavefunction[...,0,0]
J01 = wavefunction[...,0,1]
J10 = wavefunction[...,1,0]
J11 = wavefunction[...,1,1]
tmp = []
for E in [J00, J01, J10, J11]:
ret = prop_func(E, *other_args, **kwargs)
tmp.append(ret)
out = np.empty([*ret.shape,2,2],dtype=ret.dtype)
out[...,0,0] = tmp[0]
out[...,0,1] = tmp[1]
out[...,1,0] = tmp[2]
out[...,1,1] = tmp[3]
return out
return wrapper
def make_propagation_polarized(funcs_to_change=supported_propagation_funcs):
"""apply decorator to supported propagation functions
Parameters
----------
funcs_to_change : list, optional
list of propagation functions to add polarized field propagation to, by default supported_propagation_funcs
"""
for name,func in vars(propagation).items():
if name in funcs_to_change:
setattr(propagation, name, jones_adapter(func))