-
Notifications
You must be signed in to change notification settings - Fork 1
/
polarization.py
187 lines (139 loc) · 5.3 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
import numpy as np
import matplotlib.pyplot as plt
def hyperjones_to_hyperstokes(E, axis=0):
nmodes = E.shape[axis] // 2
new_shape = list(E.shape)
new_shape[axis] = 3 * nmodes
new_shape = tuple(new_shape)
sop = np.zeros(new_shape, dtype=np.float64)
for m in range(nmodes):
idx_x = m * 2
idx_y = m * 2 + 1
idx_s1 = m * 3
idx_s2 = idx_s1 + 1
idx_s3 = idx_s1 + 2
sl_1 =[slice(None)] * sop.ndim
sl_2 =[slice(None)] * sop.ndim
sl_3 =[slice(None)] * sop.ndim
sl_1[axis] = idx_s1
sl_2[axis] = idx_s2
sl_3[axis] = idx_s3
J_x = E.take(indices=idx_x, axis=axis)
J_y = E.take(indices=idx_y, axis=axis)
I = np.abs(J_x ** 2) + np.abs(J_y) ** 2
sop[tuple(sl_1)] = (np.abs(J_x) ** 2 - np.abs(J_y) ** 2) / I
sop[tuple(sl_2)] = (2 * np.real(J_x * np.conj(J_y))) / I
sop[tuple(sl_3)] = (-2 * np.imag(J_x * np.conj(J_y))) / I
return sop
def stokes_to_jones(sop):
sop_ = np.atleast_2d(sop)
# sops concatenated along second dimension
ndim = sop_.shape[0]
output = np.zeros((ndim, 2), dtype=np.complex)
Q=sop_[:, 0]
U=sop_[:, 1]
V=sop_[:, 2]
for x in range(ndim):
A=np.sqrt((1+Q[x])/2)
if A == 0:
B=1
else:
B = U[x]/(2*A)+1j*V[x]/(2*A)
output[x, 0] = A
output[x, 1] = B
return output.squeeze()
def linear_stokes(direction="x", angle=None, num=1):
if angle:
x = compute_stokes(np.array([np.cos(angle), np.sin(angle)]))
else:
if direction == "x":
x = np.array([1.0, 0.0, 0.0])
elif direction == "y":
x = np.array([-1.0, 0.0, 0.0])
else:
raise ValueError("Invalid direction")
return np.tile(x, (num,1)).squeeze()
def linear_hyperstokes(modes, direction="x", angle=None, num=1):
x = linear_stokes(direction=direction, angle=angle, num=num)
return np.tile(x, (1, modes)).squeeze()
def compute_stokes(E):
E_ = np.atleast_2d(E)
ndim = E.shape[0]
S = np.zeros((ndim, 3))
I = np.abs(E_[:, 0]) ** 2 + np.abs(E_[:, 1]) ** 2
S[:, 0] = (np.abs(E_[:, 0] ** 2) - np.abs(E_[:, 1]) ** 2) / I
S[:, 1] = (2 * np.real(E_[:, 0] * np.conj(E_[:, 1]))) / I
S[:, 2] = (-2 * np.imag(E_[:, 0] * np.conj(E_[:, 1]))) / I
return S.squeeze()
def plot_sphere(pts=30):
ax = plt.gca()
ax = plt.axes(projection='3d')
u = np.linspace(0, 2*np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 1 * np.outer(np.cos(u), np.sin(v))
y = 1 * np.outer(np.sin(u), np.sin(v))
z = 1 * np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b', edgecolor="k", linewidth=.0, alpha=0.1)
ax.plot(np.sin(u),np.cos(u),0,color='k', linestyle = 'dashed', linewidth=0.5)
ax.plot(np.sin(u),np.zeros_like(u),np.cos(u),color='k', linestyle = 'dashed', linewidth=0.5)
ax.plot(np.zeros_like(u),np.sin(u),np.cos(u),color='k', linestyle = 'dashed', linewidth=0.5)
ax.plot([0,1],[0,0],[0,0],'k--',lw=1.5, alpha=0.5)
ax.plot([0,0],[0,1],[0,0],'k--',lw=1.5, alpha=0.5)
ax.plot([0,0],[0,0],[0,1],'k--',lw=1.5, alpha=0.5)
ax.set_xlabel(r"$S_1$")
ax.set_ylabel(r"$S_2$")
ax.set_zlabel(r"$S_3$")
ax.xaxis.labelpad = 15
ax.yaxis.labelpad = 15
ax.zaxis.labelpad = 15
def random_sop(num=1, astuple=False):
azimuth = np.random.uniform(low=0, high=2*np.pi, size=(num,))
inclination = np.arccos(1 - 2*np.random.uniform(low=0, high=1, size=(num,)))
radius = 1.
x = radius * np.sin(inclination) * np.sin(azimuth)
y = radius * np.sin(inclination) * np.cos(azimuth)
z = radius * np.cos(inclination)
if astuple:
return x, y, z
else:
sop = np.zeros((num, 3))
sop[:, 0] = x
sop[:, 1] = y
sop[:, 2] = z
return sop.squeeze()
def random_hypersop(nmodes, num=1):
"""Generate a vector of states of polarization of the number of specified modes."""
sops = [random_sop(num=num).squeeze() for _ in range(nmodes)]
return np.hstack(sops).squeeze()
def random_hyperjones(nmodes, num=1):
"""Generate a generalized Jones vector with random polarization for each mode."""
hsop = random_hypersop(nmodes, num=num)
hjones = hyperstokes_to_jones(hsop)
return hjones.squeeze()
def hyperstokes_to_jones(hsop):
"""Convert an hyper-state of polarization to the equivalent Jones vector."""
sop = np.atleast_2d(hsop)
nmodes = sop.shape[1] // 3
num = sop.shape[0]
jones = np.zeros((num, 2 * nmodes), dtype=np.complex128)
for i in range(num):
s = sop[i]
s = np.reshape(s, (nmodes, 3))
j = [stokes_to_jones(x) for x in s]
jones[i] = np.concatenate(j)
return jones.squeeze()
def plot_stokes(sop, **kwargs):
ax = plt.gca()
ax.scatter(sop.T[0], sop.T[1], sop.T[2], **kwargs)
def plot_stokes_trajectory(sop, plot_sphere=False, jones=False, plot_kw={}, scatter_kw={}):
ax = plt.gca()
if plot_sphere:
plot_sphere()
lines = ax.plot3D(sop[0], sop[1], sop[2], linewidth=1.5, **plot_kw)
color = lines[-1].get_color()
ax.set_xlabel(r"$S_1$")
ax.set_ylabel(r"$S_2$")
ax.set_zlabel(r"$S_3$")
ax.xaxis.labelpad = 15
ax.yaxis.labelpad = 15
ax.zaxis.labelpad = 15