Psychtoolbox An introduction Sheraz Khan Ph D TRANSCEND
Psychtoolbox: An introduction Sheraz Khan, Ph. D TRANSCEND Research, TAL Kenet Group Fellow, MGH 1
OUTLINE • Introduction to Psychtoolbox. • Psychtoolbox by Simple Examples • Psychtoolbox in practice at Martinos center. 2
Introduction to Psychtoolbox • Psych. Toolbox 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). " • Psychtoolbox is widely used in visual psychophysics and there is a lot of documentation and knowledge around. http: //psychtoolbox. org • MAC and Windows version available, MAC version has better control over hardware. 3
Introduction to Psych. Toolbox • Installation >> cd ~/Desktop >> Download. Psychtoolbox('current') • To get help >>Screen Open. Window? >>Screen Draw. Text? • If screen freezes ctrl-c % will stop execution clear Screen % garbage collector of Screen Cmd + option +escape % Ultimate 4
Why use Psych. Toolbox ? • Psych. Toolbox is very strict about timing, so if accurate timing is important in your experiment Psych. Toolbox is good for that. • Because Psych. Toolbox using graphics acceleration you can use very fast drawing without any pixelation and other image processing artefacts • Lots of people in the world use it so it's easy to get help, and more importantly its FREE. 5
Programming simple Stimuli % Testing Psych. Toolbox >> Screen. Test ***** Screen. Test: Testing Screen 0 ***** PTB-INFO: This is the Open. GL-Psychtoolbox version 3. 0. 8. Type 'Psychtoolbox. Version' 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: Open. GL-Renderer is NVIDIA Corporation : : Ge. Force Go 7400/PCI/SSE 2 : : 2. 0. 1 PTB-Info: VBL startline = 768 , VBL Endline = -1 TB-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. . . ***** Screen. Test: Done With Screen 0 ***** 6
Programming simple Stimuli % Initializing the program >>screen. Num=1; % Screen 0 is Laptop, Screen 1 will be projector >>flip. Spd=13; % a flip every 13 frames % Opening Window on Screen, we want to used >>[w. Ptr, rect]=Screen('Open. Window', screen. Num); % w. Ptr pointer to the screen. % Getting Screen refresh rate >>monitor. Flip. Interval=Screen('Get. Flip. Interval', w. Ptr); % 1/ monitor. Flip. Interval is the frame rate of the projector 7
Programming Simple Stimuli % Finding Black and white >>black=Black. Index(w. Ptr); >>white=White. Index(w. Ptr); % Filling the screen with Black and wait >>Screen('Fill. Rect', w. Ptr, black); >>Screen(w. Ptr, 'Flip'); >>Wait. Secs(2); 8
Programming Simple Stimuli % make a rectangle in the middle of the screen flip colors and size % Fill the screen with Black >>Screen('Fill. Rect', w. Ptr, black); >>vbl=Screen(w. Ptr, 'Flip'); % collect the time for the first flip with vbl % Make a rectangle in the middle of the screen flip colors and size for i=1: 10 Screen('Fill. Rect', w. Ptr, [0 0 255], [100 150 200 250]); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); % flip 13 frames after vbl Screen('Fill. Rect', w. Ptr, [255 0 0], [100 150 400 450]); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); End % Color; Left, Right, Top, Bottom 9
Programming Simple Stimuli % Fill the screen with Black >>Screen('Fill. Rect', w. Ptr, black); >>vbl=Screen(w. Ptr, 'Flip'); % Make circles flip colors & size for i=1: 10 Screen('Fill. Oval', w. Ptr, [0 180 255], [ 500 600]); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); Screen('Fill. Oval', w. Ptr, [0 255 0], [ 400 900 700]); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); end % Color; Starting Horizontal, Starting Vertical; Ending Horizontal, Ending Vertical 10
Programming Simple Stimuli % Fill the screen with Black >>Screen('Fill. Rect', w. Ptr, black); >>vbl=Screen(w. Ptr, 'Flip'); % make lines that flip colors size & position >>for i=1: 10 Screen('Draw. Line', w. Ptr, [0 255], 500, 200, 700 , 600, 5); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); Screen('Draw. Line', w. Ptr, [255 0], 100, 600 , 100, 5); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); >>end % Color; Starting Horizontal, Starting Vertical; Ending Horizontal, Ending Vertical 11
Programming Simple Stimuli % Fill the screen with Black >>Screen('Fill. Rect', w. Ptr, black); >>vbl=Screen(w. Ptr, 'Flip'); % Texture is the great way of storing Images >>image 2 D=255*rand(100, 100); >>texture. Index=Screen('Make. Texture', w. Ptr, image 2 D); >>Screen('Draw. Texture', w. Ptr, texture. Index); >>Screen(w. Ptr, 'Flip'); >>Wait. Secs(2); 12
Programming Simple Stimuli % Fill the screen with Black >>Screen('Fill. Rect', w. Ptr, black); >>vbl=Screen(w. Ptr, 'Flip'); % Initialize the Keyboard input >>Kb. Name('Unify. Key. Names'); % Unify Max OS X, OS 9 >>[sec key. Code]=Kb. Press. Wait; % wait for the key press >>key. Code=find(key. Code~=0); % Drawing Test >>Screen('Text. Size', w. Ptr , 150); % font size >>Screen('Draw. Text', w. Ptr, num 2 str(key. Code), 200, 20, [255 50 255]); >>vbl=Screen(w. Ptr, 'Flip'); % Starting Horizontal vertical position >>Wait. Secs(2); 13
Programming Simple Stimuli % Fill the screen with Black >>Screen('Fill. Rect', w. Ptr, black); >>vbl=Screen(w. Ptr, 'Flip'); % Combining the stimuli >>for i=1: 10 Screen('Fill. Rect', w. Ptr, [0 0 255], [100 150 200 250]); Screen('Draw. Line', w. Ptr, [0 255], 500, 200, 700 , 600, 5); Screen('Fill. Oval', w. Ptr, [0 180 255], [ 500 600]); Screen('Text. Size', w. Ptr , 150); Screen('Draw. Text', w. Ptr, 'FUNKY!!', 200, 20, [255 50 255]); vbl=Screen(w. Ptr, 'Flip', vbl+(flip. Spd*monitor. Flip. Interval)); >>end 14
Programming Real Stimuli Local Connectivity in the visual cortex: From Polat and Norcia (Polat and Norcia, 1996), who discovered the paradigm (Polat and Sagi, 1993), and were the first to investigate it using VEPs. They showed that responses to b and d deviated from the linear sum of responses to (a+c) and (e+c) respectively, the difference being attributable to local connectivity. 15
Programming Real Stimuli Specifications: Size of the objects: Size of the Gabors, visual separation between them Size of the Screen Distance from the Screen L, 55 cm D, 120 cm H, 41 cm 16
Programming Real Stimuli • Normally in visual stimuli things are described in visual angle, so what it is ? Size is 1 degree, separation is 3 degrees Width=tand(1)*D, Width in screen percent=(Size/L)*100 Separation=tand(3)*D, Separation in screen percent=(Separation)/H*100 17
Programming Real Stimuli % Setting Parameters >> n. Trials = 40; >>soa = 1. 5; % Stimulus-Onset Asynchrony (SOA) >>display. Time = 0. 3; jitter = 0. 1; >>Width. Of. Grid=4. 88; Separation=20. 25; >>grating. Matrix=Gabors; % Location of three gabors >>L 1=[50 50]; L 2=[50 50 -Separation]; L 3=[50 50+Separation]; % Configuring triggers >>di = Daq. Device. Index; >>Daq. DConfig. Port(di, 0, 0); >>Daq. DOut(di, 0, 0); 18
Programming Real Stimuli % Setting Screen >>which. Screen = max(Screen('Screens')); >>rect=Screen(which. Screen, 'Rect'); % Gets Screen dimensions >>Hide. Cursor; >>window = Screen('Open. Window', which. Screen); % Color Setup % Retrieves color codes for black and white and gray. >>black = Black. Index(window); >>white = White. Index(window); >>gray = (black + white) / 2; % Computes the color code for gray. 19
Programming Real Stimuli % Giving Instruction to Subject Screen('Fill. Rect', window, gray); Screen(window, 'Text. Size', txt. Sz); Screen(window, 'Draw. Text', 'Remember to keep your eyes', rect(3)/4, rect(4)/2 -50); Screen(window, 'Draw. Text', 'on the + at the center', rect(3)/3. 3, rect(4)/2); Screen('Flip', window); % Click to proceed Get. Click; % Putting Cross in the center Screen('Fill. Rect', window, gray); Screen(window, 'Text. Font', center. Font); Screen(window, 'Text. Size', center. Txt. Sz); Screen(window, 'Draw. Text', center. Char, rect(3)/2, rect(4)/2, center. Colour); Screen('Flip', window); 20
Programming Real Stimuli [source. Rect target. Rect]=rect. Generation(location, Width. Of. Grid, rect) % Decoding percent to actual screen Width. Of. Grid=round(max(rect)*Width. Of. Grid/100); half. Width. Of. Tgt=Width. Of. Grid/2; % source. Rect: : image area source. Rect = [1 1 width. Of. Grid]; % target. Rect : : Actual Screen place and size locationl = round(rect(3)*location(1)/100 - half. Width. Of. Tgt); locationt = round(rect(4)*location(2)/100 - half. Width. Of. Tgt); locationr = round(rect(3)*location(1)/100 + half. Width. Of. Tgt); locationb = round(rect(4)*location(2)/100 + half. Width. Of. Tgt); target. Rect = [locationl locationt locationr locationb]; 21
Programming Real Stimuli % Putting Three gabors on the Screen [source. Rect target. Rect]=rect. Generation(L 1, Width. Of. Grid, rect); tex = Screen('Make. Texture', window, ceil((gray+grating. Matrix))); Screen('Draw. Textures', window, tex, source. Rect, target. Rect); [source. Rect target. Rect]=rect. Generation(L 2, Width. Of. Grid, rect); texu = Screen('Make. Texture', window, ceil((gray+grating. Matrix))); Screen('Draw. Textures', window, texu, source. Rect, target. Rect, 90); [source. Rect target. Rect]=rect. Generation(L 3, Width. Of. Grid, rect); texd = Screen('Make. Texture', window, ceil((gray+grating. Matrix))); Screen('Draw. Textures', window, texd, source. Rect, target. Rect, 90); Screen('Flip', window); 22
Programming Real Stimuli % Sending triggers Daq. DOut(di, 0, 4); %send trigger Daq. DOut(di, 0, 0); %clear trig 23
Conclusion • All the source code for this talk is available on why n how wiki. • Best way to learn Pschytoolbox is to use it on the projector. • Always test the timing of your protocol. • Photo diode and oscilloscope is available in MEG. 24
Thank you. 25
- Slides: 25