码迷,mamicode.com
首页 > 其他好文 > 详细

PTBtutorials—The basics

时间:2020-03-29 12:41:29      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:screenx   diff   计算   nbsp   solution   time   show   white   one   

 导航:1.打开屏幕,设置灰色;2.获取一些基本信息;3.精准计时;4.自定义精准计时;5.获取鼠标在屏幕上的坐标

1.Totally Minimal Demo:打开屏幕并将其变成灰色。这部分代码在后续demo中经常见到,这也是自己编心理程序时常用到的步骤

参考链接:http://peterscarfe.com/totallyminimaldemo.html      

% Clear the workspace and the screen   清除工作空间和多余窗口
sca;
close all;
clearvars;

% Here we call some default settings for setting up Psychtoolbox   调用默认设置
PsychDefaultSetup(2);

% Get the screen numbers. This gives us a number for each of the screens  获取屏幕数量
% attached to our computer.
screens = Screen(Screens);

% To draw we select the maximum of these numbers. So in a situation where we  选择最大值
% have two screens attached to our monitor we will draw to the external
% screen.
screenNumber = max(screens);

% Define black and white (white will be 1 and black 0). This is because
% in general luminace values are defined between 0 and 1 with 255 steps in   定义黑色和白色
% between. All values in Psychtoolbox are defined between 0 and 1
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);

% Do a simply calculation to calculate the luminance value for grey. This    得到灰色
% will be half the luminace values for white
grey = white / 2;

% Open an on screen window using PsychImaging and color it grey.   打开窗口.也可以将PsychImaging替换为Screens
[window, windowRect] = PsychImaging(OpenWindow, screenNumber, grey);

% Now we have drawn to the screen we wait for a keyboard button press (any   按任意键结束demo
% key) to terminate the demo.
KbStrokeWait;

% Clear the screen.  关闭屏幕
sca;

 

2.Totally Minimal Demo:获取所需要的的关于屏幕的一些基本信息

参考链接:http://peterscarfe.com/totallyminimaldemo2.html

% This function call will give use the same information as contained in "windowrect"获取窗口或显示器的矩形尺寸,返回的rect的左上角坐标始终为(0,0)
rect = Screen(‘Rect‘, window);

% Get the size of the on screen window in pixels, these are the last two 获取以像素为单位的窗口或显示器的宽度和高度
% numbers in "windowRect" and "rect"
[screenXpixels, screenYpixels] = Screen(‘WindowSize‘, window);

% Get the centre coordinate of the window in pixels. 获取屏幕中心坐标(单位为像素)
xCenter = screenXpixels / 2
yCenter = screenYpixels / 2
[xCenter, yCenter] = RectCenter(windowRect);

% Query the inter-frame-interval. This refers to the minimum possible time 获取页面切换时间
between drawing to the screen
ifi = Screen(‘GetFlipInterval‘, window);

% We can also determine the refresh rate of our screen. The  获取/设置刷新率
relationship between the two is: ifi = 1 / hertz
hertz=Screen(‘FrameRate‘,w)
hertz = FrameRate(window);
% We can also query the "nominal" refresh rate of our screen. This is 获取屏幕刷新率(显卡所描述的。被近似为最近的整数,但与实际刷新率存在细小差异) the refresh rate as reported by the video card. This is rounded to the nearest integer. In reality there can be small differences between "hertz" and "nominalHertz" nominalHertz = Screen(‘NominalFrameRate‘, window); % Here we get the pixel size which is the color depth of the pixel in bits。获取窗口或显示器的像素位数(颜色位数) pixelSize = Screen(‘PixelSize‘, window);
%获取当前分辨率下可用的像素位数
pixelSizes=Screen(‘PixelSizes‘,window)
% Queries the display size in mm as reported by the operating system. Note 获取显示器的物体高度和宽度(以毫米为单位,操作系统所描述的) that there are some complexities here. 如无法获取实际的显示尺寸,则返回值为0.但获取的并不可靠,仅供参考,否则该函数在计算视角时会非常有用 [width, height] = Screen(‘DisplaySize‘, screenNumber);
%获取窗口的颜色范围 [oldmaxvalue,oldclampcolors]= Screen(‘ColorRange‘, window,maxvalue,clampcolors);
maxvalue 颜色成分最大值 clampcolors 默认为1,自动将超出颜色范围的值设置为最小值和最大值。为0,则不做处理
oldmaxvalue 变更前的旧值 oldclampcolors 变更前旧的越界处理方式

%获取显示器分辨率
resolutions=Screen(‘Resolutions‘,screennumber)

%设置显示器分辨率
oldresolution=Screen(‘Resolution‘,screennumber,newwidth,newheight,newhz,newpixelsize)
oldresolution=SetResolution(screennumber,newwidth,newheight,newhz,newpixelsize)
SetResolution(screennumber,oldresolution)
参数依次为新的分辨率的宽度与高度、新的显示模式的刷新率与颜色深度(颜色深度一般去8、16、24、32)

3.Accurate Timing Demo:呈现每一帧都变化的刺激
参考链接:http://peterscarfe.com/accuratetimingdemo.html
% Measure the vertical refresh rate of the monitor
ifi = Screen(GetFlipInterval, window);

% Retreive the maximum priority number  检索最大优先级
topPriorityLevel = MaxPriority(window);

% Length of time and number of frames we will use for each drawing test
numSecs = 1;
numFrames = round(numSecs / ifi);

% Numer of frames to wait when specifying good timing. Note: the use of
% wait frames is to show a generalisable coding. For example, by using
% waitframes = 2 one would flip on every other frame. See the PTB
% documentation for details. In what follows we flip every frame.
waitframes = 1;

%------------
% EXAMPLE #1
%------------

