Create a phase pattern to control the beam location

In this live script we create a phase pattern to control the location of the beam using a linear grating and a spherical lens. This example includes sliders to control the generated phase pattern and the corresponding position of the beam.

Setup some parameters for the simulation

% First step is usually to add the toolbox to the path
addpath('../../');
% Then we define the SLM (pattern) size and centre
sz = [512, 512];
centre = sz ./ 2;

Setup the incident beam

type = 'gaussian';
switch type
case 'gaussian'
beam_width = 101;
incident = otslm.simple.gaussian(sz, beam_width);
case 'plane'
incident = ones(sz);
otherwise
error('Unknown beam type')
end
imagesc(incident); axis image; title('Incident');

Generate the phase function to control the beam

First lets setup some sliders to control the patterns
x_inv_spacing =50;
y_inv_spacing =-30;
z_offset = -30;
lin = otslm.simple.linear(sz, sz./[x_inv_spacing, y_inv_spacing], ...
'centre', centre);
sph = otslm.simple.spherical(sz, min(sz), ...
'scale', z_offset, 'centre', centre);
pattern = lin + sph;
pattern = otslm.tools.finalize(pattern);
imagesc(pattern); axis image; title('Phase');

Visualise the far-field

farfield = abs(otslm.tools.visualise(pattern, 'incident', incident));
imagesc(farfield); axis image; title('Far-field');

Setup a ScreenDevice

Setting up a screen device in a live script takes a bit more work than usual. Live scripts have their own non-visible figure object for plotting. When we show the ScreenDevice window, a new visible figure object is created. After we show the ScreenDevice window for the first time we need to tell the live script to use the old figure for internal plots.
To ensure this code is only run on the time, we wrap it in a if-exists block. To re-run the ScreenDevice initialisation, simply clear the sd variable.
if 0 == exist('sd', 'var') || ~ishandle(sd.figure_handle)
sd = otslm.utils.ScreenDevice(1, 'target_size', sz,...
'pattern_type', 'phase', 'prescaledPatterns', true);
% Get the figure handle for the livescript
% We need to do this to make ScreenDevice run correctly
figureHandle = gcf();
% Show the ScreenDevice figure
sd.show();
% Change back to the liveScript figure handle
% This also needs to be done if we click on another figure
set(0, 'CurrentFigure', figureHandle);
end
sd.show(pattern);

About

This example live script is part of OTSLM. Written by Isaac Lenton. For further details see https://github.com/ilent2/otslm