[tutorial] How to control an SLM with Matlab/Octave using Psychtoolbox

Most spatial light modulator (SLMs) available are controllable like a normal computer monitor and are plugged on a computer with a DVI cable.  Some SLMs are now sold with a dedicated card or can be controlled via USB. If you possess such a device, this tutorial is not for you. The first requirement to control the SLM with a DVI/HDMI cable is to have a graphic card with two monitor outputs, one for your screen, one for your SLM. Once plugged to the computer, the SLM is then handled by  the operating system as a secondary monitor. No software is required to display an image on the SLM. For that reason, the constructor does not provide any code to use the SLM with Matlab/Octave or other software. One solution to send images with matlab is to display an array in a figure that fits the size of the secondary monitor. Nevertheless, this technique presents some drawbacks due to the fact that you do not control directly the pixels of the SLM. For instance, the border of the figure, that may be different depending on the operating system, has to be taken into account. More importantly, the scaling of the figure does not guarantee that one pixel of the image displayed correspond to one pixel of the SLM. For application where a very good resolution is needed, a blured image on the SLM can be detrimental.

 

I present how to control directly the pixels of the SLM using Psychtoolbox, a free toolbox for Matlab and Octave that uses GPU acceleration. I show here a tutorial for Matlab, but the toolbox also exists for Octave and seems to work a similar way.

Requirements

  • A graphic card with two monitor outputs
  • Matlab 7.x or newer
  • A SLM with a DVI cable

Download and install Psychtoolbox

The first step is to download and install the toolbox. Full instructions are to be found on the official website of Psychtoolbox:

PsychToolBox download and installation

Control the SLM

We first need to initialize the toolbox.

window = Screen('OpenWindow',2);

This will create a full screen window on monitor '2' (the SLM).

You can now retrieve the minimum and maximum pixel values - corresponding to black and white on a normal display.

white = WhiteIndex(window);

black = BlackIndex(window); 

These values should be 0 and 255 for a 8 bits display.

There are different ways to display an image on the SLM. To draw a rectangle of a given value, one can use the function :

Screen(window, 'FillRect',value,[x1,x2,y1,y2])

[x1,x2,y1,y2] being the coordinates of the rectangle. The image is drawn off-screen so far, so nothing is displayed and it can me modify again before being sent on the screen. When finished, display the image on the SLM using the function:

Screen(window,'Flip');

The following code shows how to display an image on the entire SLM of resolution resX by resY.

[x,y] = meshgrid(0:resX-1, 0:resY-1);

image =  sin(0.01*2*pi*x);

Screen(window, 'PutImage',image);

Screen(window,'Flip'); 

This technique can be slow and decrease the frame rate at which images are displayed. If it is possible to generate the images before sending them on the SLM, it is preferable to load them before in textures.

for i = 1:30

    image =  sin(0.01*2*pi*(x+i/30*100));

    texture(i) = Screen(window, 'MakeTexture', image);

end

An then send them on the SLM:

for i = 1:30

    Screen('DrawTexture', window, texture(i));

    Screen(window,'Flip');

end 

 Finally, close the window using:

Screen('CloseAll');

 

 

 

Submit to FacebookSubmit to Google BookmarksSubmit to TwitterSubmit to LinkedIn

Comments   

 
#8 Daniel 2017-04-19 03:09
Quoting Martin M:
Bonjour,
Merci pour ce tuto, malheureusement il ne marche pas pour moi...
Je viens d'installer "Subversion 1.7.x command-line client" et d'enregistrer "Psychtoolbox installer " dans C:\Drivers\toolbox
Je lance matlab en administrateur, puis ">> cd C:\Drivers\toolbox
DownloadPsychtoolbox('C:\Drivers\toolbox')"

et j'obtiens un message d'erreur


"'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes.
Sorry, the download command "CHECKOUT" failed with error code 1:
'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes."

je suis perdu...
pouvez vous m'aider

bien à vous
Martin



You can download the Zip file from Github directly. Note that the latest version is 3.0.13 and beta by now. It is much simple rather than the Download order.
Quote
 
 
#7 Sébastien Popoff 2016-05-05 11:36
Hi,

I never used Cogent Graphics, I did not know about it to be honest, so I cannot compare.

Best,

Sebastien
Quote
 
 
#6 Ivan Gusachenko 2016-05-05 09:44
Thank you for this! By any chance, have you tried Cogent Graphics library for Matlab before? I think it's quite a similar thing, but maybe not maintained anymore. I'm using Cogent now, but I suspect it causes some instabilities in Matlab. I wonder if it's worth switching to Psychtoolbox, and if you have experience working with both.
Thank you
Quote
 
 
#5 Sébastien Popoff 2016-03-30 12:48
Sorry I did not reply in time on your issue about installing Psychtoolbox, but I do not think I would have been of any help.
I switched to Python now and the install procedure has change since last time I used Psychtoolbox.

Best,
Quote
 
 
#4 martin m 2016-03-29 07:51
tout est bon!!!
Quote
 
 
#3 Martin M 2016-03-24 17:09
Bonjour,
Merci pour ce tuto, malheureusement il ne marche pas pour moi...
Je viens d'installer "Subversion 1.7.x command-line client" et d'enregistrer "Psychtoolbox installer " dans C:\Drivers\toolbox
Je lance matlab en administrateur, puis ">> cd C:\Drivers\toolbox
DownloadPsychtoolbox('C:\Drivers\toolbox')"

et j'obtiens un message d'erreur


"'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes.
Sorry, the download command "CHECKOUT" failed with error code 1:
'svn' n'est pas reconnu en tant que commande interne
ou externe, un programme ex‚cutable ou un fichier de commandes."

je suis perdu...
pouvez vous m'aider

bien à vous
Martin
Quote
 
 
#2 Sébastien Popoff 2015-04-27 12:59
Thanks for the info, the link has been updated.
Quote
 
 
#1 Andy 2015-04-20 18:38
The links to Psychtoolbox are outdated. Here is an updated link to their homepage

http://psychtoolbox.org/
Quote
 

Additional information