forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfresnel_diffract.py
More file actions
207 lines (169 loc) · 7.13 KB
/
Copy pathfresnel_diffract.py
File metadata and controls
207 lines (169 loc) · 7.13 KB
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
"""
Title: Fresnel Diffraction for Coherent and Monochromatic
Wave Fields
Fresnel Diffraction describes the behavior of a wave field as it
moves through free space or interacts with an object under the
small-angle approximation. It is particularly useful for near
field diffraction.
The following algorithm is an adaptation of the 'transfer function'
based approach contained in the reference. It is critically
sampled when:
pixel_size = wavelength * prop_dist / side_length
Or equivalently:
pixel_size = sqrt(wavelength * prop_dist / pixel_num)
Under and oversampling occur when the left-hand side is less
than or greater than the right-hand side, respectively.
This code is adapted and modified from:
Computational Fourier Optics: A MATLAB Tutorial by David Voelz
"""
from math import pi
import numpy as np
from scipy.fft import fft, fft2, fftshift, ifft, ifft2, ifftshift
def fresnel_diffract(
wavefunc_0: np.ndarray, pixel_size: float, wavelength: float, prop_dist: float
) -> np.ndarray:
"""
Fresnel Diffraction of 1D or 2D Wave Fields.
This function calculates the Fresnel diffraction of a
given wave field, suitable for near-field diffraction. The
wave field is assumed to be coherent and monochromatic.
Args:
wavefunc0 (np.ndarray): The initial wave field at the unpropagated plane.
pixel_size (float): The physical size of a pixel (or data point) at the
pixel_size (float): The physical size of a pixel (or data point) at the
unpropagated plane.
wavelength (float): The wavelength of the wave field.
prop_dist (float): The desired propagation distance.
Raises:
ValueError: If the input wave field is not 1D or 2D.
Returns:
np.ndarray: The wave field at the propagated plane.
Examples:
>>> import numpy as np
>>> res = fresnel_diffract(np.ones(64), 1, 1, 1)
>>> res.shape
(64,)
>>> import numpy as np
>>> res = fresnel_diffract(np.ones((64, 64)), 1, 1, 1)
>>> res.shape
(64, 64)
>>> import numpy as np
>>> res = fresnel_diffract(np.ones((4, 4, 4)), 1, 1, 1)
Traceback (most recent call last):
...
ValueError: Expected a 1D or 2D wavefield, but got (4, 4, 4)
# Test that conservation of energy is obeyed
>>> import numpy as np
>>> wf0 = np.ones(64)
>>> wfz = fresnel_diffract(wf0, 1, 1, 1)
>>> bool(np.isclose(np.sum(abs(wf0)**2), np.sum(abs(wfz)**2)))
True
>>> import numpy as np
>>> wf0 = np.ones((64, 64))
>>> wfz = fresnel_diffract(wf0, 1, 1, 1)
>>> bool(np.isclose(np.sum(abs(wf0)**2), np.sum(abs(wfz)**2)))
True
# Test that propagation distance of 0 returns the contact image
>>> import numpy as np
>>> x = np.linspace(-32, 32, 1)
>>> wf0 = np.where(abs(x)<=8, 1, 0)
>>> wfz = fresnel_diffract(wf0, 1, 1, 0)
>>> np.allclose(wf0, wfz)
True
"""
if len(wavefunc_0.shape) == 1:
return _fresnel_diffract_1d(wavefunc_0, pixel_size, wavelength, prop_dist)
elif len(wavefunc_0.shape) == 2:
return _fresnel_diffract_2d(wavefunc_0, pixel_size, wavelength, prop_dist)
else:
error_message = f"Expected a 1D or 2D wavefield, but got {wavefunc_0.shape}"
raise ValueError(error_message)
def _fresnel_diffract_2d(
wavefunc_0: np.ndarray, pixel_size: float, wavelength: float, prop_dist: float
) -> np.ndarray:
"""
Fresnel Diffraction of 2D Wave Fields.
This private function is called by 'fresnel_diffract' to handle the
fresnel diffraction of 2D wave fields specifically.
Args:
wavefunc_0 (np.ndarray): The initial 2D wave field at the unpropagated plane.
pixel_size (float): The physical size of a pixel (or data point) at the
unpropagated plane.
wavelength (float): The wavelength of the wave field.
prop_dist (float): The desired propagation distance.
Returns:
np.ndarray: The 2D wave field at the propagated plane.
Examples:
>>> import numpy as np
>>> res = _fresnel_diffract_2d(np.ones((64, 64)), 1, 1, 1)
>>> res.shape
(64, 64)
>>> import numpy as np
>>> wf0 = np.ones((64, 64))
>>> wfz = _fresnel_diffract_2d(wf0, 1, 1, 1)
>>> bool(np.isclose(np.sum(abs(wf0)**2), np.sum(abs(wfz)**2)))
True
# Test that propagation distance of 0 returns the contact image
>>> import numpy as np
>>> x = np.linspace(-32, 32, 1)
>>> X1, X2 = np.meshgrid(x, x)
>>> wf0 = np.where(abs(X1)<=8, 1, 0) * np.where(abs(X2)<=8, 1, 0)
>>> wfz = _fresnel_diffract_2d(wf0, 1, 1, 0)
>>> np.allclose(wf0, wfz)
True
"""
pixel_num, _ = wavefunc_0.shape
side_length = pixel_num * pixel_size
# Coordinates in Fourier space are proportionate to 1 / pixel_size
f_x = np.arange(-1 / (2 * pixel_size), 1 / (2 * pixel_size), 1 / side_length)
f_x2d, f_y2d = np.meshgrid(f_x, f_x)
# Transfer function which models diffraction
transferf = np.exp(-1j * np.pi * wavelength * prop_dist * (f_x2d**2 + f_y2d**2))
transferf = fftshift(transferf)
# Fourier space wave function at the unpropagated plane
f_wavefunc_0 = fft2(fftshift(wavefunc_0))
# Wave function at the propagated, or 'z' plane
wavefuncz = ifftshift(ifft2(transferf * f_wavefunc_0))
return wavefuncz
def _fresnel_diffract_1d(
wavefunc_0: np.ndarray, pixel_size: float, wavelength: float, prop_dist: float
) -> np.ndarray:
"""
Fresnel Diffraction of 1D Wave Fields.
This private function is called by 'fresnel_diffract' to handle the
fresnel diffraction of 1D wave fields specifically.
Args:
wavefunc0 (np.ndarray): The initial 1D wave field at the unpropagated plane.
pixel_size (float): The physical size of a pixel (or data point) at the
unpropagated plane.
wavelength (float): The wavelength of the wave field.
prop_dist (float): The desired propagation distance.
Returns:
np.ndarray: The 1D wave field at the propagated plane.
Examples:
>>> import numpy as np
>>> res = _fresnel_diffract_1d(np.ones(64), 1, 1, 1)
>>> res.shape
(64,)
# Conservation of energy
>>> import numpy as np
>>> wf0 = np.ones(64)
>>> wfz = _fresnel_diffract_1d(wf0, 1, 1, 1)
>>> bool(np.isclose(np.sum(abs(wf0)**2), np.sum(abs(wfz)**2)))
True
# Test that propagation distance of 0 returns the contact image
>>> import numpy as np
>>> x = np.linspace(-32, 32, 1)
>>> wf0 = np.where(abs(x)<=8, 1, 0)
>>> wfz = _fresnel_diffract_1d(wf0, 1, 1, 0)
>>> np.allclose(wf0, wfz)
True
"""
pixel_num = len(wavefunc_0)
side_length = pixel_num * pixel_size
fx = np.arange(-1 / (2 * pixel_size), 1 / (2 * pixel_size), 1 / side_length)
transferf = np.exp(-1j * pi * wavelength * prop_dist * (fx**2))
transferf = fftshift(transferf)
f_wavefunc_0 = fft(fftshift(wavefunc_0))
wavefunc_z = ifftshift(ifft(transferf * f_wavefunc_0))
return wavefunc_z