Skip to content

Instantly share code, notes, and snippets.

@mottakun
Created August 5, 2017 14:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mottakun/b26a77e80b037af6378663c59bc76924 to your computer and use it in GitHub Desktop.
Save mottakun/b26a77e80b037af6378663c59bc76924 to your computer and use it in GitHub Desktop.
MATLAB Self-Paced Reading Test
function SPR_MainTest(cond)
% =========================================================================
% function SPR_MainTest
% Experiment = Self-Paced/RSVP Paradigm
% =========================================
% Assumes 60 Hz refresh rate
% ** PLEASE ADJUST MONITOR REFRESH RATE **
% =========================================
%
%% SELF PACED READING TEST
%
%% Code Authors:
% Thiago Oliveira da Motta Sampaio (ACESIN/LAPEX/INSERM/NeuroSpin)
% Virginie van Wassenhove (CEA/NeuroSpin)
%==========================================================================
%%
try
KbName('UnifyKeyNames');
escapeKey = KbName('ESCAPE');
AssertOpenGL;
%----------------------------------------------------------------------
% - PROMPT USER FOR DATA FILE NAME -
%----------------------------------------------------------------------
dataFile='tmp';
promptUser=true;
while promptUser
prompt=inputdlg('ID','Output File',1,{'tmp'});
if isempty(prompt);
disp(['Cancel experiment ...']);
return;
else
initials=prompt{1};
end
tmpFile = [initials,'_SPR.mat'];
if ~exist(tmpFile);
promptUser = false;
else
replace=questdlg(['Um arquivo com o mesmo nome já existe. Gostaria de substituí-lo?']);
if strcmp( replace, 'Yes' );
dataFile = tmpFile;
promptUser = false;
end
end
end
%----------------------------------------------------------------------
% LOADING VERSION/SUBJECT PARAMS
%----------------------------------------------------------------------
if exist('cond')
load([initials, '_SPR_MainTest_params_', cond, '.mat']);
dataFile = [initials,'_SPR_data_', cond];
else
display('error: specify condition');
end
psycdata = [];
%----------------------------------------------------------------------
% - INITIALIZE DISPLAY -
%----------------------------------------------------------------------
% affichage warning/error/autocalibration settings
Screen('Preference','SkipSyncTests', 0);
Screen('Preference','VisualDebugLevel', 1);
Screen('Preference', 'ConserveVRAM', 4096);
screens = Screen('Screens');
% Size of window: Default to fullscreen:
screenNumber = 0;
[w,screenRect] = Screen('OpenWindow', screenNumber,0,[],32,2);
% blending (pour antialiasing)
Screen('BlendFunction', w, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
% Display frequency [adaptive synchronizer]
fps = Screen('FrameRate',w); % frames per second
ifi = Screen('GetFlipInterval', w);
if fps == 0
fps = 1/ifi;
end
% Screen size in cm
sH = 24; % screen height (cm)
sW = 25; % screen width (cm)
%
whichScreen = max(Screen('Screens'));
[w,wRect] = Screen('OpenWindow', whichScreen);
% Color Defs
white = WhiteIndex(w);
black = BlackIndex(w);
gray = (white+black)/2;
red = [255 0 0 ];
green = [0 255 0];
blue = [0 0 255];
yellow = [255 255 0];
grayish = [100 100 100];
ft_color = white;
bck_color = black;
% Fixation Dot
% dim + pos defs
dW = wRect(3) - wRect(1);
dH = wRect(4) - wRect(2);
%
pixel = dH/sH;
x0 = dW/2;
y0 = dH/2;
fix_size = 0.2*pixel;
fix_pos = [x0-fix_size/2 y0-fix_size/2 x0+fix_size/2 y0+fix_size/2];
fix_col = black;
% choice positions TO CHECK
x1 = x0 - 300;
x2 = x0 + 150;
y1 = y0 - 100;
y2 = y0 - 150;
%
Screen('FillRect',w, bck_color);
Screen('Flip', w);
%
%%
%----------------------------------------------------------------------
% - INSTRUCTIONS SCREEN -
%----------------------------------------------------------------------
HideCursor;
inst1 = 'Pressione ESPA«áO para continuar';
inst2 = 'PAUSA';
inst3 = 'Fim do Experimento. Obrigado pela participaÁ„o';
%
clear RestrictKeysForKbCheck;
RestrictKeysForKbCheck([41, 44]);
Screen('FillRect',w, bck_color);
Screen('TextFont',w, 'Arial');
Screen('TextSize',w, 28);
Screen('DrawText', w, inst1, x0-300, y0, ft_color);
Screen('FillOval',w, fix_col, fix_pos);
Screen('Flip', w);
KbWait;
%----------------------------------------------------------------------
% - STIMULI PRESENTATION -
%----------------------------------------------------------------------
% - START LOOP -
priorityLevel=MaxPriority(w);
Priority(priorityLevel);
for block = 1:1;
% Initialize Data Matrix
psycdata = [];
for trial=1:length(expMat);
clear RestrictKeysForKbCheck;
RestrictKeysForKbCheck([41, 44]);
%
Screen('FillOval', w, fix_col, fix_pos);
[nx, ny, bbox] = DrawFormattedText(w, ' + ', 'center', 'center', 255);
Screen('Flip', w);
WaitSecs(1);
%
[nx, ny, bbox] = DrawFormattedText(w, ' ', 'center', 'center', 255);
Screen('Flip', w);
WaitSecs(.03);
% initialize trial RT
t0 = GetSecs;
for cnum = 3:9
promptUser = true;
[nx, ny, bbox] = DrawFormattedText(w, lista{expMat(trial, cnum), expMat(trial,2)}, 'center', 'center', 255);
Screen('Flip', w);
WaitSecs(0.2);
while (promptUser)
KbWait;
[keyIsDown,secs,keyCode]=KbCheck;
WaitSecs(0.001); % delay to prevent CPU hogging
if keyIsDown;
psycdata{block}(trial,cnum) = secs-t0; % col 3:9 RT
promptUser = false;
end
end
end
%
%WaitSecs(0.03) %???? tempo
RestrictKeysForKbCheck([14, 15, 41]);
% TASK
[nx, ny, bbox] = DrawFormattedText(w, lista{expMat(trial, 10), expMat(trial,2)}, 'center', 'center', red);
Screen('Flip', w);
%WaitSecs(.03);
promptUser = true;
while (promptUser)
KbWait;
[keyIsDown,secs,keyCode]=KbCheck;
if keyIsDown;
keyNum = find(keyCode);
psycdata{block}(trial,10) = secs-t0; % col 2 RT
if keyNum == 14; % Q
psycdata{block}(trial,11) = 1; % col 11 choix
elseif keyNum == 15; % P
psycdata{block}(trial,11) = 2; % col 11 choix
elseif keyCode(escapeKey)
error('Esc key was pressed'); % ESCAPE program
break;
end
promptUser = false;
end
end
%
end % trial
save(dataFile,'expMat','psycdata');
%----------------------------------------------------------------------
% - BREAK AND END OF EXPERIMENT -
%----------------------------------------------------------------------
if block > 1
% participant's break time after each block
Screen('FillRect',w, bck_color);
Screen('TextFont',w, 'Arial');
Screen('TextSize',w, 30);
Screen('DrawText', w, inst2, x0-50, y0-10, ft_color);
Screen('Flip', w);
WaitSecs(2)
KbWait;
Screen('DrawText', w, inst1, x0-300, y0, ft_color);
Screen('FillOval',w, fix_col, fix_pos);
Screen('Flip', w);
WaitSecs(2)
KbWait;
else
Screen('FillRect',w, black);
Screen('TextFont',w, 'Arial');
Screen('TextSize',w, 30);
Screen('DrawText', w, inst3, x0-350, y0, ft_color);
Screen('Flip', w);
WaitSecs(3);
end
%
end % block
%% SEPARATING TIMES OF EACH SEGMENT
% initialize temporary data matrix
tmpdata = []
% Copy Subject Data from psycdata
tmpdata = psycdata{1,1};
% merge trial number and stimuli number with subject data
tmpdata(:,1) = expMat(:,1);
tmpdata(:,2) = expMat(:,2);
% As Rts are cumulative (2nd segment RT = 1st + 2nd segment RT) we need
% to calculate segment RT from sentence check points.
tmpdata(:,4) = tmpdata(:,4)-tmpdata(:,3)
tmpdata(:,5) = tmpdata(:,5)-tmpdata(:,4)-tmpdata(:,3) % = tmpdata 5th column should be equal to tmpdata 5th column - tmpdata 4rd column and so on....
tmpdata(:,6) = tmpdata(:,6)-tmpdata(:,5)-tmpdata(:,4)-tmpdata(:,3)
tmpdata(:,7) = tmpdata(:,7)-tmpdata(:,5)-tmpdata(:,4)-tmpdata(:,6)-tmpdata(:,3)
tmpdata(:,8) = tmpdata(:,8)-tmpdata(:,5)-tmpdata(:,4)-tmpdata(:,6)-tmpdata(:,7)-tmpdata(:,3)
tmpdata(:,9) = tmpdata(:,9)-tmpdata(:,5)-tmpdata(:,4)-tmpdata(:,6)-tmpdata(:,7)-tmpdata(:,8)-tmpdata(:,3)
tmpdata(:,10) = tmpdata(:,10)-tmpdata(:,4)-tmpdata(:,3)-tmpdata(:,5)-tmpdata(:,6)-tmpdata(:,7)-tmpdata(:,8)-tmpdata(:,9)
% save temporary data matrix as the final subject data matrix 'subjdata'
subjdata = tmpdata
% clear base matrixes
clear psycdata, clear expMat, clear tmpdata
%%% END %%%
% Access DATA folder and save datafile
cd DATA_SPR/
save(dataFile, 'initials', 'subjdata');
% Closing the Experiment
Priority(0);
ShowCursor;
clear mex;
Screen('CloseAll');
%----------------------------------------------------------------------
% - CATCH ERRORS -
%----------------------------------------------------------------------
catch
ShowCursor;
Screen('CloseAll');
disp(['CRITICAL ERROR: ' lasterr ]);
disp(['Exiting program ...']);
rethrow(lasterror);
end
function SPR_SortTrials
% =========================================================================
% function (SPR) SortTrials
% Experiment = Self-Paced/Kinectic Reading
% Sort trials for SPR experiment
% =========================================
% Assumes 60 Hz refresh rate
% =========================================
%% Sort Trials for SPR Experiment
%
%% Code Authors:
% Thiago Motta Sampaio (ACESIN/LAPEX/INSERM/Neurospin)
%==========================================================================
%%
%----------------------------------------------------------------------
% - PROMPT USER FOR DATA FILE NAME -
%----------------------------------------------------------------------
dataFile='tmp';
promptUser=true;
while promptUser
prompt=inputdlg('ID','Output File',1,{'tmp'});
if isempty(prompt);
disp(['Cancel experiment ...']);
return;
else
initials=prompt{1};
promptUser = false;
end
end
%--------------------------------------------------------------------------
fRate = FrameRate([0]);
%
dur = 200; % ms
dur_f = fix(dur*fRate/1000); % frames
ITI_ms = [500,1000];
%% STIMULI
% FORM
% 'segment1','segment2','segment3','segment4','segment5','segment6,’segment7’,’QUESTION’;
% VERSION = Latin Square Version of the Test
%% %%%%%%%%%%%%%%% 1st VERSION %%%%%%%%%%%%%%%
lista1= {‘LIST YOUR STIMULI HERE’}’;
% END OF 1st VERSION
%% %%%%%%%%%%%%%%% 2nd VERSION %%%%%%%%%%%%%%%
lista2= {‘LIST YOUR STIMULI HERE’}’;
% END OF 2nd VERSION
%% %%%%%%%%%%%%%%% 3rd VERSION %%%%%%%%%%%%%%%
lista3= {‘LIST YOUR STIMULI HERE’}’;
% END OF 3rd VERSION
%% %%%%%%%%%%%%%%% 4th VERSION %%%%%%%%%%%%%%%
lista4= {‘LIST YOUR STIMULI HERE’}’;
% END OF 4th VERSION
%%%%%% End of Experimental Sentences %%%%%%%
%% BUILD expMat
nS = length(lista1); % total # of sentences
nV = 4; % total # of versions
ITI = fix(ITI_ms*fRate/1000); % range of ISI and ITI
% load conditions, open c % file containing condition map
for k = 1:nV
tmpmat(:,2) = Shuffle(Shuffle(1:nS)'); % COL 2 = sentence index in lista
tmpmat(:,1) = Shuffle(Shuffle(1:nS)'); % COL 1 = trial number
tmpmat(:,3) = 1,1; % COL 3 = Word(s)1
tmpmat(:,4) = 2,1; % COL 4 = Word(s)2
tmpmat(:,5) = 3,1; % COL 5 = Word(s)3
tmpmat(:,6) = 4,1; % COL 6 = Word(s)4
tmpmat(:,7) = 5,1; % COL 7 = Word(s)5
tmpmat(:,8) = 6,1; % COL 8 = Word(s)6
tmpmat(:,9) = 7,1; % COL 9 = Word(s)7
tmpmat(:,10) = 8,1; % COL 10 = TASK
tmpmat= sortrows(tmpmat);
expMat = tmpmat;
clear tmpmat
clear k
end
% INSTR for each version
for i = 1:4
% INSTR 1 = which list each version is going to use
if i == 1
lista = lista1
% expMat(:,3) = c(:,1) % Condition map for V1
elseif i == 2
lista = lista2
% expMat(:,3) = c(:,2) % Condition map for V2
elseif i == 3
lista = lista3
% expMat(:,3) = c(:,3) % Condition map for V3
elseif i == 4
lista = lista4
% expMat(:,3) = c(:,4) % Condition map for V4
end
% INSTR 2 = save parameters for each version
if i == 1
save([initials, '_SPR_params_A'])
elseif i == 2
save([initials, '_SPR_params_B'])
elseif i == 3
save([initials, '_SPR_params_C'])
elseif i == 4
save([initials, '_SPR_params_D'])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment