Transcript Matlab Class 2 - Rutgers University
Matlab Class 5
Xiaotao Su, Ph.D.
Visual Psychophysicist & IT Group Leader Rutgers Center for Cognitive Science (RuCCS)
RuCCS Psychtoolbox Basics • Computers are perfect for creating and displaying visual psychophysics stimuli • Programs usually written in a low-level language (e.g. C or Pascal) to achieve full control of the hardware for precise stimulus display but are difficult to program • Interpreted languages like Matlab are abstracted from hardware details and provide friendlier development environments • The Psychophysics Toolbox is a software package that allows Matlab to fully control the hardware for precise stimulus display while retaining the flexibility and ease of Matlab.
RuCCS Screen drawing and psychtoolbox Origin
Screen Geometry
Y X ----------- Positive ->
Coordinates are measured in pixels.
Positive Max X and Y
RuCCS How you typically work with the psychtoolbox Back Buffer (invisible)
Front Buffer
RuCCS How you typically work with the psychtoolbox
Step 1: Draw Shape to the back buffer Front Buffer
RuCCS How you typically work with the psychtoolbox
Step 2: Flip the back buffer to the front buffer
RuCCS How you typically work with the psychtoolbox Back Buffer is automatically cleared
RuCCS How you typically work with the psychtoolbox
Now you can continue with your next frame of animation
RuCCS How you typically work with the psychtoolbox
Flip the back buffer to the front buffer
RuCCS How you typically work with the psychtoolbox Back Buffer is automatically cleared
Basic Display Loop in PsychToolbox (PTB) RuCCS % Open Window % while some condition is true do the following % draw something % make some changes % repeat % Close screen
Set up program draw_stuff % draw_stuff.m
% % Draws stuff on the screen RuCCS
Skip Sync Tests • Add to your program before opening the new screen Screen('Preference','SkipSyncTests', 1); RuCCS
Initialize the main window % draw_stuff.m
% % Draws stuff on the screen clear all; which_screen=0; RuCCS
Set up program draw_stuff % draw_stuff.m
% % Draws stuff on the screen clear all; which_screen=0; [window_ptr, screen_dimensions] 0 =Screen(0, 'OpenWindow' , [ 0 , 0 , 0 ]); RuCCS 0 is main screen (allows for multiple monitors) Command issued to the psychtoolbox Screen function Vector specifying Screen color [red , green , blue]
Set up program draw_stuff % draw_stuff.m
% % Draws stuff on the screen clear all; which_screen=0; [window_ptr, screen_dimensions]=Screen(0, 'OpenWindow' , [ 0 , 0 , 0 ]); x1= 0 y1 = 0 Vector [x1, y1, x2, y2] Which could be: [0, 0, 800, 600] For an 800 X 600 display x2= 800 y2 = 600 RuCCS
Draw shape in back buffer % draw_stuff.m
% % Draws stuff on the screen clear all; which_screen=0; [window_ptr, screen_dimensions]=Screen(0,'OpenWindow', [ 0 , 0 , 0 ]); shape_dimensions = screen_dimensions/2; Screen( window_ptr, 'FillRect', [ 0 , 0 , 255 ], shape_dimensions);
X1, Y1
RuCCS Front buffer
X2, Y2
Back buffer
Flip the back buffer to the front buffer
% draw_stuff.m
% % Draws stuff on the screen clear all; which_screen=0; [window_ptr, screen_dimensions]=Screen(0, 'OpenWindow' , [ 0 , 0 , 0 ]); shape_dimensions = screen_dimensions/2; Screen(window_ptr, 'FillRect', [0,0,255], shape_dimensions); %Flip the buffers Screen(window_ptr,'Flip'); Front buffer Back buffer RuCCS
Clear
screen
<<<
Task 1: Making a movie % draw_stuff.m
% % Draws stuff on the screen clear all; which_screen=0; [window_ptr, screen_dimensions] …=Screen(0,'OpenWindow', [0, 0, 0]); shape_dimensions = screen_dimensions/2; Screen(window_pointer, 'FillRect', … [0,0,255], shape_dimensions); % then loop and increment screen_dimensions at each turn RuCCS
RuCCS Task 1: Basic Display Loop in PTB (code) % draw_stuff.m
% % Draws stuff on the screen clear all; try which_screen=0; bg_color = tic; [ 0 , 0 , 0 ]; [window_ptr, screen_dimensions]= … Screen(which_screen, … 'OpenWindow',bg_color); shape_dimensions = … screen_dimensions/2; while (toc < 5) % move shape shape_dimensions % draw shape [ 0 , 0 , 255 = … shape_dimensions + [0,5,0,5]; Screen(window_ptr, 'FillRect', … ], shape_dimensions); % flip buffer and repeat Screen(‘Flip’, window_ptr); end clear Screen; catch clear Screen; end
RuCCS Moving Shapes around the Screen • Shapes are defined by an array of numbers e.g. sh1 = [10, 15, 40, 45] ; % [x1, y1, x2, y2] • To move a shape in the x direction by 5 pixels we need to increment both x coordinates by 5 e.g. sh1(1) = sh1(1) +5; sh1(3) = sh1(3) +5 or use another array: sh1 = sh1 + [5, 0, 5, 0]; • Same for moving in the y direction • Increment both if you want to move diagonally
RuCCS Task 2
Make the rectangle move to the bottom of the screen at 5 pixels per frame, then move right when it hits the bottom.
When it hits the right, the program ends Hint: shape_dimensions(1) is x1, (2) is y1, (3) is x2, 4 is y2
RuCCS Task 3
When the rectangle hits the bottom make it change colors to red Hint: color is a 3 number vector red is [255, 0, 0]