Matlab Class Chapter 6.pdf
(
68 KB
)
Pobierz
Chapter 6 - 1 - 4/18/2007
Chapter 6 – Psychtoolbox
We don’t want to run this experiment on the Matlab figure window, since as you can see
it is slow and clunky. To do this we are going to need to start using a group of Matlab
programs developed especially for doing behavioral experiments. Many groups have
developed similar sets of programs, but the ones we are going to use are called
PsychToolbox.
You need to download Psychtoolbox from here:
http://psychtoolbox.org/
PsychToolbox is a collection of matlab functions written to make presenting visual
stimuli easier. Remember to cite the Toolbox.
"We wrote our experiments in MATLAB, using the Psychophysics Toolbox extensions
(Brainard, 1997; Pelli, 1997)."
Brainard, D. H. (1997) The Psychophysics Toolbox, Spatial Vision , 10:443-446.
Pelli, D. G. (1997) The VideoToolbox software for visual psychophysics: Transforming
numbers into movies, Spatial Vision 10:437-442.
You’ll have to download Psychtoolbox from the web site. Make sure you download the
right version (for MacOSX or PC). Once you have installed Matlab, then you can carry
on with the chapter.
Getting started with PsychToolbox Screen.m
1. Start with just 1 monitor
2. Try:
>ScreenTest
If you get the screen going blank and then something like the following then
screenTest
worked
***** ScreenTest: Testing Screen 0 *****
PTB-INFO: This is the OpenGL-Psychtoolbox version 3.0.8. Type
'PsychtoolboxVersion' for more detailed version information.
PTB-INFO: Psychtoolbox is licensed to you under terms of the GNU
General Public License (GPL). See file 'License.txt' in the
PTB-INFO: Psychtoolbox root folder for a copy of the GPL license.
PTB-INFO: OpenGL-Renderer is NVIDIA Corporation :: GeForce Go
7400/PCI/SSE2 :: 2.0.1
PTB-Info: VBL startline = 768 , VBL Endline = -1
Chapter 6 - 2 - 4/18/2007
PTB-Info: Measured monitor refresh interval from VBLsync =
16.712593 ms [59.835118 Hz]. (50 valid samples taken,
stddev=0.044112 ms.)
PTB-Info: Reported monitor refresh interval from operating system
= 16.666667 ms [60.000000 Hz].
PTB-Info: Small deviations between reported values are normal and
no reason to worry.
PTB-INFO: Using NVidia's GL_TEXTURE_RECTANGLE_NV extension for
efficient high-performance texture mapping...
***** ScreenTest: Done With Screen 0 *****
If not, then Psychtoolbox isn't working right on your computer
Writing code using Screen.m
The next stage is also very simple. We are simply going to open an experimental window, making it black,
making it white, and then closing it again.
The script below uses a command called
Screen
, which one of the core functions of Psychtoolbox.
Screen is actually not an m file (like the ones you have been writing. It is a mex file. This means that it is
written in C (or C++) or some other programming language, and is then compiled to run in Matlab. The
reason this was done is because Screen does some pretty funky stuff that would be impossible in Matlab.
Oddly, under certain circumstances the
Screen
command likes to have a capital letter (it’s case sensitive
unlike most commands in Matlab).
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
% DarkScreen.m
%
% opens a window using Psychtoolbox,
% makes the window black, then white, and then closes
% the window again
%
% written for Psychtoolbox 3 on the PC by IF 3/2007
screenNum=0;
res=[1280 1024];
clrdepth=32;
[wPtr,rect]=Screen(
'OpenWindow'
,screenNum,0,
…
[0 0 res(1) res(2)], clrdepth);
black=BlackIndex(wPtr);
white=WhiteIndex(wPtr);
Screen(
'FillRect'
,wPtr,black);
Screen(wPtr,
'Flip'
);
HideCursor;
tic
while
toc<3
;
end
Screen(
'FillRect'
,wPtr,white);
Screen(wPtr,
'Flip'
);
Chapter 6 - 3 - 4/18/2007
27
28
29
30
31
32
33
34
35
HideCursor;
tic
while
toc<3
;
end
Screen(
'CloseAll'
);
ShowCursor
One weird thing about Screen is that you don’t get help about it’s commands in the
normal way.
If you type:
>help Screen
You don’t get a lot of info.
If you try
>Screen
You will get a list of all the subcommands that are contained within Screen. To get more information about a
particular command you do:
>Screen OpenWindow?
>Screen DrawText?
You should also bear in mind that you are entering the murky world of non-professional code. This
code is written by people like you, in the middle of trying to do real science. This means that
commands may not work as stated, help files may be out of date, commands may not even exist.
This is especially true in the case of Psychtoolbox, which is essentially a collaborative effort
between lots of people and has been through lots of reincarnations.
Line 9.
If you are running more than one monitor on your computer then Screen tells you which one to use.
0 means the monitor with the menu bar, 1 means the other monitor. For now, if you have any difficulty I
would make sure you are only using one monitor. If you are cloning your monitor then Screen 1 becomes
the display screen.
Line 10-11.
Check the resolution of your screen and the using Start, Control Panel, Display and write down
what the resolution of your monitor is in terms of the number of pixels and the color depth of the monitor. As
far as the resolution of the monitor in pixels is concerned the first number is the width of the screen, the
second number is the height of the screen. The resolution of your screen in terms of color depth will be 8, 16
or 32 bits, depending on the age of your monitor or computer. Set it to the highest setting the computer will
allow.
Line 12.
This calls a function called Screen, which takes 5 arguments.
Argument 1
. a command telling Screen what to do – in this case you want Screen to open a window.
Argument 2.
which monitor you want the window opened inside. In this case you want the Screen opened in
monitor 0 – the one with the menu bar.
Argument 3.
The color you want to fill the window with. This currently doesn’t seem to work in Psychtoolbox.
Argument 4
. This tells Screen how big you want the window to be – in this case you want it to be a rectangle
the size of the entire screen. The order in which you describe this rectangle can be remembered as
L
ox (Left, Top, Right, Bottom). We want the rectangle to start 0 pixels from the Left, and 0 pixels
from the Top, and go to 1280 pixels towards the Right and 1024 pixels towards the Bottom.
(Why does it start with 0 instead of 1? Why because Screen is a mex file and was written in C which is an 0-
based language.) Currently Psychtoolbox always uses the whole screen, no matter what you enter into rect.
Argument 5
is the color depth of the monitor.
e
T
te
RB
Arguments 3-5 don’t actually need to be specified – Matlab will default to certain values: a random
background color, the whole screen and the color depth of the monitor as described in the control panel
Lines 13-14
Find the colormap values that will give you black and white.
Line 15
Draw a black rectangle the size of the screen. Psychtoolbox automatically assumes that you have
an offscreen window and an onscreen window. Every time you use a drawing command like DrawRect it will
automatically draw on the offscreen window. So the rectangle won’t yet be visible. The advantage of this
Chapter 6 - 4 - 4/18/2007
approach (as you will see later) is that you can spend some time drawing several things offscreen without
them showing up one by one on the screen. Once you have finished drawing you can move the offscreen
window to the front.
Line 16
You need to
Flip
the screen so the offscreen window you drew the black rectangle on comes to
the onscreen window – the one that is actually on the monitor.
Lines 18-22
Hide the cursor and wait 3 seconds
Lines 24-30
Draw a white rectangle on the offscreen window, flip it to the front and wait 3 seconds
Lines 34-35.
Close the screens and re-appear the cursor
****************************************************************************
NOTE FOR IF YOUR SCREEN FREEZES
If you display stimuli on the main screen, as we often do, then the Screen window will
hide the main menu bar and obscure Matlab’s command window. That can be a problem
if your program stops (perhaps due to an error) before closing the window. The keyboard
will seem to be dead because its output is directed to the front most window, which
belongs to Screen not Matlab, so Matlab won’t be aware of your typing.
Remain calm.
Typing Ctrl-C will stop your program if hasn't stopped already. Typing:
command-zero (on the Mac)
Alt-Tab (on Windows)
Will bring Matlab’s command window forward. That will restore keyboard input. The
screen might still be hard to make out, if you’ve been playing with the lookup table.
Typing:
clear Screen
will then cause Matlab to flush Screen.mex. Screen.mex, as part of its exit procedure,
cleans up everything it did, closing all its windows and restoring the lookup table of all
its displays. And everything will be hunky dory again.
****************************************************************************
Here’s another more elaborate example of how you can use
Screen
1
2
3
4
5
6
7
8
9
10
11
12
13
% FunkyScreen.m
%
% opens a window using psychtoolbox,
% makes the window do some funky things
%
% written for Psychtoolbox 3 on the PC by IF 3/2007
screenNum=0;
flipSpd=13;
% a flip every 13 frames
[wPtr,rect]=Screen(
'OpenWindow'
,screenNum);
monitorFlipInterval=Screen(
'GetFlipInterval'
, wPtr);
Chapter 6 - 5 - 4/18/2007
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
% 1/monitorFlipInterval is the frame rate of the monitor
black=BlackIndex(wPtr);
white=WhiteIndex(wPtr);
% blank the Screen and wait a second
Screen(
'FillRect'
,wPtr,black);
Screen(wPtr,
'Flip'
);
HideCursor;
tic
while
toc<1
;
end
% make a rectangle in the middle of the screen flip colors and size
Screen(
'FillRect'
,wPtr,black);
vbl=Screen(wPtr,
'Flip'
);
% collect the time for the first flip with vbl
for
i=1:10
Screen(
'FillRect'
,wPtr,[0 0 255], [100 150 200 250]);
vbl=Screen(wPtr,
'Flip'
, vbl+(flipSpd*monitorFlipInterval));
% flip 13 frames after vbl
Screen(
'FillRect'
,wPtr,[255 0 0], [100 150 400 450]);
vbl=Screen(wPtr,
'Flip'
, vbl+(flipSpd*monitorFlipInterval));
end
% blank the screen and wait a second
Screen(
'FillRect'
,wPtr,black);
vbl=Screen(wPtr,
'Flip'
, vbl+(flipSpd*monitorFlipInterval));
tic
while
toc<1
;
end
% make circles flip colors & size
Screen(
'FillRect'
,wPtr,black);
vbl=Screen(wPtr,
'Flip'
);
for
i=1:10
Screen(
'FillOval'
,wPtr,[0 180 255], [ 500 500 600 600]);
vbl=Screen(wPtr,
'Flip'
, vbl+(flipSpd*monitorFlipInterval));
Screen(
'FillOval'
,wPtr,[0 255 0], [ 400 400 900 700]);
vbl=Screen(wPtr,
'Flip'
, vbl+(flipSpd*monitorFlipInterval));
end
% blank the Screen and wait a second
Screen(
'FillRect'
,wPtr,black);
vbl=Screen(wPtr,
'Flip'
, vbl+(flipSpd*monitorFlipInterval));
tic
while
toc<1
;
end
% make lines that flip colors size & position
Screen(
'FillRect'
,wPtr,black);
vbl=Screen(wPtr,
'Flip'
);
for
i=1:10
Plik z chomika:
miery
Inne pliki z tego folderu:
male.zip
(67 KB)
Matlab Class Chapter 1.pdf
(44 KB)
Matlab Class Chapter 2.pdf
(49 KB)
Matlab Class Chapter 3.pdf
(66 KB)
Matlab Class Chapter 4.pdf
(38 KB)
Inne foldery tego chomika:
MatLab
XML4MAT
Zgłoś jeśli
naruszono regulamin