Skip to content

Commit

Permalink
Add set_phases_8bit for simulated SLM
Browse files Browse the repository at this point in the history
  • Loading branch information
dedean16 committed Nov 8, 2024
1 parent 2696ebe commit 1b136f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
7 changes: 7 additions & 0 deletions openwfs/simulation/slm.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ def set_phases(self, values: ArrayLike, update=True):
if update:
self.update()

def set_phases_8bit(self, values: ArrayLike, **kwargs):
"""
Pass 8bit unsigned integers [0, 255] as linearly mapped to phase, such that 0 -> 0, 256 -> 2π. Any other
keyword arguments will be passed to set_phases.
"""
self.set_phases(values * 2 * np.pi / 256, **kwargs)

@property
def pixels(self) -> Detector:
"""Returns an object to monitor the current state of the SLM.
Expand Down
33 changes: 7 additions & 26 deletions tests/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,36 +206,17 @@ def test_mock_slm_lut_and_phase_response():
"""
# === Test default lookup table and phase response ===
# Includes edge cases like rounding/wrapping: -0.501 -> 255, -0.499 -> 0
input_phases_a = (
np.asarray(
(
-1,
-0.501,
-0.499,
0,
1,
64,
128,
192,
255,
255.499,
255.501,
256,
257,
511,
512,
)
)
* 2
* np.pi
/ 256
)
input_phase_a_256 = np.asarray([-1, -0.501, -0.499, 0, 1, 64, 128, 192, 255, 255.499, 255.501, 256, 257, 511, 512])
input_phases_a = input_phase_a_256 * 2 * np.pi / 256
expected_output_phases_a = (
np.asarray((255, 255, 0, 0, 1, 64, 128, 192, 255, 255, 0, 0, 1, 255, 0)) * 2 * np.pi / 256
np.asarray([255, 255, 0, 0, 1, 64, 128, 192, 255, 255, 0, 0, 1, 255, 0]) * 2 * np.pi / 256
)
slm1 = SLM(shape=(3, input_phases_a.shape[0]))
slm1.set_phases(input_phases_a)
assert np.all(np.abs(slm1.phases.read() - expected_output_phases_a) < 1e6)
assert np.all(np.abs(slm1.phases.read() - expected_output_phases_a) < 1e-6)

slm1.set_phases_8bit(input_phase_a_256)
assert np.all(np.abs(slm1.phases.read() - expected_output_phases_a) < 1e-6)

# === Test setting custom phase response of SLM ===
# Construct custom phase response
Expand Down

0 comments on commit 1b136f3

Please sign in to comment.