% First we will demonstrate a poor way in which to get good timing of
% visually presented stimuli. We generally use this way of presenting in    在硬件有缺陷时采用这样的程序,一般并不推荐这种
% the demos in order to allow the demos to run on potentially defective
% hardware. In this way of presenting we leave much to chance as regards
% when our stimuli get to the screen, so it is not reccomended that you use
% this approach.
for frame = 1:numFrames

    % Color the screen grey
    Screen(FillRect, window, [0.5 0.5 0.5]);

    % Flip to the screen
    Screen(Flip, window);

end
%------------
% EXAMPLE #2
%------------

% Finally we do the same as the last example except now we additionally
% tell PTB that no more drawing commands will be given between coloring the
% screen and the flip command. This, can help acheive good timing when one
% is needing to do additional non-PTB processing between setting up drawing
% and flipping to the screen. Thus, you would only use this technique if
% you were doing this. So, if you are not, go with example #3
Priority(topPriorityLevel); 将PTB优先级设置为最高,使其先于其他系统和应用程序。建议在设置窗口后,脚本开始时立刻设置优先级
vbl = Screen(Flip, window); 获取刷屏完成的时刻
for frame = 1:numFrames

    % Color the screen blue
    Screen(FillRect, window, [0 0 0.5]);

    % Tell PTB no more drawing commands will be issued until the next flip  告知PTB在翻转页面前没有进一步绘制的要求,使得PTB对绘制操作进行优化处理。
    Screen(DrawingFinished, window);                                         但是不要在一次页面切换前多次使用该命令,否则会影响性能
如果需要在绘制与翻转间需要其他非PTB操作,可以这样。不需要则可以省略该步骤
% Flip to the screen vbl = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi); 这里使用了一半的刷屏周期(半帧),确保PTB实现我们所想要的时程 end Priority(0);

 

4.Wait Frames Demo:以不同于显示器刷新率的速率跟新图像,如每秒更新一次

参考链接:http://peterscarfe.com/waitframesdemo.html

% Measure the vertical refresh rate of the monitor
ifi = Screen(GetFlipInterval, window);

% Retreive the maximum priority number and set max priority
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);

% Here we use to a waitframes number greater then 1 to flip at a rate not
% equal to the monitors refreash rate. For this example, once per second,
% to the nearest frame
flipSecs = 1;
waitframes = round(flipSecs / ifi);    和上面的示例3基本一致,只是图像更新的速率变为了1s一次

% Flip outside of the loop to get a time stamp
vbl = Screen(Flip, window);

% Run until a key is pressed
while ~KbCheck

    % Color the screen a random color
    Screen(FillRect, window, rand(1, 3));

    % Flip to the screen
    vbl = Screen(Flip, window, vbl + (waitframes - 0.5) * ifi);

end


5.Screen Coordinates Demo:当在屏幕上移动鼠标时,在屏幕上绘制显示此时鼠标位置坐标的文本。这有助于了解屏幕坐标如何定义,即如何查询鼠标位置
参考链接:http://peterscarfe.com/screencoordinatesdemo.html

SetMouse(x,y,w) /WaitSetMouse(x,y,w)  将鼠标设置于屏幕上某位置    x,y为此位置坐标,w为窗口指针


[x,y,button]=Getmouse(w) 获取指定窗口的鼠标状态   x,y为鼠标位置的坐标,button为1*3的向量,取0时表示无按键,取1时表示有按键


DrawFormattedText[w,text,x,y,color]   w为窗口指针,text为绘制的文本,x,y为文本起始坐标(省略则从0.0开始,为"center"则为水平或垂直居中),color为[r g b]或[r g b a]

% Open an on screen window 打开窗口
[window, windowRect] = PsychImaging(OpenWindow, screenNumber, black);

% Get the size of the on screen window   获取显示器像素大小
[screenXpixels, screenYpixels] = Screen(WindowSize, window);

% Query the frame duration  获取刷屏周期
ifi = Screen(GetFlipInterval, window);

% Setup the text type for the window  设置字体与字号
Screen(TextFont, window, Ariel);
Screen(TextSize, window, 50);

% Get the centre coordinate of the window 获取屏幕中心位置的坐标
[xCenter, yCenter] = RectCenter(windowRect);

% Here we set the initial position of the mouse to a random position on the  将鼠标的初始位置设置为随机值
% screen
SetMouse(round(rand * screenXpixels), round(rand * screenYpixels), window);

% Set the mouse to the top left of the screen to start with 设置到屏幕左上角
SetMouse(0, 0, window);

% Loop the animation until a key is pressed  
while ~KbCheck   检测按键。若没有按键,则该循环一直进行

    % Get the current position of the mouse
    [x, y, buttons] = GetMouse(window);  获取鼠标位置与按键状况

    % We clamp the values at the maximum values of the screen in X and Y 如果连接了两台显示器,将坐标值限制在屏幕最大值内
    % incase people have two monitors connected. This is all we want to
    % show for this basic demo.
    x = min(x, screenXpixels);
    y = min(y, screenYpixels);

    % Construct our text string
    textString = [Mouse at X pixel  num2str(round(x))...
         and Y pixel  num2str(round(y))];

    % Text output of mouse position draw in the centre of the screen
    DrawFormattedText(window, textString, center, center, white); 绘制文本

    % Draw a white dot where the mouse cursor is
    Screen(DrawDots, window, [x y], 10, white, [], 2);  在鼠标所在位置处画点

    % Flip to the screen
    Screen(Flip, window);

end

PTBtutorials—The basics

标签:screenx   diff   计算   nbsp   solution   time   show   white   one   

原文地址:https://www.cnblogs.com/zxpsyneuroscience/p/12590204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!