fourierslm 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fourierslm-0.1.0/.gitignore +9 -0
- fourierslm-0.1.0/LICENSE +21 -0
- fourierslm-0.1.0/PKG-INFO +72 -0
- fourierslm-0.1.0/README.md +49 -0
- fourierslm-0.1.0/pyproject.toml +63 -0
- fourierslm-0.1.0/src/fourierslm/__init__.py +27 -0
- fourierslm-0.1.0/src/fourierslm/beam.py +82 -0
- fourierslm-0.1.0/src/fourierslm/phase.py +205 -0
- fourierslm-0.1.0/src/fourierslm/propagate.py +218 -0
- fourierslm-0.1.0/tests/test_beam.py +29 -0
- fourierslm-0.1.0/tests/test_phase.py +67 -0
- fourierslm-0.1.0/tests/test_propagate.py +117 -0
fourierslm-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Melvin Redon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: fourierslm
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fourier optics of a phase-only SLM: input beams, phase patterns and propagation to the focus of a lens
|
|
5
|
+
Project-URL: Documentation, https://melvinrdn.github.io/fourierslm
|
|
6
|
+
Project-URL: Repository, https://github.com/melvinrdn/fourierslm
|
|
7
|
+
Author: Melvin Redon
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: beam propagation,collins integral,fourier optics,slm,spatial light modulator
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Requires-Dist: numpy>=1.24
|
|
21
|
+
Requires-Dist: scipy>=1.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# fourierslm
|
|
25
|
+
|
|
26
|
+
[](https://github.com/melvinrdn/fourierslm/actions/workflows/ci.yml)
|
|
27
|
+
[](https://melvinrdn.github.io/fourierslm)
|
|
28
|
+
|
|
29
|
+
Fourier optics of a phase-only SLM: the beam on the SLM, its phase pattern, and the field it makes in the focal region
|
|
30
|
+
of a lens, in physical units.
|
|
31
|
+
|
|
32
|
+
**Documentation:** https://melvinrdn.github.io/fourierslm
|
|
33
|
+
|
|
34
|
+
## Install
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install fourierslm # or: uv add fourierslm
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Example
|
|
41
|
+
|
|
42
|
+
A Gaussian beam of 1 W on a 1920 × 1200 SLM with 8 µm pixels, a vortex of charge 1, and the field at the focus of a
|
|
43
|
+
50 cm lens placed 30 cm after the SLM.
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import numpy as np
|
|
47
|
+
import fourierslm as fs
|
|
48
|
+
from fourierslm import beam, phase, propagate
|
|
49
|
+
|
|
50
|
+
X, Y = fs.grid(1920, 8e-6), fs.grid(1200, 8e-6)
|
|
51
|
+
E = beam.gaussian(X, Y, w=2e-3, power=1.0) * np.exp(1j * phase.vortex(X, Y, charge=1))
|
|
52
|
+
x = y = np.linspace(-300e-6, 300e-6, 256) # window at the focus [m]
|
|
53
|
+
E_focus = propagate.focus(E, X, Y, x, y, wavelength=1030e-9, f=0.5, slm_distance=0.3)
|
|
54
|
+
I = beam.intensity(E_focus) # [W/m²]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Speed
|
|
58
|
+
|
|
59
|
+
On the same 1920 × 1200 SLM, in complex64, on 24 cores. Every propagator gives the analytic Gaussian beam to 10⁻⁸ in
|
|
60
|
+
complex128.
|
|
61
|
+
|
|
62
|
+
| case | run time |
|
|
63
|
+
|---|---|
|
|
64
|
+
| `focus`, window 256² | 10 ms |
|
|
65
|
+
| `focus` on 256², then `free`, 101 planes | 0.2 s |
|
|
66
|
+
| `focus_fft`, whole plane (3840 × 2400) | 50 ms |
|
|
67
|
+
|
|
68
|
+
Details in the [benchmark](https://melvinrdn.github.io/fourierslm/benchmark.html).
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# fourierslm
|
|
2
|
+
|
|
3
|
+
[](https://github.com/melvinrdn/fourierslm/actions/workflows/ci.yml)
|
|
4
|
+
[](https://melvinrdn.github.io/fourierslm)
|
|
5
|
+
|
|
6
|
+
Fourier optics of a phase-only SLM: the beam on the SLM, its phase pattern, and the field it makes in the focal region
|
|
7
|
+
of a lens, in physical units.
|
|
8
|
+
|
|
9
|
+
**Documentation:** https://melvinrdn.github.io/fourierslm
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install fourierslm # or: uv add fourierslm
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
A Gaussian beam of 1 W on a 1920 × 1200 SLM with 8 µm pixels, a vortex of charge 1, and the field at the focus of a
|
|
20
|
+
50 cm lens placed 30 cm after the SLM.
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import numpy as np
|
|
24
|
+
import fourierslm as fs
|
|
25
|
+
from fourierslm import beam, phase, propagate
|
|
26
|
+
|
|
27
|
+
X, Y = fs.grid(1920, 8e-6), fs.grid(1200, 8e-6)
|
|
28
|
+
E = beam.gaussian(X, Y, w=2e-3, power=1.0) * np.exp(1j * phase.vortex(X, Y, charge=1))
|
|
29
|
+
x = y = np.linspace(-300e-6, 300e-6, 256) # window at the focus [m]
|
|
30
|
+
E_focus = propagate.focus(E, X, Y, x, y, wavelength=1030e-9, f=0.5, slm_distance=0.3)
|
|
31
|
+
I = beam.intensity(E_focus) # [W/m²]
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Speed
|
|
35
|
+
|
|
36
|
+
On the same 1920 × 1200 SLM, in complex64, on 24 cores. Every propagator gives the analytic Gaussian beam to 10⁻⁸ in
|
|
37
|
+
complex128.
|
|
38
|
+
|
|
39
|
+
| case | run time |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `focus`, window 256² | 10 ms |
|
|
42
|
+
| `focus` on 256², then `free`, 101 planes | 0.2 s |
|
|
43
|
+
| `focus_fft`, whole plane (3840 × 2400) | 50 ms |
|
|
44
|
+
|
|
45
|
+
Details in the [benchmark](https://melvinrdn.github.io/fourierslm/benchmark.html).
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fourierslm"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Fourier optics of a phase-only SLM: input beams, phase patterns and propagation to the focus of a lens"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{name = "Melvin Redon"}]
|
|
14
|
+
keywords = ["fourier optics", "slm", "spatial light modulator", "beam propagation", "collins integral"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Intended Audience :: Science/Research",
|
|
22
|
+
"Topic :: Scientific/Engineering :: Physics",
|
|
23
|
+
"Operating System :: OS Independent",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"numpy>=1.24",
|
|
27
|
+
"scipy>=1.10",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Documentation = "https://melvinrdn.github.io/fourierslm"
|
|
32
|
+
Repository = "https://github.com/melvinrdn/fourierslm"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = ["pytest>=8"]
|
|
36
|
+
docs = [
|
|
37
|
+
"sphinx>=8",
|
|
38
|
+
"pydata-sphinx-theme>=0.16",
|
|
39
|
+
"numpydoc>=1.8",
|
|
40
|
+
"myst-parser>=4",
|
|
41
|
+
"matplotlib>=3.8",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.sdist]
|
|
45
|
+
include = ["src", "tests", "README.md", "LICENSE"]
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
testpaths = ["tests"]
|
|
49
|
+
|
|
50
|
+
[tool.ruff]
|
|
51
|
+
line-length = 120
|
|
52
|
+
|
|
53
|
+
[tool.ruff.lint]
|
|
54
|
+
extend-select = ["E501", "I", "UP", "B", "NPY", "C4", "PERF", "D"]
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint.isort]
|
|
57
|
+
combine-as-imports = true
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint.per-file-ignores]
|
|
60
|
+
"tests/*" = ["D"]
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint.pydocstyle]
|
|
63
|
+
convention = "numpy"
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Fourier optics of a phase-only SLM: input beams, phase patterns and propagation to the focus of a lens."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from . import beam, phase, propagate
|
|
6
|
+
|
|
7
|
+
__all__ = ["beam", "grid", "phase", "propagate"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def grid(n: int, pitch: float) -> np.ndarray:
|
|
11
|
+
"""Centered pixel coordinates, with the axis on pixel n // 2.
|
|
12
|
+
|
|
13
|
+
The same convention as `propagate.focus_fft`, so the phase patterns line up with its output.
|
|
14
|
+
|
|
15
|
+
Parameters
|
|
16
|
+
----------
|
|
17
|
+
n : int
|
|
18
|
+
Number of pixels.
|
|
19
|
+
pitch : float
|
|
20
|
+
Pixel pitch [m].
|
|
21
|
+
|
|
22
|
+
Returns
|
|
23
|
+
-------
|
|
24
|
+
ndarray
|
|
25
|
+
Coordinates (i − n // 2)·pitch of the pixels i = 0 … n − 1 [m].
|
|
26
|
+
"""
|
|
27
|
+
return (np.arange(n) - n // 2) * pitch
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Input beams on the SLM.
|
|
2
|
+
|
|
3
|
+
Fields are complex analytic fields [V/m], so the intensity is I = ½ε₀c|E|². Coordinates X and Y are 1D, and the
|
|
4
|
+
arrays have the shape (len(Y), len(X)).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from scipy.constants import c, epsilon_0
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def intensity(E: np.ndarray) -> np.ndarray:
|
|
12
|
+
"""Intensity of a field.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
E : ndarray
|
|
17
|
+
Complex field [V/m].
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
ndarray
|
|
22
|
+
I = ½ε₀c|E|² [W/m²].
|
|
23
|
+
"""
|
|
24
|
+
return 0.5 * epsilon_0 * c * np.abs(E) ** 2
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _amplitude(I0):
|
|
28
|
+
"""Field amplitude of the intensity I0."""
|
|
29
|
+
return np.sqrt(2 * I0 / (epsilon_0 * c))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _r2(X, Y, center):
|
|
33
|
+
"""Squared distance to center, shape (len(Y), len(X))."""
|
|
34
|
+
return (np.asarray(X)[None, :] - center[0]) ** 2 + (np.asarray(Y)[:, None] - center[1]) ** 2
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def gaussian(
|
|
38
|
+
X: np.ndarray, Y: np.ndarray, w: float, power: float = 1.0, center: tuple[float, float] = (0.0, 0.0)
|
|
39
|
+
) -> np.ndarray:
|
|
40
|
+
"""Gaussian beam with a flat wavefront.
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
----------
|
|
44
|
+
X, Y : ndarray
|
|
45
|
+
1D coordinates [m].
|
|
46
|
+
w : float
|
|
47
|
+
Radius at 1/e² of the intensity [m].
|
|
48
|
+
power : float, default 1
|
|
49
|
+
Power [W].
|
|
50
|
+
center : tuple of float, default (0, 0)
|
|
51
|
+
Center of the beam [m].
|
|
52
|
+
|
|
53
|
+
Returns
|
|
54
|
+
-------
|
|
55
|
+
ndarray
|
|
56
|
+
Real field E [V/m], with I = 2P/(πw²)·exp(−2r²/w²).
|
|
57
|
+
"""
|
|
58
|
+
return _amplitude(2 * power / (np.pi * w**2)) * np.exp(-_r2(X, Y, center) / w**2)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def flattop(
|
|
62
|
+
X: np.ndarray, Y: np.ndarray, radius: float, power: float = 1.0, center: tuple[float, float] = (0.0, 0.0)
|
|
63
|
+
) -> np.ndarray:
|
|
64
|
+
"""Uniform beam cut by a circular aperture.
|
|
65
|
+
|
|
66
|
+
Parameters
|
|
67
|
+
----------
|
|
68
|
+
X, Y : ndarray
|
|
69
|
+
1D coordinates [m].
|
|
70
|
+
radius : float
|
|
71
|
+
Radius of the aperture [m].
|
|
72
|
+
power : float, default 1
|
|
73
|
+
Power [W] of the continuous disk. The pixelated edge changes it slightly.
|
|
74
|
+
center : tuple of float, default (0, 0)
|
|
75
|
+
Center of the beam [m].
|
|
76
|
+
|
|
77
|
+
Returns
|
|
78
|
+
-------
|
|
79
|
+
ndarray
|
|
80
|
+
Real field E [V/m], with I = P/(π radius²) inside the disk and 0 outside.
|
|
81
|
+
"""
|
|
82
|
+
return _amplitude(power / (np.pi * radius**2)) * (_r2(X, Y, center) <= radius**2)
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"""Phase patterns for the SLM [rad].
|
|
2
|
+
|
|
3
|
+
The patterns are continuous, not wrapped, so they add. Wrap or quantize the sum only for display. Coordinates X and Y
|
|
4
|
+
are 1D, the arrays have the shape (len(Y), len(X)), and angles are in radians. The sign convention is the plane wave
|
|
5
|
+
exp[i(kz − ωt)], so a pattern that grows along +X steers the beam toward +x.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _xy(X, Y, center):
|
|
12
|
+
"""Coordinates relative to center, broadcast to (len(Y), len(X))."""
|
|
13
|
+
return np.asarray(X)[None, :] - center[0], np.asarray(Y)[:, None] - center[1]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def lens(
|
|
17
|
+
X: np.ndarray, Y: np.ndarray, wavelength: float, f: float, center: tuple[float, float] = (0.0, 0.0)
|
|
18
|
+
) -> np.ndarray:
|
|
19
|
+
"""Thin lens, −k(X² + Y²)/(2f).
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
X, Y : ndarray
|
|
24
|
+
1D coordinates [m].
|
|
25
|
+
wavelength : float
|
|
26
|
+
Wavelength [m].
|
|
27
|
+
f : float
|
|
28
|
+
Focal length [m], positive for a converging lens.
|
|
29
|
+
center : tuple of float, default (0, 0)
|
|
30
|
+
Center of the lens [m].
|
|
31
|
+
|
|
32
|
+
Returns
|
|
33
|
+
-------
|
|
34
|
+
ndarray
|
|
35
|
+
Phase [rad].
|
|
36
|
+
"""
|
|
37
|
+
x, y = _xy(X, Y, center)
|
|
38
|
+
return -np.pi / (wavelength * f) * (x**2 + y**2)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def axicon(
|
|
42
|
+
X: np.ndarray, Y: np.ndarray, wavelength: float, angle: float, center: tuple[float, float] = (0.0, 0.0)
|
|
43
|
+
) -> np.ndarray:
|
|
44
|
+
"""Axicon, −kR sin(angle), which focuses to a Bessel beam.
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
----------
|
|
48
|
+
X, Y : ndarray
|
|
49
|
+
1D coordinates [m].
|
|
50
|
+
wavelength : float
|
|
51
|
+
Wavelength [m].
|
|
52
|
+
angle : float
|
|
53
|
+
Half angle of the cone of rays [rad].
|
|
54
|
+
center : tuple of float, default (0, 0)
|
|
55
|
+
Apex [m].
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
-------
|
|
59
|
+
ndarray
|
|
60
|
+
Phase [rad].
|
|
61
|
+
"""
|
|
62
|
+
x, y = _xy(X, Y, center)
|
|
63
|
+
return -2 * np.pi / wavelength * np.sin(angle) * np.hypot(x, y)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def grating(X: np.ndarray, Y: np.ndarray, wavelength: float, angle: float, azimuth: float = 0.0) -> np.ndarray:
|
|
67
|
+
"""Blazed grating, k sin(angle)·(X cos ψ + Y sin ψ).
|
|
68
|
+
|
|
69
|
+
Behind a lens of focal length f, it moves the focus to f sin(angle) along the azimuth ψ.
|
|
70
|
+
|
|
71
|
+
Parameters
|
|
72
|
+
----------
|
|
73
|
+
X, Y : ndarray
|
|
74
|
+
1D coordinates [m].
|
|
75
|
+
wavelength : float
|
|
76
|
+
Wavelength [m].
|
|
77
|
+
angle : float
|
|
78
|
+
Deflection angle [rad].
|
|
79
|
+
azimuth : float, default 0
|
|
80
|
+
Direction ψ of the deflection [rad], 0 along +x and π/2 along +y.
|
|
81
|
+
|
|
82
|
+
Returns
|
|
83
|
+
-------
|
|
84
|
+
ndarray
|
|
85
|
+
Phase [rad].
|
|
86
|
+
"""
|
|
87
|
+
x, y = _xy(X, Y, (0.0, 0.0))
|
|
88
|
+
return 2 * np.pi / wavelength * np.sin(angle) * (x * np.cos(azimuth) + y * np.sin(azimuth))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def vortex(
|
|
92
|
+
X: np.ndarray, Y: np.ndarray, charge: float, center: tuple[float, float] = (0.0, 0.0), rotation: float = 0.0
|
|
93
|
+
) -> np.ndarray:
|
|
94
|
+
"""Vortex, ℓψ.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
X, Y : ndarray
|
|
99
|
+
1D coordinates [m].
|
|
100
|
+
charge : float
|
|
101
|
+
Topological charge ℓ, integer or fractional.
|
|
102
|
+
center : tuple of float, default (0, 0)
|
|
103
|
+
Center of the vortex [m].
|
|
104
|
+
rotation : float, default 0
|
|
105
|
+
Azimuth of the 2πℓ step is π + rotation [rad]. It only shows for a fractional charge.
|
|
106
|
+
|
|
107
|
+
Returns
|
|
108
|
+
-------
|
|
109
|
+
ndarray
|
|
110
|
+
Phase [rad], with the azimuth ψ − rotation taken in (−π, π].
|
|
111
|
+
"""
|
|
112
|
+
x, y = _xy(X, Y, center)
|
|
113
|
+
psi = np.arctan2(y, x)
|
|
114
|
+
if rotation:
|
|
115
|
+
psi = np.angle(np.exp(1j * (psi - rotation)))
|
|
116
|
+
return charge * psi
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def binary(X: np.ndarray, Y: np.ndarray, period: float, depth: float = np.pi, azimuth: float = 0.0) -> np.ndarray:
|
|
120
|
+
"""Binary grating, stripes of 0 and depth.
|
|
121
|
+
|
|
122
|
+
Parameters
|
|
123
|
+
----------
|
|
124
|
+
X, Y : ndarray
|
|
125
|
+
1D coordinates [m].
|
|
126
|
+
period : float
|
|
127
|
+
Period, two stripes [m].
|
|
128
|
+
depth : float, default π
|
|
129
|
+
Phase of every other stripe [rad].
|
|
130
|
+
azimuth : float, default 0
|
|
131
|
+
Direction ψ across the stripes [rad], 0 for stripes along y.
|
|
132
|
+
|
|
133
|
+
Returns
|
|
134
|
+
-------
|
|
135
|
+
ndarray
|
|
136
|
+
Phase [rad], 0 on the stripe that starts at the origin.
|
|
137
|
+
"""
|
|
138
|
+
x, y = _xy(X, Y, (0.0, 0.0))
|
|
139
|
+
u = x * np.cos(azimuth) + y * np.sin(azimuth)
|
|
140
|
+
return depth * (np.floor(2 * u / period + 1e-9) % 2) # pixels on an edge go to the next stripe, not by rounding
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def radial_step(
|
|
144
|
+
X: np.ndarray,
|
|
145
|
+
Y: np.ndarray,
|
|
146
|
+
radius: float,
|
|
147
|
+
inner: float = 0.0,
|
|
148
|
+
outer: float = np.pi,
|
|
149
|
+
center: tuple[float, float] = (0.0, 0.0),
|
|
150
|
+
) -> np.ndarray:
|
|
151
|
+
"""Phase step at a radius.
|
|
152
|
+
|
|
153
|
+
Parameters
|
|
154
|
+
----------
|
|
155
|
+
X, Y : ndarray
|
|
156
|
+
1D coordinates [m].
|
|
157
|
+
radius : float
|
|
158
|
+
Radius of the step [m].
|
|
159
|
+
inner, outer : float, default 0 and π
|
|
160
|
+
Phase inside and outside the radius [rad].
|
|
161
|
+
center : tuple of float, default (0, 0)
|
|
162
|
+
Center of the step [m].
|
|
163
|
+
|
|
164
|
+
Returns
|
|
165
|
+
-------
|
|
166
|
+
ndarray
|
|
167
|
+
Phase [rad].
|
|
168
|
+
"""
|
|
169
|
+
x, y = _xy(X, Y, center)
|
|
170
|
+
return np.where(x**2 + y**2 <= radius**2, inner, outer)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def wrap(phase: np.ndarray) -> np.ndarray:
|
|
174
|
+
"""Wrap a phase into [0, 2π).
|
|
175
|
+
|
|
176
|
+
Parameters
|
|
177
|
+
----------
|
|
178
|
+
phase : ndarray
|
|
179
|
+
Phase [rad].
|
|
180
|
+
|
|
181
|
+
Returns
|
|
182
|
+
-------
|
|
183
|
+
ndarray
|
|
184
|
+
Phase modulo 2π [rad].
|
|
185
|
+
"""
|
|
186
|
+
return np.mod(phase, 2 * np.pi)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def quantize(phase: np.ndarray, bits: int) -> np.ndarray:
|
|
190
|
+
"""Round a phase to the gray levels of an SLM.
|
|
191
|
+
|
|
192
|
+
Parameters
|
|
193
|
+
----------
|
|
194
|
+
phase : ndarray
|
|
195
|
+
Phase [rad].
|
|
196
|
+
bits : int
|
|
197
|
+
Bit depth of the SLM, 2^bits levels over 2π.
|
|
198
|
+
|
|
199
|
+
Returns
|
|
200
|
+
-------
|
|
201
|
+
ndarray
|
|
202
|
+
Phase on the levels 2πn/2^bits, n = 0 … 2^bits − 1 [rad]. 2π rounds to 0.
|
|
203
|
+
"""
|
|
204
|
+
levels = 2**bits
|
|
205
|
+
return 2 * np.pi / levels * (np.round(wrap(phase) * levels / (2 * np.pi)) % levels)
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Propagation from the SLM, through a lens, to the focal region, and in free space.
|
|
2
|
+
|
|
3
|
+
The SLM sits at a distance L in front of a lens of focal length f, and z is measured from the back focal plane. With
|
|
4
|
+
the ray matrix of this setup, A = −z/f, B = f + z(1 − L/f), D = 1 − L/f, the paraxial field is the Collins integral
|
|
5
|
+
|
|
6
|
+
E(x, y, z) = 1/(iλB) · exp[ikD(x² + y²)/(2B)] ∬ E(X, Y) exp[ikA(X² + Y²)/(2B)] exp[−ik(xX + yY)/B] dX dY.
|
|
7
|
+
|
|
8
|
+
At z = 0 it is the FT of the SLM field times the curvature exp[ik(1 − L/f)(x² + y²)/(2f)], Goodman (5-19). The plane
|
|
9
|
+
wave is exp[i(kz − ωt)], fields are complex analytic fields [V/m], and the constant phase exp[ik(L + f + z)] of the
|
|
10
|
+
carrier is left out. Arrays have the shape (len(Y), len(X)). Complex64 fields are propagated in complex64, which is
|
|
11
|
+
about twice as fast, and FFTs and matrix products run on every core.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import numpy as np
|
|
15
|
+
import scipy.fft
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _abd(z, f, slm_distance):
|
|
19
|
+
"""Elements A, B, D of the ray matrix from the SLM to the plane z."""
|
|
20
|
+
L = f if slm_distance is None else slm_distance
|
|
21
|
+
return -z / f, f + z * (1 - L / f), 1 - L / f
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _dtype(E):
|
|
25
|
+
"""Complex dtype of the computation, complex64 only for single-precision input."""
|
|
26
|
+
return np.result_type(E, np.complex64)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _expi(phi, dtype):
|
|
30
|
+
"""exp(iφ) in dtype, from a float64 phase, by cos and sin (several times faster than a complex exp)."""
|
|
31
|
+
phi = phi.astype(np.finfo(dtype).dtype, copy=False)
|
|
32
|
+
out = np.empty(phi.shape, dtype)
|
|
33
|
+
np.cos(phi, out=out.real)
|
|
34
|
+
np.sin(phi, out=out.imag)
|
|
35
|
+
return out
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _kernel(X, x, k, A, B, D, dtype):
|
|
39
|
+
"""Collins kernel along one axis, exp[ik(AX² − 2xX + Dx²)/(2B)], shape (len(X), len(x))."""
|
|
40
|
+
X, x = np.asarray(X, float), np.asarray(x, float)
|
|
41
|
+
phi = np.outer(X, -2 * x)
|
|
42
|
+
phi += A * X[:, None] ** 2
|
|
43
|
+
phi += D * x**2
|
|
44
|
+
phi *= k / (2 * B)
|
|
45
|
+
return _expi(phi, dtype)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _fft_size(n):
|
|
49
|
+
"""Smallest even FFT-friendly size ≥ n."""
|
|
50
|
+
m = scipy.fft.next_fast_len(n)
|
|
51
|
+
while m % 2:
|
|
52
|
+
m = scipy.fft.next_fast_len(m + 1)
|
|
53
|
+
return m
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def focus(
|
|
57
|
+
E: np.ndarray,
|
|
58
|
+
X: np.ndarray,
|
|
59
|
+
Y: np.ndarray,
|
|
60
|
+
x: np.ndarray,
|
|
61
|
+
y: np.ndarray,
|
|
62
|
+
wavelength: float,
|
|
63
|
+
f: float,
|
|
64
|
+
z: float | np.ndarray = 0.0,
|
|
65
|
+
slm_distance: float | None = None,
|
|
66
|
+
) -> np.ndarray:
|
|
67
|
+
"""Field in the focal region, on any window and in any plane.
|
|
68
|
+
|
|
69
|
+
The Collins integral as two matrix products (matrix Fourier transform), so the window (x, y) is free and no
|
|
70
|
+
padding is needed. Each plane z is exact and independent of the others.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
E : ndarray
|
|
75
|
+
Field on the SLM [V/m], shape (len(Y), len(X)).
|
|
76
|
+
X, Y : ndarray
|
|
77
|
+
Uniform 1D coordinates on the SLM [m].
|
|
78
|
+
x, y : ndarray
|
|
79
|
+
1D coordinates of the window [m], any sampling.
|
|
80
|
+
wavelength : float
|
|
81
|
+
Wavelength [m].
|
|
82
|
+
f : float
|
|
83
|
+
Focal length of the lens [m].
|
|
84
|
+
z : float or ndarray, default 0
|
|
85
|
+
Distance from the back focal plane [m], one value or a 1D scan.
|
|
86
|
+
slm_distance : float, optional
|
|
87
|
+
Distance L from the SLM to the lens [m], f by default.
|
|
88
|
+
|
|
89
|
+
Returns
|
|
90
|
+
-------
|
|
91
|
+
ndarray
|
|
92
|
+
Field [V/m], shape (len(y), len(x)), or (len(z), len(y), len(x)) for a scan.
|
|
93
|
+
"""
|
|
94
|
+
k = 2 * np.pi / wavelength
|
|
95
|
+
dtype = _dtype(E)
|
|
96
|
+
area = (X[1] - X[0]) * (Y[1] - Y[0])
|
|
97
|
+
zs = np.atleast_1d(z)
|
|
98
|
+
out = np.empty((len(zs), len(y), len(x)), dtype)
|
|
99
|
+
for i, zi in enumerate(zs):
|
|
100
|
+
A, B, D = _abd(zi, f, slm_distance)
|
|
101
|
+
Ky = _kernel(Y, y, k, A, B, D, dtype).T
|
|
102
|
+
Kx = _kernel(X, x, k, A, B, D, dtype)
|
|
103
|
+
out[i] = np.linalg.multi_dot([Ky, E, Kx])
|
|
104
|
+
out[i] *= complex(area / (1j * wavelength * B))
|
|
105
|
+
return out[0] if np.ndim(z) == 0 else out
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def focus_fft(
|
|
109
|
+
E: np.ndarray,
|
|
110
|
+
pitch: float,
|
|
111
|
+
wavelength: float,
|
|
112
|
+
f: float,
|
|
113
|
+
z: float = 0.0,
|
|
114
|
+
slm_distance: float | None = None,
|
|
115
|
+
pad: float = 2,
|
|
116
|
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
117
|
+
"""Field in a plane of the focal region, on the whole FFT grid.
|
|
118
|
+
|
|
119
|
+
The same Collins integral as `focus`, by one FFT of the padded SLM field. Faster than `focus` when the whole plane
|
|
120
|
+
is needed, and the window is fixed by the sampling.
|
|
121
|
+
|
|
122
|
+
Parameters
|
|
123
|
+
----------
|
|
124
|
+
E : ndarray
|
|
125
|
+
Field on the SLM [V/m], on the pixels of `fourierslm.grid`.
|
|
126
|
+
pitch : float
|
|
127
|
+
Pixel pitch of the SLM [m].
|
|
128
|
+
wavelength : float
|
|
129
|
+
Wavelength [m].
|
|
130
|
+
f : float
|
|
131
|
+
Focal length of the lens [m].
|
|
132
|
+
z : float, default 0
|
|
133
|
+
Distance from the back focal plane [m].
|
|
134
|
+
slm_distance : float, optional
|
|
135
|
+
Distance L from the SLM to the lens [m], f by default.
|
|
136
|
+
pad : float, default 2
|
|
137
|
+
Zero padding, the FFT size over the SLM size along each axis, rounded up to an even FFT-friendly size N.
|
|
138
|
+
|
|
139
|
+
Returns
|
|
140
|
+
-------
|
|
141
|
+
E_focus : ndarray
|
|
142
|
+
Field [V/m], shape (Ny, Nx).
|
|
143
|
+
x, y : ndarray
|
|
144
|
+
Its coordinates [m], with the pitch λB/(N·pitch) and the axis on index N // 2.
|
|
145
|
+
"""
|
|
146
|
+
k = 2 * np.pi / wavelength
|
|
147
|
+
A, B, D = _abd(z, f, slm_distance)
|
|
148
|
+
dtype = _dtype(E)
|
|
149
|
+
(ny, nx), (Ny, Nx) = E.shape, tuple(_fft_size(int(np.ceil(pad * n))) for n in E.shape)
|
|
150
|
+
|
|
151
|
+
def factors(n, N):
|
|
152
|
+
"""Centering signs and chirps along one axis, before and after the FFT, and the output coordinates."""
|
|
153
|
+
start = N // 2 - n // 2
|
|
154
|
+
X = (np.arange(n) - n // 2) * pitch
|
|
155
|
+
x = (np.arange(N) - N // 2) * wavelength * B / (N * pitch)
|
|
156
|
+
before = (-1.0) ** np.arange(start, start + n) * np.exp(1j * k * A * X**2 / (2 * B))
|
|
157
|
+
after = (-1.0) ** (np.arange(N) + N // 2) * np.exp(1j * k * D * x**2 / (2 * B))
|
|
158
|
+
return start, before.astype(dtype), after.astype(dtype), x
|
|
159
|
+
|
|
160
|
+
y0, by, ay, y = factors(ny, Ny)
|
|
161
|
+
x0, bx, ax, x = factors(nx, Nx)
|
|
162
|
+
|
|
163
|
+
G = np.zeros((Ny, Nx), dtype)
|
|
164
|
+
view = G[y0 : y0 + ny, x0 : x0 + nx]
|
|
165
|
+
np.multiply(E, by[:, None], out=view)
|
|
166
|
+
view *= bx * dtype.type(pitch**2 / (1j * wavelength * B))
|
|
167
|
+
G = scipy.fft.fft2(G, overwrite_x=True, workers=-1)
|
|
168
|
+
G *= ay[:, None]
|
|
169
|
+
G *= ax
|
|
170
|
+
return G, x, y
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def free(E: np.ndarray, pitch: float, wavelength: float, z: float | np.ndarray, bandlimit: bool = True) -> np.ndarray:
|
|
174
|
+
"""Free-space propagation by z, angular spectrum on a fixed grid.
|
|
175
|
+
|
|
176
|
+
The transfer function is exp[iz(k_z − k)], exact beyond the paraxial approximation, with the evanescent waves
|
|
177
|
+
dropped. The grid is periodic, so keep the field away from its edges. A scan reuses the FFT of E, so it costs one
|
|
178
|
+
inverse FFT per plane.
|
|
179
|
+
|
|
180
|
+
Parameters
|
|
181
|
+
----------
|
|
182
|
+
E : ndarray
|
|
183
|
+
Field [V/m].
|
|
184
|
+
pitch : float
|
|
185
|
+
Pixel pitch [m].
|
|
186
|
+
wavelength : float
|
|
187
|
+
Wavelength [m].
|
|
188
|
+
z : float or ndarray
|
|
189
|
+
Distance [m], negative to propagate backward, one value or a 1D scan.
|
|
190
|
+
bandlimit : bool, default True
|
|
191
|
+
Cut the spatial frequencies the grid cannot sample at this distance, above 1/(λ√(1 + (2z/(N·pitch))²)) along
|
|
192
|
+
each axis, Matsushima and Shimobaba, Opt. Express 17, 19662 (2009).
|
|
193
|
+
|
|
194
|
+
Returns
|
|
195
|
+
-------
|
|
196
|
+
ndarray
|
|
197
|
+
Field [V/m] on the same grid, shape (ny, nx) like E, or (len(z), ny, nx) for a scan.
|
|
198
|
+
"""
|
|
199
|
+
k = 2 * np.pi / wavelength
|
|
200
|
+
dtype = _dtype(E)
|
|
201
|
+
ny, nx = E.shape
|
|
202
|
+
fx, fy = scipy.fft.fftfreq(nx, pitch)[None, :], scipy.fft.fftfreq(ny, pitch)[:, None]
|
|
203
|
+
s2 = (wavelength * fx) ** 2 + (wavelength * fy) ** 2
|
|
204
|
+
propagating = s2 < 1
|
|
205
|
+
kz_minus_k = -k * s2 / (1 + np.sqrt(np.where(propagating, 1 - s2, 1))) # without the cancellation of k_z − k
|
|
206
|
+
G = scipy.fft.fft2(E, workers=-1)
|
|
207
|
+
zs = np.atleast_1d(z)
|
|
208
|
+
out = np.empty((len(zs), ny, nx), dtype)
|
|
209
|
+
for i, zi in enumerate(zs):
|
|
210
|
+
keep = propagating
|
|
211
|
+
if bandlimit:
|
|
212
|
+
keep = keep & (wavelength * np.abs(fx) * np.hypot(1, 2 * zi / (nx * pitch)) <= 1)
|
|
213
|
+
keep &= wavelength * np.abs(fy) * np.hypot(1, 2 * zi / (ny * pitch)) <= 1
|
|
214
|
+
H = _expi(zi * kz_minus_k, dtype)
|
|
215
|
+
H *= keep
|
|
216
|
+
H *= G
|
|
217
|
+
out[i] = scipy.fft.ifft2(H, overwrite_x=True, workers=-1)
|
|
218
|
+
return out[0] if np.ndim(z) == 0 else out
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
import fourierslm as fs
|
|
4
|
+
from fourierslm import beam
|
|
5
|
+
|
|
6
|
+
PITCH = 8e-6
|
|
7
|
+
X, Y = fs.grid(512, PITCH), fs.grid(384, PITCH)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_grid():
|
|
11
|
+
x = fs.grid(6, 2.0)
|
|
12
|
+
assert np.array_equal(x, [-6, -4, -2, 0, 2, 4])
|
|
13
|
+
assert fs.grid(5, 1.0)[2] == 0
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_gaussian():
|
|
17
|
+
E = beam.gaussian(X, Y, 0.3e-3, power=2.0, center=(40e-6, -80e-6))
|
|
18
|
+
I = beam.intensity(E)
|
|
19
|
+
assert E.shape == (384, 512)
|
|
20
|
+
assert np.isclose(I.sum() * PITCH**2, 2.0, rtol=1e-9)
|
|
21
|
+
assert np.isclose(I[192 - 10, 256 + 5], 2 * 2.0 / (np.pi * 0.3e-3**2))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_flattop():
|
|
25
|
+
E = beam.flattop(X, Y, 1e-3, power=0.5)
|
|
26
|
+
I = beam.intensity(E)
|
|
27
|
+
assert np.isclose(I.max(), 0.5 / (np.pi * 1e-3**2))
|
|
28
|
+
assert np.isclose(I.sum() * PITCH**2, 0.5, rtol=1e-2)
|
|
29
|
+
assert I[192, 256 + 126] == 0 and I[192, 256 + 125] > 0
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
import fourierslm as fs
|
|
5
|
+
from fourierslm import beam, phase, propagate
|
|
6
|
+
|
|
7
|
+
LAM, F, PITCH = 1030e-9, 0.2, 8e-6
|
|
8
|
+
X = Y = fs.grid(512, PITCH)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_lens_sign():
|
|
12
|
+
"""A converging lens delays the edge, −k r²/(2f)."""
|
|
13
|
+
p = phase.lens(X, Y, LAM, F, center=(1e-4, 0.0))
|
|
14
|
+
assert p.shape == (512, 512)
|
|
15
|
+
assert np.isclose(p[256, 256 + 100], -np.pi / (LAM * F) * (100 * PITCH - 1e-4) ** 2)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@pytest.mark.parametrize(("azimuth", "axis"), [(0.0, "x"), (np.pi / 2, "y")])
|
|
19
|
+
def test_grating_steers_the_focus(azimuth, axis):
|
|
20
|
+
angle = 2e-4
|
|
21
|
+
E = beam.gaussian(X, Y, 0.4e-3) * np.exp(1j * phase.grating(X, Y, LAM, angle, azimuth))
|
|
22
|
+
u = np.linspace(-80e-6, 80e-6, 321)
|
|
23
|
+
I = np.abs(propagate.focus(E, X, Y, u, u, LAM, F)) ** 2
|
|
24
|
+
iy, ix = np.unravel_index(np.argmax(I), I.shape)
|
|
25
|
+
peak = u[ix] if axis == "x" else u[iy]
|
|
26
|
+
assert abs(peak - F * np.sin(angle)) <= u[1] - u[0]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_vortex_winds_once_per_charge():
|
|
30
|
+
p = phase.vortex(X, Y, 3)
|
|
31
|
+
t = np.linspace(0, 2 * np.pi, 400)
|
|
32
|
+
ring = p[(256 + 50 * np.sin(t)).astype(int), (256 + 50 * np.cos(t)).astype(int)]
|
|
33
|
+
assert np.isclose(np.sum(np.angle(np.exp(1j * np.diff(ring)))), 6 * np.pi)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_vortex_rotation_moves_the_step():
|
|
37
|
+
p = phase.vortex(X, Y, 0.5, rotation=np.pi / 2)
|
|
38
|
+
assert np.isclose(p[256 - 50, 256], 0.5 * np.pi, atol=1e-12) or np.isclose(p[256 - 50, 256], -0.5 * np.pi)
|
|
39
|
+
assert np.isclose(p[256 + 50, 256], 0.0, atol=1e-12)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_axicon():
|
|
43
|
+
p = phase.axicon(X, Y, LAM, 1e-3)
|
|
44
|
+
assert np.isclose(p[256 + 30, 256 - 40], -2 * np.pi / LAM * np.sin(1e-3) * 50 * PITCH)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_binary():
|
|
48
|
+
p = phase.binary(X, Y, period=10 * PITCH, depth=2.0)
|
|
49
|
+
assert set(np.unique(p)) == {0.0, 2.0}
|
|
50
|
+
assert np.array_equal(p[0, 256:266], [0, 0, 0, 0, 0, 2, 2, 2, 2, 2])
|
|
51
|
+
assert np.array_equal(phase.binary(X, Y, 10 * PITCH, azimuth=np.pi / 2), phase.binary(X, Y, 10 * PITCH).T)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_radial_step():
|
|
55
|
+
p = phase.radial_step(X, Y, 20 * PITCH, inner=1.0, outer=3.0)
|
|
56
|
+
assert p[256, 256 + 20] == 1.0 and p[256, 256 + 21] == 3.0
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_wrap():
|
|
60
|
+
p = phase.wrap(np.array([-0.5, 0.0, 2 * np.pi, 7.0]))
|
|
61
|
+
assert np.allclose(p, [2 * np.pi - 0.5, 0.0, 0.0, 7.0 - 2 * np.pi])
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_quantize():
|
|
65
|
+
p = phase.quantize(np.linspace(-10, 10, 10001), bits=3)
|
|
66
|
+
assert np.allclose(np.unique(p), 2 * np.pi / 8 * np.arange(8))
|
|
67
|
+
assert phase.quantize(np.array([2 * np.pi - 1e-3]), bits=8)[0] == 0.0
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
import fourierslm as fs
|
|
5
|
+
from fourierslm import beam, phase, propagate
|
|
6
|
+
|
|
7
|
+
LAM, F, PITCH, W, P = 1030e-9, 0.2, 8e-6, 0.4e-3, 2.0
|
|
8
|
+
X, Y = fs.grid(512, PITCH), fs.grid(384, PITCH)
|
|
9
|
+
W0 = LAM * F / (np.pi * W) # waist at the focus
|
|
10
|
+
ZR = np.pi * W0**2 / LAM
|
|
11
|
+
I0 = 2 * P / (np.pi * W0**2)
|
|
12
|
+
x = np.linspace(-3 * W0, 3 * W0, 121)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pytest.fixture(scope="module")
|
|
16
|
+
def E():
|
|
17
|
+
return beam.gaussian(X, Y, W, P).astype(complex)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_gaussian_focus(E):
|
|
21
|
+
I = beam.intensity(propagate.focus(E, X, Y, x, x, LAM, F))
|
|
22
|
+
assert np.allclose(I, I0 * np.exp(-2 * (x[None, :] ** 2 + x[:, None] ** 2) / W0**2), rtol=0, atol=1e-6 * I0)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_z_scan_intensity_and_gouy_phase(E):
|
|
26
|
+
z = np.linspace(-3 * ZR, 3 * ZR, 13)
|
|
27
|
+
axis = propagate.focus(E, X, Y, np.zeros(1), np.zeros(1), LAM, F, z)[:, 0, 0]
|
|
28
|
+
assert np.allclose(beam.intensity(axis), I0 / (1 + (z / ZR) ** 2), rtol=1e-6)
|
|
29
|
+
assert np.allclose(np.angle(axis * np.exp(1j * (np.pi / 2 + np.arctan(z / ZR)))), 0, atol=1e-6)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_scalar_z_and_scan_agree(E):
|
|
33
|
+
z = np.array([-ZR, 0.5 * ZR])
|
|
34
|
+
scan = propagate.focus(E, X, Y, x, x[:50], LAM, F, z, slm_distance=0.1)
|
|
35
|
+
for i, zi in enumerate(z):
|
|
36
|
+
assert np.array_equal(scan[i], propagate.focus(E, X, Y, x, x[:50], LAM, F, zi, slm_distance=0.1))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_lens_on_the_slm_moves_the_focus(E):
|
|
40
|
+
"""A lens of focal length f_slm on the SLM moves the focus to z = −f²/f_slm."""
|
|
41
|
+
f_slm = 20.0
|
|
42
|
+
z = np.linspace(-6, 4, 201) * ZR
|
|
43
|
+
axis = propagate.focus(E * np.exp(1j * phase.lens(X, Y, LAM, f_slm)), X, Y, np.zeros(1), np.zeros(1), LAM, F, z)
|
|
44
|
+
assert abs(z[np.argmax(np.abs(axis[:, 0, 0]))] + F**2 / f_slm) <= z[1] - z[0]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@pytest.mark.parametrize(("z", "slm_distance"), [(0.0, None), (0.0, 0.12), (1.5 * ZR, 0.12)])
|
|
48
|
+
def test_fft_matches_mft(E, z, slm_distance):
|
|
49
|
+
G, xf, yf = propagate.focus_fft(E, PITCH, LAM, F, z, slm_distance)
|
|
50
|
+
sx, sy = slice(len(xf) // 2 - 30, len(xf) // 2 + 31), slice(len(yf) // 2 - 20, len(yf) // 2 + 21)
|
|
51
|
+
M = propagate.focus(E, X, Y, xf[sx], yf[sy], LAM, F, z, slm_distance)
|
|
52
|
+
assert np.allclose(G[sy, sx], M, rtol=0, atol=1e-10 * np.abs(M).max())
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_fft_odd_shape():
|
|
56
|
+
Xo, Yo = fs.grid(301, PITCH), fs.grid(255, PITCH)
|
|
57
|
+
Eo = beam.gaussian(Xo, Yo, 0.2e-3, center=(30e-6, -20e-6)) * np.exp(1j * phase.vortex(Xo, Yo, 1))
|
|
58
|
+
G, xf, yf = propagate.focus_fft(Eo, PITCH, LAM, F, 0.0, 0.12, pad=1.5)
|
|
59
|
+
assert G.shape[0] % 2 == 0 and G.shape[1] % 2 == 0
|
|
60
|
+
assert np.allclose(G, propagate.focus(Eo, Xo, Yo, xf, yf, LAM, F, 0.0, 0.12), rtol=0, atol=1e-9 * np.abs(G).max())
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_fft_conserves_power(E):
|
|
64
|
+
G, xf, yf = propagate.focus_fft(E, PITCH, LAM, F, 0.3 * ZR, 0.12)
|
|
65
|
+
p_slm = beam.intensity(E).sum() * PITCH**2
|
|
66
|
+
assert np.isclose(beam.intensity(G).sum() * (xf[1] - xf[0]) * (yf[1] - yf[0]), p_slm, rtol=1e-12)
|
|
67
|
+
assert np.isclose(p_slm, P, rtol=1e-6)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_focus_matches_free_space_and_lens():
|
|
71
|
+
"""SLM at L ≠ f, plane z ≠ 0: Collins integral against angular spectrum, thin lens, angular spectrum."""
|
|
72
|
+
lam, f, L, n = 1e-6, 0.05, 0.03, 2048
|
|
73
|
+
Xs = fs.grid(n, PITCH)
|
|
74
|
+
w0 = lam * f / (np.pi * 0.25e-3)
|
|
75
|
+
E = beam.gaussian(Xs, Xs, 0.25e-3) * np.exp(1j * phase.vortex(Xs, Xs, 1))
|
|
76
|
+
z = 1.5 * np.pi * w0**2 / lam
|
|
77
|
+
G = propagate.free(E, PITCH, lam, L) * np.exp(1j * phase.lens(Xs, Xs, lam, f))
|
|
78
|
+
G = propagate.free(G, PITCH, lam, f + z)
|
|
79
|
+
s = slice(n // 2 - 40, n // 2 + 41)
|
|
80
|
+
M = propagate.focus(E, Xs, Xs, Xs[s], Xs[s], lam, f, z, slm_distance=L)
|
|
81
|
+
assert np.abs(G[s, s] - M).max() < 1e-3 * np.abs(M).max()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_free_is_unitary_and_reversible(E):
|
|
85
|
+
G = propagate.free(E, PITCH, LAM, 0.05, bandlimit=False)
|
|
86
|
+
assert np.isclose(np.sum(np.abs(G) ** 2), np.sum(np.abs(E) ** 2), rtol=1e-12)
|
|
87
|
+
assert np.allclose(propagate.free(G, PITCH, LAM, -0.05, bandlimit=False), E, rtol=0, atol=1e-12 * np.abs(E).max())
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_free_scan_and_single_planes_agree(E):
|
|
91
|
+
z = np.array([-0.02, 0.0, 0.05])
|
|
92
|
+
scan = propagate.free(E, PITCH, LAM, z)
|
|
93
|
+
for i, zi in enumerate(z):
|
|
94
|
+
assert np.array_equal(scan[i], propagate.free(E, PITCH, LAM, zi))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_free_scan_continues_focus(E):
|
|
98
|
+
"""A z scan by free from the focal plane, against the exact focus in each plane."""
|
|
99
|
+
u = fs.grid(256, 12e-6)
|
|
100
|
+
z = np.linspace(-2, 2, 5) * ZR
|
|
101
|
+
scan = propagate.free(propagate.focus(E, X, Y, u, u, LAM, F, slm_distance=0.12), 12e-6, LAM, z)
|
|
102
|
+
exact = propagate.focus(E, X, Y, u, u, LAM, F, z, slm_distance=0.12)
|
|
103
|
+
assert np.abs(scan - exact).max() < 1e-6 * np.abs(exact).max()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_free_zero_distance(E):
|
|
107
|
+
assert np.allclose(propagate.free(E, PITCH, LAM, 0.0), E, rtol=0, atol=1e-12 * np.abs(E).max())
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_complex64(E):
|
|
111
|
+
E32 = E.astype(np.complex64)
|
|
112
|
+
M = propagate.focus(E32, X, Y, x, x, LAM, F, slm_distance=0.12)
|
|
113
|
+
assert M.dtype == np.complex64
|
|
114
|
+
ref = propagate.focus(E, X, Y, x, x, LAM, F, slm_distance=0.12)
|
|
115
|
+
assert np.abs(M - ref).max() < 1e-5 * np.abs(ref).max()
|
|
116
|
+
assert propagate.focus_fft(E32, PITCH, LAM, F)[0].dtype == np.complex64
|
|
117
|
+
assert propagate.free(E32, PITCH, LAM, 0.01).dtype == np.complex64
|