-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolt_practice_rep_supp.m
executable file
·254 lines (202 loc) · 7.78 KB
/
volt_practice_rep_supp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
% 12/21/2015: press 5 before starting scanner to move from "Object Preference Task"
% to fixation cross (have participant fixate during ref lines)
% scanner should trigger experiment as normal!!!
function data = volt_practice_rep_supp(header)
%% Setup
par = header.parameters;
expName = 'volt';
data = struct('subNr', header.subNr);
data.Environs = header.Environs;
data.dispStim = header.practice.rep_supp.disp;
data.actualOnsets = nan(par.practice.rep_supp.disp.nTotalTrials,1);
data.RT = nan(par.practice.rep_supp.disp.nTotalTrials,1);
data.resp = nan(par.practice.rep_supp.disp.nTotalTrials,1);
nStimPerRun = 5; % number of stimuli per run, hard coded for practice
allowedRespTime = 3; % # of seconds in which the participant can respond
% Set response options
oneKey = KbName('1!'); % button to output 1
twoKey = KbName('2@'); % button to output 2
threeKey = KbName('3#'); % button to output 3
fourKey = KbName('4$'); % button to output 4
posskeys = [oneKey twoKey threeKey fourKey];
%% Check for button box
intake=PsychHID('Devices');
extKeys=0;
for n=1:length(intake)
if (intake(n).productID == 8 && strcmp(intake(n).usageName,'Keyboard')) % UT
extKeys=n;
elseif strcmp(intake(n).usageName,'Keyboard')
intKeys=n;
end
end
if extKeys==0, disp('No Buttonbox Detected.'); end
% dev = extKeys; % change this line to intKeys for running in the lab
dev = intKeys; % change this line to extKeys for running in the scanner
%% Preload all stimuli
namepattern = [header.path.stim 'object_%03d.jpg'];
obj_stim = cell(nStimPerRun,1);
for t=1:nStimPerRun % for each stimuli
stimNr = header.practice.rep_supp.disp(t,3); % stimuli numbers
for obj = 1:length(stimNr)
stimfname = sprintf(namepattern,stimNr(obj));
obj_stim{t,obj} = imread(stimfname);
end
end
data.stimNamePattern = namepattern;
%% Create output text file
outfname = sprintf('%s/%s_practice_RS_%d_%s',header.path.practicedata_subNr,expName,header.subNr,header.subInit);
if exist([outfname '.txt'],'file') == 2
error('The data file for this scan already exists. Please check your input parameters.');
end
formatString = '%0.1f\t%0.1f\t%d\t%d\t%0.3f\n';
fid=fopen([outfname '.txt'], 'w'); % open the file
fprintf(fid,'%s\t%s\t%s\t%s\t%s\n',...
'onset (s)','fixation','stimNmbr','Resp','RT'); %Create header
%% Open up the screen
oldEnableFlag = Screen('Preference','SuppressAllWarnings');
Screen('Preference', 'VisualDebugLevel', 1);
Screen('Preference', 'SkipSyncTests', 2); % will not show warning screen
[par.window, par.screenRect] = Screen(0, 'OpenWindow', par.backColor); % change me
[monitorFlipInterval] = Screen(par.window,'GetFlipInterval'); % gets the refresh time / monitor flip interval, to be used in calculating onsets below
par.xc = par.screenRect(3)/2;
par.yc = par.screenRect(4)/2;
par.xct = par.xc - par.txtSize/3; % center of screen for text
par.yct = par.yc - par.txtSize/3; % center of screen for text
Screen(par.window, 'TextSize', par.txtSize);
Screen(par.window, 'TextFont', 'Arial');
HideCursor;
% Display run number -- PRESS 5 MANUALLY TO GET FIXATION FOR REFERENCE LINES
repText = sprintf('Object Preference Task: Practice Run');
Screen('TextSize',par.window,par.txtSize);
[normBoundsRect_repText,offsetBoundsRects_repText] = Screen('TextBounds',par.window,repText);
xcreptext = par.xc - normBoundsRect_repText(3)/2;
Screen(par.window, 'DrawText', repText, xcreptext, par.yc, par.txtColor);
Screen(par.window, 'Flip');
clear keyCode;
clear keyIsDown;
pa.TRIGGERED = 0;
while ~pa.TRIGGERED
[keyIsDown, t, keyCode] = KbCheck(-1);
if strcmp(KbName(keyCode), '7&')
pa.TRIGGERED = 1;
end
end
while KbCheck; end
% Fixation for reference lines, trigger happens by scanner
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
Screen(par.window, 'Flip');
clear keyCode;
clear keyIsDown;
clear t;
pa.TRIGGERED = 0;
while ~pa.TRIGGERED
[keyIsDown, t, keyCode] = KbCheck(-1);
if strcmp(KbName(keyCode), '5%')
pa.TRIGGERED = 1;
end
end
% to here
scannerStart = GetSecs;
startTime = scannerStart+par.bookendFixTime;
data.beginTime = fix(clock);
% Flip fixation on screen
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor); %add par.initTime for scanner!;
Screen(par.window, 'Flip');
%% Loop through all trials
for t = 1:nStimPerRun
% Get trial onset
ontime = data.dispStim(t,1);
fixation = data.dispStim(t,2);
img = obj_stim{t,1};
%% Stimulus with black fixation
% Make texture for current trial presentation
stim = Screen(par.window,'MakeTexture',img);
% Draw current trial image to buffer with fixation superimposed
Screen(par.window, 'DrawTexture',stim);
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% Calculate presentation onset
stimtime = startTime + ontime;
% Flip presentation of image to screen at correct onset
on = Screen(par.window,'Flip',stimtime);
data.actualOnsets(t) = on - startTime;
% Collects response and RT during object presentation
[resp rt] = flipkeys_kate(stimtime, par.rep_supp.stimTime,par,dev,on,posskeys);
data.resp(t) = str2double(resp(1));
data.RT(t) = rt(1);
% Draw fixation to buffer
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% Calculate fixation onset
fixtime = stimtime + par.rep_supp.stimTime;
%% flip fixation to screen
on = Screen(par.window,'Flip',fixtime);
% Calculate allowed response time
resptime = stimtime + allowedRespTime;
% get response and RTs
resp = [];
while GetSecs < resptime
[keyIsDown, ~, keyCode] = KbCheck; %check for key response
if keyIsDown
if keyCode(oneKey)
resp = 1;
respstop = GetSecs;
break;
elseif keyCode(twoKey)
resp = 2;
respstop = GetSecs;
break;
elseif keyCode(threeKey)
resp = 3;
respstop = GetSecs;
break;
elseif keyCode(fourKey)
resp = 4;
respstop = GetSecs;
break;
end %end of keycode check
end %end of kbcheck
end %end of while
% black fixation alone
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
%% save responses
% assign RT and fixation response if participant didn't respond in time
if isempty(resp)
resp = nan;
rt = nan;
else
rt = respstop - stimtime; % trial RT
end
% data.resp(t) = str2double(resp(1));
data.resp(t) = resp(1);
data.RT(t) = rt(1);
% Save trial info to text file
fprintf(fid, formatString, data.dispStim(t,:), data.resp(t), data.RT(t));
end
clear t
% while (GetSecs-startTime) <= par.rep_supp.nTotalTrials*par.rep_supp.trialTime
% end
WaitSecs(par.bookendFixTime)
% Put relevant info into data structure
data.duration = GetSecs - startTime; % includes end bookend fix time but not beginning
data.endTime = fix(clock);
clear startTime;
%% End experiment
ShowCursor;
Screen('CloseAll');
Screen('Preference','SuppressAllWarnings',oldEnableFlag);
clear screen;
data.parameters = par;
save(outfname,'data');
disp('---------------');
disp(sprintf('Object Preference Task: Practice Run completed!'));
disp('---------------');
disp(sprintf('NaNs: %d',sum(isnan(data.resp))));
disp('---------------');
disp(' ');
disp(sprintf('Please continue by calling %s_rep_supp',expName))
disp('---------------');
disp(' ');