Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Psychophysics Toolbox

Similar presentations


Presentation on theme: "The Psychophysics Toolbox"— Presentation transcript:

1 The Psychophysics Toolbox
Part II In Matlab

2 Showing Movies % Ignore synchronization tests Screen('Preference', 'SkipSyncTests', 1); % Open a pointer in video memory ScreenNum = 0; [windowPtr,Rect] = Screen('OpenWindow', ScreenNum); % Prepare some image creation variables [x,y] = meshgrid(1:Rect(3),1:Rect(4)); r = hypot(x-(Rect(3)+1)/2,y-(Rect(4)+1)/2);

3 Showing Movies (continued)
% Generate the frame textures nFrm = 100; Texture = zeros(nFrm,1); for iFrm = 1:nFrm Img = cos(2*pi*r/20 - 2*pi*iFrm/nFrm)* ; Texture(iFrm) = Screen('MakeTexture', windowPtr, Img); end Screen('PreloadTextures', windowPtr);

4 Showing Movies (continued)
for iFrm = 1:nFrm % Draw the image in video memory Screen('DrawTexture', windowPtr, Texture(iFrm)); % Transfer the image from video memory to the screen Screen('Flip', windowPtr); end % Close all windows and clear video memory Screen('CloseAll');

5 Line-By-Line nFrm = 100; Movies are made up of several static images presented in rapid succession (e.g., each image may last only 0.02 seconds). We will prepare 100 such images.

6 Line-By-Line Texture = zeros(nFrm,1);
Each image will be saved in video memory as an Open Graphics Library (OpenGL) “texture” and each texture will be assigned a scalar pointer that we can use to access it.

7 Line-By-Line for iFrm = 1:nFrm
Img = cos(2*pi*r/20 - 2*pi*iFrm/nFrm)* ; Texture(iFrm) = Screen('MakeTexture', windowPtr, Img); end We create our images in a for loop starting with image 1, and going up to image number nFrm (i.e. 100). Each image is a cosine function of the radius whose phase depends on the frame number. The images are saved as OpenGL textures, and a pointer to the texture is saved in the variable Texture (one for each video frame). φ = 0° 1 φ = 45° 0.5 cos(r + φ) φ = 90° -0.5 2 4 r 6 8 10 12

8 Line-By-Line Screen('PreloadTextures', windowPtr);
Pre-loads the textures to into video RAM for fast drawing. This can reduce drawing time by avoiding upload delays.

9 Line-By-Line for iFrm = 1:nFrm % Draw the image in video memory
Screen('DrawTexture', windowPtr, Texture(iFrm)); % Transfer the image from video memory to the screen Screen('Flip', windowPtr); end We display the movie in a for loop, starting from frame 1 and stopping after frame nFrm. We draw each texture to the specified target window using DrawTexture. Then we Flip the texture onto the display to make it visible.

10 Line-By-Line Screen('CloseAll');
Closes all windows and clears video memory. This brings us back to the desktop.

11 Built-In Functions Using the methods presented, each and every pixel may be individually controlled to produce any desired image. Some types of images, however, are commonly used in experiments, and so built-in functions have been written to implement them.

12

13 (0,0) Shapes y(2,1)-y(1,1) (Rect(3),Rect(4)) % Ignore synchronization tests Screen('Preference', 'SkipSyncTests', 1); % Open a pointer in video memory ScreenNum = 0; [windowPtr,Rect] = Screen('OpenWindow', ScreenNum,[0 0 0]); % Set up a grid of points where we will put shapes [x,y] = meshgrid(linspace(1,Rect(3),6),linspace(1,Rect(4),4)); x = round(x(2:end-1,2:end-1)); y = round(y(2:end-1,2:end-1)); w = round(0.9*(y(2,1)-y(1,1))/2); % Half the width of a shape

14 Shapes (continued) % Draw a line x1 = 0; x2 = Rect(3); y1 = round(Rect(4)/2); y2 = y1; Screen('DrawLine',windowPtr ,[ ],x1,y1,x2,y2); (x1,y1) (x2,y2)

15 Shapes (continued) % Draw an arc ArcRect = [x(1,1)-w y(1,1)-w x(1,1)+w y(1,1)+w]; startAngle = 135; arcAngle = 270; Screen('FrameArc',windowPtr,[ ],ArcRect,startAngle,arcAngle) ArcRect = [x(1,1)-w y(2,1)-w x(1,1)+w y(2,1)+w]; Screen('FillArc',windowPtr,[ ],ArcRect,startAngle,arcAngle) w (ArcRect(1),ArcRect(2)) startAngle (deg.) arcAngle (deg.) (ArcRect(3),ArcRect(4))

