我的电脑屏幕刷新率为165Hz,对于1秒、0.5秒这样表示不能整除每帧0.0061s的时间,为保证实验的准确性,采用帧的方式来呈现图片:
代码:
global screens screenNumber win wsize flipIntv slack cx cy time_stamp
try
%初始化
HideCursor;
InitializeMatlabOpenGL;
Screen('Preference','SkipSyncTests',1);
screens = Screen('Screens');
screenNumber = max(screens);
[win,wsize] = Screen('OpenWindow',screenNumber);
cx = wsize(3)/2;
cy = wsize(4)/2;
flipIntv = Screen('GetFlipInterval',win); %我电脑屏幕刷新率为165Hz 1/165 = 0.0061 即flipIntv的值为0.0061
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win);
% 呈现一个视角5度的图片
pixs = deg2pix(5,15.5,wsize(3),50); %计算该条件下对应视角在屏幕中的像素数
%画图
%首先加载图片 本脚本与Screen_tutorial处于同一目录
cd Screen_tutorial/images2
files = dir('*tif*');
for i = 1:length(files)
Image_pool{i} = imread(files(i).name);
end
%进行一一组合一共有多少条件
%设定SOA条件 SOA为启动刺激(Prime)呈现起到目标刺激(Probe)呈现之间的时间
%可理解为图片呈现的时间
%由于1秒、0.5秒这样表示不能整除每帧0.0061s的时间,为保证实验的准确性,不采用秒的方式来呈现图片
%更改采用帧数来展示图片,这里设置了一个存储时间的元胞数组SOA_pool 为了好计算 间隔专门设置成每帧所用时间
SOA_pool = [6.1:6.1:24.4000]/1000;
SOA_pool = time2frame(flipIntv,SOA_pool); %调用time2frame函数 该函数将时间四舍五入取整求帧数
Mask_pool = 1:2; %Mask_pool用来表示实验过程中是否有mask图片
%计算共有多少次数 遍历所有条件
Total_trial = length(Image_pool)*length(SOA_pool)*length(Mask_pool);
%分配图像在这么多试次的分布(随机)
%由于matlab从1开始 取模后要加1
Image_order = mod(randperm(Total_trial),length(Image_pool))+1;
SOA_order = mod(randperm(Total_trial),length(SOA_pool))+1;
Mask_order = randi(2,[1,Total_trial])-1;
%秒转化为帧
trial_interval = 1; %试次之间的间隔,单位是秒
trial_interval = time2frame(flipIntv,trial_interval);
fix_onset = 0.5;
fix_onset = time2frame(flipIntv,fix_onset);
mask_onset = 0.5;
mask_onset = time2frame(flipIntv,mask_onset);
word_onset = 3;
word_onset = time2frame(flipIntv,word_onset);
for i = 1:Total_trial
Screen('FillRect',win,128);%背景与注视点
Screen('FillOval',win,255, [cx-5,cy-5,cx+5,cy+5]);
time_stamp = Screen('Flip',win,time_stamp+(trial_interval-0.5)*flipIntv);
%展示图片
Screen('FillRect',win,128);
Image_Index = Screen('MakeTexture',win,Image_pool{Image_order(i)});
Screen('DrawTexture',win,Image_Index,[],[cx-pixs,cy-pixs,cx+pixs,cy+pixs]);
time_stamp = Screen('Flip',win,time_stamp+(fix_onset-0.5)*flipIntv);
if(Mask_order(i))
%呈现mask
Screen('FillRect',win,128);
masks = im2uint8(rand(50));
Image_Index = Screen('MakeTexture',win,masks);
Screen('DrawTexture',win,Image_Index,[],[cx-pixs,cy-pixs,cx+pixs,cy+pixs]);
time_stamp = Screen('Flip',win,time_stamp+(SOA_pool(SOA_order(i))-0.5)*flipIntv);
else
%不呈现mask
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win,time_stamp+(SOA_pool(SOA_order(i))-0.5)*flipIntv);
end
%呈现指导语
Screen('FillRect',win,128);
txt = '2 Animal';
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy,255);
txt = '1 Object';
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy-bRect(4),255);
txt = '3 Scene';
bRect = Screen('TextBounds',win,txt);
Screen('DrawText',win,txt,cx-bRect(3)/2,cy+bRect(4),255);
time_stamp = Screen('Flip',win,time_stamp+(mask_onset-0.5)*flipIntv);
%要求被试反应
Screen('FillRect',win,128);
time_stamp = Screen('Flip',win,time_stamp+(word_onset-0.5)*flipIntv);
end
time_stamp = Screen('Flip',win,time_stamp+1);
%记录状态
stats.subinfo={'WZY','Male','21yrs'};
stats.trial_num=Total_trial;
stats.Image_order=Image_order;
stats.image_shown=Image_pool;
stats.SOAs=SOA_pool(SOA_order);
stats.Masks=Mask_pool(Mask_order+1);
stats.response=[];
stats.docs='Masks = 1 is unmask,Masks = 2 is masked';
save mask_result.mat stat%储存在.mat文件中
Screen('CloseAll');
catch
sca;
end
function pixs=deg2pix(degree,inch,pwidth,vdist)
screenWidth = inch*2.54/sqrt(1+9/16);
pix = screenWidth/pwidth;
pixs = round(2*tan((degree/2)*pi/180)*vdist/pix);
end
%求帧函数
function nframe = time2frame(flipIntv,duration)
nframe = round(duration/flipIntv);%round四舍五入
end
详细讲解参考b站up的视频:https://www.bilibili.com/video/BV1g44y1P7UD?spm_id_from=333.880.my_history.page.click