-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffraction.py
322 lines (226 loc) · 8.19 KB
/
diffraction.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
#!/usr/bin/python3
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
N = 1000 # number of points
global zoom
zoom = 1/4 #zoom in the diffraction graphs
# DEFAULT VALUES
global SIDE
SIDE = 1 # target side, in cm
global RADIUS
RADIUS = 0.5 # circle radius, in mm
global EDGE
EDGE = 1 # square edge, in mm
global LDA
LDA = 600e-9 # wavelength, in m
global Z
Z = 100 # obstacle-sensor distance, in cm
global FNAME
FNAME = "img/atom.png"
#############################################
## GENERACIÓN DE SUPERFICIES DE DIFRACCIÓN ##
def circle(radius):
"""Generates an NxN array of 0s with 1s within the
given radius from the center (in cm)"""
bitmap = np.zeros((N,N))
for i in range(N):
for j in range(N):
deltax, deltay = index_to_distance(i, j) # in cm
if(np.sqrt(deltax**2 + deltay**2) < radius):
bitmap[i,j] = 1
return bitmap
def rectangle(side):
"""Generates an NxN array of 0s with 1s within a centered
square of the indicated side (in cm)"""
bitmap = np.zeros((N,N))
for i in range(N):
for j in range(N):
x, y = index_to_distance(i,j)
if(np.abs(x) < side/2 and np.abs(y) < side/2):
bitmap[i,j] = 1
return bitmap
def index_to_distance(i, j):
"Returns mapped distance on the array, in cm"
ratio = float(SIDE)/float(N)
x = (i - N/2)*ratio
y = (N/2 - j)*ratio
return [x, y]
#############################################
### DIFRACTION FUNCTIONS ####################
def fraunhoffer(u1):
"""Calculates the intensity in the sensor at z using
Fraunhoffer's far-field approximation"""
u1 = u1.astype(complex)
fu1 = np.fft.fft2(u1)/(N*N)
fu1 = np.fft.fftshift(fu1)
P = np.abs(fu1)
I = (1/(LDA*(Z/100))**2)*P # z to m
return I
def fresnel(u1):
"""Calculates the intensity in the sensor at k using
Fresnel's approximation"""
k = 2*np.pi/LDA
u = u1.astype(complex)
for i in range(N):
for j in range(N):
x, y = index_to_distance(i,j) # in cm
u[i,j] = u[i,j]*np.exp((1j*k)/(2*Z/100)*((x/100)**2 + (y/100)**2))
fu1 = np.fft.fft2(u)/(N*N)
fu1 = np.fft.fftshift(fu1)
P = np.abs(fu1)
I = (1/(LDA*(Z/100))**2)*P # z to m
return I
#############################################
############### MENU STUFF ##################
def menu():
print("")
print("### DIFFRACTION SIMULATOR ### ")
print("")
print("Raúl Coterillo")
print("")
print("This program calculates the diffraction pattern")
print("of several shapes using either Fresnel's or")
print("Fraunhoffer's approximation. For more details,")
print("refer to the source code.")
print("")
print("First, select the shape of the obstacle: (default: Circle)")
print("[0] - Circle")
print("[1] - Square")
print("[2] - Image")
choice = input("-> ")
print("")
#defaulting to circle
if(choice == ""):
choice = "0"
if choice == "0":
print("Please, now input the following information:\n")
global SIDE
rply = input_def("Obstacle/sensor area side: (default: "+str(SIDE)+"cm)", SIDE)
SIDE = float(rply)
global RADIUS
rply = input_def("Circle radius in mm: (default: "+str(RADIUS)+"mm)", RADIUS)
RADIUS = float(rply)
global Z
rply = input_def("Obstacle-sensor separaton in cm: (default: "+str(Z)+"cm)", Z)
Z = float(rply)
global LDA
rply = input_def("Light wavelength in m: (default: "+str(LDA)+"m)", LDA)
LDA = float(rply)
print("Finally, choose the diffraction method: (default: Fresnel's)")
print("[0] - Fresnel")
print("[1] - Fraunhoffer")
method = input("-> ")
print("")
if(method == "1"):
print("Using Fraunhoffer's approximation...")
obs = circle(RADIUS/10) # converted to cm
diff = fraunhoffer(obs)
else:
print("Using Fresnel's approximation...")
obs = circle(RADIUS/10) # converted to cm
diff = fresnel(obs)
print("DONE!")
plot_stuff(obs, diff)
quit()
if choice == "1":
print("Please, now input the following information:\n")
rply = input_def("Obstacle/sensor area side: (default: "+str(SIDE)+"cm)", SIDE)
SIDE = int(rply)
global EDGE
rply = input_def("Square side in mm: (default: "+str(EDGE)+"mm)", EDGE)
EDGE = int(rply)
rply = input_def("Obstacle-sensor separaton in cm: (default: "+str(Z)+"cm)", Z)
Z = float(rply)
rply = input_def("Light wavelength in m: (default: "+str(LDA)+"m)", LDA)
LDA = float(rply)
print("Finally, choose the diffraction method: (default: Fresnel's)")
print("[0] - Fresnel")
print("[1] - Fraunhoffer")
method = input("-> ")
print("")
if(method == "1"):
print("Using Fraunhoffer's approximation...")
obs = rectangle(EDGE/10) # converted to cm
diff = fraunhoffer(obs)
else:
print("Using Fresnel's approximation...")
obs = rectangle(EDGE/10) # converted to cm
diff = fresnel(obs)
print("DONE!")
plot_stuff(obs, diff)
quit()
if choice == "2":
print("Please, now input the following information:\n")
global FNAME
rply = input_def("Image file name: (default: "+ FNAME +", image must be "
+ str(N) + "x" + str(N) + " pixels)", FNAME)
FNAME = rply
try:
obs = np.array(Image.open(rply).convert("L"))
except FileNotFoundError:
print("\n File does not exist, quitting.")
quit()
except OSError:
print("\n File type not recognized, quitting.")
quit()
rply = input_def("Obstacle/sensor area side: (default: "+str(SIDE)+"cm)", SIDE)
SIDE = int(rply)
rply = input_def("Obstacle-sensor separaton in cm: (default: "+str(Z)+"cm)", Z)
Z = float(rply)
rply = input_def("Light wavelength in m: (default: "+str(LDA)+"m)", LDA)
LDA = float(rply)
print("Finally, choose the diffraction method: (default: Fresnel's)")
print("[0] - Fresnel")
print("[1] - Fraunhoffer")
method = input("-> ")
print("")
if(method == "1"):
print("Using Fraunhoffer's approximation...")
diff = fraunhoffer(obs)
else:
print("Using Fresnel's approximation...")
diff = fresnel(obs)
print("DONE!")
plot_stuff(obs, diff)
quit()
else:
print("")
print("Please, select a valid pattern using the numbers provided.")
print("")
quit()
def input_def(ph, def_val):
print(ph)
rply = input("-> ")
print("")
if(rply != ""):
return rply
else:
return def_val
def plot_stuff(obstacle, difracted):
plt.figure(figsize=(900, 400))
factor = 1/(LDA*1000*Z*10)
plt.suptitle(r"$z$ = " + str(int(Z)) + "cm\n\n" + r"$\lambda$ = " + str(LDA) + "m", fontsize = 20)
plt.subplot(1,3,1)
plt.title("Obstacle", fontsize=16)
plt.imshow(obstacle, cmap="gray", extent=[-SIDE*5,SIDE*5,-SIDE*5, SIDE*5])
plt.xlabel("y/mm", fontsize=14)
plt.ylabel("x/mm", fontsize=14)
plt.subplot(1,3,2)
plt.title("Diffraction Pattern", fontsize=16)
plt.imshow(difracted[int(N/2-N*zoom/2):int(N/2+N*zoom/2)-1,int(N/2-N*zoom/2):int(N/2+N*zoom/2)-1],
cmap="gray", extent=[factor*(-SIDE*5*zoom),factor*(SIDE*5*zoom),factor*(-SIDE*5*zoom),factor*(SIDE*5*zoom)])
plt.xlabel("y/mm", fontsize=14)
plt.ylabel("x/mm", fontsize=14)
plt.subplot(1,3,3)
plt.title("Diffraction Pattern (log)", fontsize=16)
plt.imshow(np.log(difracted[int(N/2-N*zoom/2):int(N/2+N*zoom/2)-1,int(N/2-N*zoom/2):int(N/2+N*zoom/2)-1]),
cmap="gray", extent=[factor*(-SIDE*5*zoom),factor*(SIDE*5*zoom),factor*(-SIDE*5*zoom),factor*(SIDE*5*zoom)])
plt.xlabel("y/mm", fontsize=14)
plt.ylabel("x/mm", fontsize=14)
plt.show()
## MAIN METHOD
if __name__ == "__main__":
""" Launch this on execute."""
menu()
print("")