16 Shapes (continued) % Draw a circle CircRect = [x(1,2)-w y(1,2)-w x(1,2)+w y(1,2)+w]; Screen('FrameOval',windowPtr,[ ],CircRect); CircRect = [x(2,2)-w y(2,2)-w x(2,2)+w y(2,2)+w]; Screen('FillOval',windowPtr,[ ],CircRect); w (CircRect(1), CircRect(2)) (CircRect(3), CircRect(4))

17 Shapes (continued) % Draw a square SqrRect = [x(1,3)-w y(1,3)-w x(1,3)+w y(1,3)+w]; Screen('FrameRect',windowPtr,[ ],SqrRect); SqrRect = [x(2,3)-w y(2,3)-w x(2,3)+w y(2,3)+w]; Screen('FillRect',windowPtr,[ ],SqrRect); w (SqrRect(1), SqrRect(2)) (SqrRect(3), SqrRect(4))

18 Shapes (continued) PointList = [ …] % Draw a polygon t = 0:pi/12:2*pi; r = sin(8*t)*w/2+w/2; PointList = [r.*cos(t)+x(1,4); r.*sin(t)+y(1,4)]'; Screen('FramePoly',windowPtr,[ ],PointList); PointList = [r.*cos(t)+x(2,4); r.*sin(t)+y(2,4)]'; Screen('FillPoly',windowPtr,[ ],PointList);

19 Shapes (continued) % Transfers the image from video memory to the monitor Screen('Flip', windowPtr); % Waits for the user to press a key KbWait; % Closes all windows Screen('CloseAll');

20

21 Text R, G, B colour x-position y-position
% Ignore synchronization tests Screen('Preference', 'SkipSyncTests', 1); % Open a pointer in video memory ScreenNum = 2; [windowPtr,Rect] = Screen('OpenWindow', ScreenNum,[0 0 0]); Screen('DrawText',windowPtr,'Press any key to continue ...',100,200,[ ]); % Transfers the image from video memory to the monitor Screen('Flip', windowPtr); % Waits for the user to press a key KbWait; % Closes all windows Screen('CloseAll'); R, G, B colour x-position y-position

22 200 100

23 Text Options % Draw Text in windows textModes = Screen('TextModes');
oldCopyMode=Screen('TextMode', windowPtr [,textMode]); oldTextSize=Screen('TextSize', windowPtr [,textSize]); oldStyle=Screen('TextStyle', windowPtr [,style]); [oldFontName,oldFontNumber,oldTextStyle]=Screen('TextFont', windowPtr [,fontNameOrNumber][,textStyle]); [normBoundsRect, offsetBoundsRect, textHeight, xAdvance] = Screen('TextBounds', windowPtr, text [,x] [,y] [,yPositionIsBaseline] [,swapTextDirection]); [newX, newY, textHeight]=Screen('DrawText', windowPtr, text [,x] [,y] [,color] [,backgroundColor] [,yPositionIsBaseline] [,swapTextDirection]); oldTextColor=Screen('TextColor', windowPtr [,colorVector]); oldTextBackgroundColor=Screen('TextBackgroundColor', windowPtr [,colorVector]); oldMatrix = Screen('TextTransform', windowPtr [, newMatrix]);

24 Centering Text % Ignore synchronization tests Screen('Preference', 'SkipSyncTests', 1); % Open a pointer in video memory ScreenNum = 2; [windowPtr,Rect] = Screen('OpenWindow', ScreenNum,[0 0 0]); % Get the size of the text [TxtRect, offsetBoundsRect] = Screen('TextBounds',windowPtr,'My centered text'); % Center the text on the screen CentRect = CenterRect(TxtRect,Rect);

25 Centering Text (continued)
% Draw the text FntCol = [ ]; [newX,newY] = Screen('DrawText',windowPtr,'My centered text', ... CentRect(1),CentRect(2),FntCol); % Transfers the image from video memory to the monitor Screen('Flip', windowPtr); % Waits for the user to press a key KbWait; % Closes all windows Screen('CloseAll');

26 My centered text

27 PTB Summary We’ve learned how to:
Make our own images (where we control every pixel) and put them on the screen. Make and display movies. Put pre-defined shapes on the screen. Put text on the screen.


Download ppt "The Psychophysics Toolbox"

Similar presentations


Ads by Google