-
Notifications
You must be signed in to change notification settings - Fork 0
/
volt_disp.m
executable file
·304 lines (246 loc) · 9.83 KB
/
volt_disp.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
% 11/27/2012: fixation during reference lines added by MLS
% press 5 before starting scanner to move from "Detection Task"
% to fixation cross (have sub fixate during ref lines)
% scanner should trigger experiment as normal!!!
% 05/2015: edited by KRS for volt
function data = volt_disp(header,phase,runNr)
%% Setup
par = header.parameters;
expName = 'volt';
data = struct('subNr', header.subNr);
data.Environs = header.Environs;
data.dispStim = header.disp{runNr};
data.actualOnsets = nan(par.disp.nStimPerRun,1);
data.RT = nan(par.disp.nStimPerRun,1);
data.resp = nan(par.disp.nStimPerRun,1);
data.correctResp = data.dispStim(:,3);
% 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(par.nEnvTraining, par.nObjPerEnv);
for t=1:par.nEnvTraining % for each environment
env = data.Environs(t,:);
stimType = env(2:5);
stimNr = env(6:9);
for obj = 1:par.nObjPerEnv
stimfname = sprintf(namepattern,stimNr(obj));
obj_stim{t,obj} = imread(stimfname);
end
end
data.stimNamePattern = namepattern;
%% %% Create output text file
outfname = sprintf('%s/%s_disp%d_run%d_%d_%s',header.path.subNr,header.exp,phase,runNr,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 = '%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%0.3f\t%d\n';
fid=fopen([outfname '.txt'], 'w'); % open the file
fprintf(fid,'%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n',...
'onset (s)','relFixFlip (ms)','fixColor','envNr','sTypeA','sTypeB','sTypeC','sTypeD',...
'env1','env2','env3','env4','stimType','Resp','RT','Accuracy'); %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 depending on screen output (either 0 or 1)
[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('Detection Task %d: Run %d',phase,runNr);
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);
Screen(par.window, 'Flip');
% loop through all study trials
for t = 1:par.disp.nStimPerRun
% get trial onset
ontime = data.dispStim(t,1);
envNr = data.dispStim(t,4);
switch data.dispStim(t,13);
case 1 % A
img = obj_stim{envNr,1};
case 2 % B
img = obj_stim{envNr,2};
case 3 % C
img = obj_stim{envNr,3};
case 4 % D
img = obj_stim{envNr,4};
end
%% flip 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 object presentation onset
stimtime = startTime + ontime - monitorFlipInterval/2; % subtract refresh time / 2
% flip presentation of image to screen at correct onset
on = Screen(par.window,'Flip',stimtime);
data.actualOnsets(t) = on - startTime;
%% flip 2: stimulus with blue/green fixation
% draw the colored fixation to buffer
stim = Screen(par.window,'MakeTexture',img);
% draw current trial image to buffer with COLORED fixation superimposed
Screen(par.window,'DrawTexture',stim);
Screen(par.window, 'TextSize', par.numSize);
if data.dispStim(t,3)==1
Screen(par.window,'DrawText', '+', par.xct, par.yct, [0 0 255]); % make it blue
else
Screen(par.window,'DrawText', '+', par.xct, par.yct, [60 179 113]); % make it green
end
objfliptime = stimtime + data.dispStim(t,2)/1000; % trial onset + time to colored flip
% flip fixation to screen at correct onset
on = Screen(par.window,'Flip',objfliptime);
%% flip 3: blue/green fixation alone
Screen(par.window, 'TextSize', par.numSize);
if data.dispStim(t,3)==1
Screen(par.window,'DrawText', '+', par.xct, par.yct, [0 0 255]); % make it blue
else
Screen(par.window,'DrawText', '+', par.xct, par.yct, [60 179 113]); % make it green
end
% flips blue/green fixation alone
% [resp rt] = flipkeys_kate(par.disp.stimTime - data.dispStim(t,2)/1000,par.disp.stimTime - data.dispStim(t,2)/1000 + par.disp.fixTime - 0.5,par,dev,on,posskeys);
% calculate flip to blue/green fixation alone onset
fixfliptime = stimtime + par.disp.stimTime; % trial onset + object duration
% get response and RTs
resp = [];
% respstart = GetSecs; %start of response time
while GetSecs < fixfliptime
[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;
end %end of keycode check
end %end of kbcheck
end %end of while
% flip fixation off screen at correct onset
Screen(par.window,'Flip',fixfliptime);
%% flip 4: black fixation alone
Screen(par.window, 'TextSize', par.numSize);
Screen(par.window, 'DrawText', '+', par.xct, par.yct, par.fixColor);
% calculate flip to black fixation onset
blackfixtime = stimtime + (par.disp.trialTime-0.5);
while GetSecs < blackfixtime
[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;
end %end of keycode check
end %end of kbcheck
end %end of while
Screen(par.window,'Flip',blackfixtime);
%% save responses
% assign RT and fixation response if participant didn't respond in time
if isempty(resp)
resp = nan;
rt = nan;
else
%rt = respstop - respstart; %trial RT
rt = respstop - objfliptime; % trial RT
end
% data.resp(t) = str2double(resp(1));
data.resp(t) = resp(1);
data.RT(t) = rt(1);
% calculate accuracy
if resp == header.disp{1,runNr}(t,3) %correct response
acc = 1;
else
acc = 0;
end
% save trial info to text file
fprintf(fid,formatString,data.dispStim(t,:),data.resp(t),data.RT(t),acc);
end
clear t
while (GetSecs-startTime) <= par.disp.nTotalTrials*par.disp.trialTime
end
WaitSecs(par.bookendFixTime)
% while GetSecs() < scannerStart + 240 % change in other disps and assign parameter
% end
% 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('Detection task: Run %d completed!',runNr));
disp('---------------');
disp(sprintf('NaNs: %d',sum(isnan(data.resp))));
disp(sprintf('Acc: %0.3f',mean(data.resp==data.correctResp)));
disp(sprintf('Acc excl NaNs: %0.3f', mean(data.correctResp(~isnan(data.resp))==data.resp(~isnan(data.resp)))));
disp('---------------');