-
Notifications
You must be signed in to change notification settings - Fork 1
/
LD_dynamic_MAE_fulltest_v14.m
604 lines (495 loc) · 26.1 KB
/
LD_dynamic_MAE_fulltest_v14.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
function LD_dynamic_MAE_fulltest_v14(subject, session, debug)
%In this version, we add multiple velocities
% subject = 'sub-01';
% session = 1;
% debug = 0;
ex.version = 'v13';
%%%% resolution
if debug == 1
ex.screenWidth = 40; % in cm; %laptop=27.5,office=43, %19=%T3b, miniHelm=39;
ex.viewingDist = 46; % in cm; 3Tb/office=43, miniHelm=57;
ex.resolution = SetResolution(max(Screen('Screens')),1280,1024,85); % laptop 1920,1080/ 2880, 1800 ,0
ex.gammaCorrection = 0; % make sure this = 1 when you're at the scanner!
else
ex.screenWidth = 40; % in cm; % 16 in eye tracking room 425%laptop=27.5,office=43, %19=%T3b, miniHelm=39;
ex.viewingDist = 46; % in cm; %23 in eye tracking room 425 3Tb/office=43, miniHelm=57;
ex.resolution = SetResolution(max(Screen('Screens')),1280,1024,85); % ET room 1600,900,60
ex.gammaCorrection = 1; % make sure this = 1 when you're at the scanner!
end
%%%% keyboard
[keyboardIndices, productNames, ~] = GetKeyboardIndices ;
deviceNumber = keyboardIndices(1);
responseKeys = zeros(1,256);
responseKeys(KbName('LeftArrow'))=1; % button box
responseKeys(KbName('RightArrow'))=1; % button box
responseKeys(KbName('DownArrow'))=1; % button box 3
Screen('Preference', 'SkipSyncTests', 0);
% ex.scanNum = input('Scan number :');
ex.runNum = input('Run number :');
%%% basic naming set-up
ex.subject = subject;
ex.session = session;
%%%% set-up rand
ex.startTime = clock;
rng(sum(100*ex.startTime));
ex.rand = rng;
%%%% files and things
ex.root = pwd;
ex.date = datestr(now,30);
%%%% 2D sine wave grating inducers properties
ex.stim.spatialFreqDeg = 0.5/2; % cycles per degree of visual angle
ex.stim.orientation = [90]; %[90 180]; % in degrees
ex.stim.gaborHDeg = 6; % in degrees of visual angle
ex.stim.gaborWDeg = 12;
ex.stim.gapSizeDeg = 2.6;
ex.stim.distFromFixDeg = (ex.stim.gapSizeDeg+ex.stim.gaborHDeg)/2; %each grating center deg horizontal away from fixation (grating center 6 deg away)
ex.stim.backgroundLum = [60 60 60];
ex.stim.contrast = [0.0375];
ex.stim.contrastOffset = [(ex.stim.backgroundLum(1)./255)./(1-ex.stim.contrast(1))];%+ex.stim.contrast/2;
ex.stim.luminanceRange = 2*ex.stim.contrast.*ex.stim.contrastOffset;
ex.stim.contrastMultiplicator = ex.stim.luminanceRange./2; % for sine wave
ex.stim.maxLum = 255*(ex.stim.contrastOffset+ex.stim.contrastMultiplicator);
ex.stim.minLum = 255*(ex.stim.contrastOffset-ex.stim.contrastMultiplicator);
ex.stim.contrast = (ex.stim.maxLum-ex.stim.minLum)./(ex.stim.maxLum+ex.stim.minLum);
%%%% sine wave grating timing (within block scale)
ex.initialFixation = 3; % in seconds
ex.finalFixation = 2; % in seconds
ex.blockLength = 8; %ex.trialFixation+ ceil(ex.stimDur*ex.stimsPerBlock); % in seconds
ex.testLength = 1;% in seconds
ex.ITI1 = 3;
ex.ITI2 = 0.5;% in seconds
ex.ITI3 = 9; %+9 sec break every 10 trial
ex.flipsPerSec = 60; % 60; % number of phase changes we want from the visual stimulus, and thus the number of times we want to change visual stimulation on the screen
ex.flipWin = 1/ex.flipsPerSec; % in seconds then actually in 1 sec the stimuli will change 12 times
ex.stim.cycPerSec = 2;%1.5; %drifting speed in cycles of grating per sec
ex.stim.motionRate = 360*ex.stim.cycPerSec; %drifting speed in degrees of visual angle per sec
ex.stim.dphase = ex.stim.motionRate/ex.flipsPerSec; %degrees per flip
%%%% Test stimulus: counterphasing grating
ex.test.contrastOffset = ex.stim.backgroundLum(1,1)./255;%
ex.test.contrast = 0.0375;
ex.test.luminanceRange = 2*ex.test.contrast*ex.test.contrastOffset;%0.1; %linspace(0.01,0.20,10);%[0.05, 0.10, 0.15]; % in %, maybe?? %here the number of stimulus contrast levels is the number of different conditions
ex.test.contrastMultiplicator = ex.test.luminanceRange/2; % for sine wave 0.5 = 100% contrast, 0.2 = 40%
ex.test.gaborHDeg = (2/3)*ex.stim.gapSizeDeg; % in degrees of visual angle
ex.test.gaborWDeg = ex.stim.gaborWDeg;%4;%
ex.test.distFromFixDeg = 0; % in degrees of visual angle, grating center 2 deg away (edge 1 deg away)
ex.test.cycPerSec = ex.stim.cycPerSec;
ex.testDur = (1/ex.test.cycPerSec); % in seconds. 1.77 sec refers to sine wave grating 1.77 = 2cycles/1.13cyc.sec-1 mutiplied by 2 for back and forth
%%%% Fixation
ex.fixSizeDeg = .2; % in degrees, the size of the biggest white dot in the fixation
ex.bigFixSizeDeg = 0.5;
ex.outerFixPixels = 2; % in pixels, the black ring around fixation
%%%% Nonius lines
ex.lineHdeg = 0.45;
ex.lineWdeg = 0.06;
%%% horizontal line
ex.horiLineWdeg = 0.45;
%%%% conditions & layout (across blocks scale)
ex.conds = {'vLowContFullRight','vLowContFullLeft'};
ex.repsPerRun = [12 12]; % repetitions of each condition per run
condIdx = 1:length(ex.conds); %[1,4,7]; %conditions we are interested to keep
ex.conds = ex.conds(condIdx);
ex.repsPerRun = ex.repsPerRun(condIdx);
ex.numConds = length(ex.conds);
% with line of code below we will have 1 condition per block, randomized. we might need to change that
% later, to have the conditions randomized within each block
ex.numBlocks = sum(ex.repsPerRun);
ex.condShuffle = [];
for i =1:ex.repsPerRun(1)
ex.condShuffle = [ex.condShuffle, Shuffle(1:length(ex.conds))];
end
ex.totalTime = [];
for t =1:length(ex.blockLength) %there is a different block length for every drifting speed
if t == 1
ex.totalTime = sum([ex.totalTime, ex.initialFixation + (ex.numBlocks/length(ex.blockLength) * (ex.blockLength(t) + ex.testLength))]);
elseif t <length(ex.blockLength) && t > 1
ex.totalTime = sum([ex.totalTime, (ex.numBlocks/length(ex.blockLength) * (ex.blockLength(t) + ex.testLength))]);
elseif t == length(ex.blockLength)
ex.totalTime = sum([ex.totalTime, ((ex.numBlocks/length(ex.blockLength)-1) * (ex.blockLength(t) + ex.testLength)) + ex.blockLength(t) + ex.finalFixation]);
end
end
ex.allFlips = (0:ex.flipWin:ex.totalTime);
ex.allFlips = ex.allFlips(1:end-1);
ex.trialFlips = (0:ex.flipWin:ex.blockLength(1)+ex.testLength);
ex.trialFlips = ex.trialFlips(1:end-1);
%%%% screen
ex.backgroundColor = [127.5 127.5 127.5];%[108.3760 108.3760 108.3760];%; % color based on minimum gating luminance
ex.fontSize = 26;
%% %%%%%%%%%%%%%%%%%
% timing model %
%%%%%%%%%%%%%%%%%
ex.onSecs = [ones(1,ex.blockLength(t)) zeros(1,ex.testLength)];
ex.longFormBlocks = Expand(ex.onSecs,ex.flipsPerSec,1); %1 when block, 0 when between block
length(ex.longFormBlocks)
ex.stimOnSecs = [ones(1,ex.blockLength(t)) zeros(1,ex.testLength)]; %zeros(1,ex.trialFixation)
ex.longFormStimOnSecs = Expand(ex.stimOnSecs,ex.flipsPerSec,1); %1 when stim on, 0 when fixation or between blocks
% %% create the timing model of stimulus conditions for this particular run
% clear i
for i =1:ex.numConds
conditions(i).name = ex.conds(i);
conditions(i).startTimes = [];
end
%%
%%%%%%%%%%%%%%%
% open screen %
%%%%%%%%%%%%%%%
HideCursor;
Priority(9);
%Priority(0);
%%%% open screen
screen=max(Screen('Screens'));
if debug
[w, rect]=Screen('OpenWindow',screen,ex.backgroundColor,[],[],[],[],[],kPsychNeed32BPCFloat);
%[w, rect]=Screen('OpenWindow',screen,ex.backgroundColor,[100 100 600 400],[],[],[],[],kPsychNeed32BPCFloat); %might need to switch 900 and 600 by 1600 and 1200 for room 425
else
%[w, rect]=Screen('OpenWindow',screen,ex.backgroundColor,[100 100 600 400],[],[],[],[],kPsychNeed32BPCFloat); %might need to switch 900 and 600 by 1600 and 1200 for room 425
[w, rect]=Screen('OpenWindow',screen,ex.backgroundColor,[],[],[],[],[],kPsychNeed32BPCFloat); %might need to switch 900 and 600 by 1600 and 1200 for room 425
end
Screen(w, 'TextSize', ex.fontSize);
Screen('BlendFunction', w, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); % Set up alpha-blending for smooth (anti-aliased) lines
%gamma correction, file prepared for room 417
if ex.gammaCorrection
% Gamma correction (run phase2_photometry.mat in 417C computer, get gamma
% table)
load("phase2_photometry.mat");
Screen('LoadNormalizedGammaTable', screen, inverseCLUT);
end
%%%% timing optimization
frameInt = Screen('GetFlipInterval',w);
slack = frameInt/2;
frameRate = 1/frameInt;%Screen('NominalFrameRate',w);
%
flipTimes = [0:1/ex.flipsPerSec:1/(ex.stim.cycPerSec)]; %multiply frameInt by 60/12 = 5 to flip the image every 5 frames
ex.stim.flipTimes = flipTimes(1:length(flipTimes)-1);
nconds = length(ex.stim.contrastMultiplicator);
ex.stim.phases = nan(nconds, length(ex.stim.flipTimes));
clear r l
for l =1:nconds
ex.stim.phases(l,:) = ((1:length(ex.stim.flipTimes))-1)*ex.stim.dphase;%(ex.stim.oscillation1(c,l,r,:).*180*ex.stim.cycles(1)+ ex.stim.oscillation2(c,l,r,:).*180*ex.stim.cycles(2))/2 + ex.stim.spatialPhase; %./ex.stimDur-2*pi*flipTimes./ex.stimDur make it oscillatory
end
flipTimesTest = [0:1/ex.flipsPerSec:ex.testLength(1)]; %multiply frameInt by 60/12 = 5 to flip the image every 5 frames
ex.test.flipTimes = flipTimesTest(1:length(flipTimesTest)-1);
ex.test.tempPhase1 = 0;% rand(1,1)*2*pi;
halfPeriod1 = linspace(-1,1,ex.testDur*ex.flipsPerSec/2+1);
halfPeriod2 = linspace(1,-1,ex.testDur*ex.flipsPerSec/2+1);
ex.test.oscillation1 = [halfPeriod1(1:end-1),halfPeriod2(1:end-1)];%cos(2*pi*(1/ex.testDur(1))*ex.test.flipTimes+ex.test.tempPhase1);
ex.test.spatialPhase1 = 0;
ex.test.tmpphase = ex.test.oscillation1.*90+ex.test.spatialPhase1; %./ex.stimDur-2*pi*flipTimes./ex.stimDur make it oscillatory
%reasonning behind the calculation of ex.stim.phases:
%GOAL: render the back and forth of grating drifts oscillatory in time instead
%of linear to smooth the signal phase shifts at the time of drift direction
%reversal
%1) The length (x) of grating shift after each flip is the variable that will have to be oscillatory
%over time
%2)oscillatory signal formula is x(t) = A*cos(w*t+phase) (x is the oscillatory signal, w is the angular
%frequency in rad/s, t is the time in s, phase is a phase shift in rad at time t (here 0),
%A is the amplitude of the signal
%3)Here the angular frequency is calculated based on the idea of one
%oscillation per stimulus. Considering a stimulus as 1 cycle forward and 1 cycle backwards(definition might change based on how many cycles are desired per stimulus), one oscillation will represent 2 cycles =ex.stim.cycPerSec*ex.stimDur)
% thus the temporal frequency of the oscillation should be 1/ex.stimDur
% (this is w)
%%4) We multiply w by t. Here the resolution of the signal is given by the number of flips per second, so the duration is given by flipTimes.
%Finally, we multiply by 2*pi for the units to be in radian.
%5)Then we subtract the phase at each flip at time t which here is 0.
%6)The amplitude of cos() varies between -1 and +1. Here we want our
%displacement (grating shift) in degrees with a displacement of 360? (of the spatial grating) every half temporal oscillation so we multiply by 180 for the range to be [-180;+180].
%7) we multiply by the number of cycles desired to drift over one lap
%(ex.stim.cycles).
%%%% scale the stim params for the screen
ex.ppd = pi* rect(3) / (atan(ex.screenWidth/ex.viewingDist/2)) / 360;
ex.gaborHeight = round(ex.stim.gaborHDeg*ex.ppd); % in pixels, the size of our objects
ex.gaborWidth = round(ex.stim.gaborWDeg*ex.ppd); % in pixels, the size of our objects
ex.rawGaborHeight = ex.gaborHeight;
ex.rawGaborWidth = ex.gaborWidth;
ex.stim.distFromFix = round(ex.stim.distFromFixDeg*ex.ppd);
ex.stim.gapSize = round(ex.stim.gapSizeDeg*ex.ppd);
%%%% scale the test params for the screen
ex.probeHeight = round(ex.test.gaborHDeg*ex.ppd); % in pixels, the size of our objects
ex.probeWidth = round(ex.test.gaborWDeg*ex.ppd); % in pixels, the size of our objects
ex.rawProbeHeight = ex.probeHeight*1;
ex.rawProbeWidth = ex.probeWidth*1;
ex.test.distFromFix = round(ex.test.distFromFixDeg*ex.ppd);
%%%scale the fixation params for the screen
ex.fixSize = round(ex.fixSizeDeg*ex.ppd);
ex.bigFixSize = round(ex.bigFixSizeDeg*ex.ppd);
ex.lineW = round(ex.lineWdeg*ex.ppd);
ex.lineH = round(ex.lineHdeg*ex.ppd);
ex.horiLineW = round(ex.horiLineWdeg*ex.ppd);
%% Create sinewave grating frames saved for each repetition and each condition
ex.rectSWave = nan(ex.rawGaborHeight,ex.rawGaborWidth,length(ex.test.oscillation1),nconds);
ex.rectSWaveID = nan(length(ex.test.oscillation1),nconds);
clear c r
for l = 1:length(ex.stim.contrastMultiplicator)
for f =1:length(ex.test.oscillation1)
phase = ex.stim.phases(l,f);
ex.rectSWave(:,:,f,l) = makeSineGrating(ex.rawGaborHeight,ex.rawGaborWidth,ex.stim.spatialFreqDeg,...
ex.stim.orientation,phase,ex.stim.contrastOffset(l),ex.stim.contrastMultiplicator(l),...
ex.ppd);
ex.rectSWaveID(f,l) = Screen('MakeTexture', w, squeeze(ex.rectSWave(:,:,f,l)));
end
end
% for i =1:60
% figure();
% imshow(squeeze(ex.rectSWave(:,:,i,1))/255)
% end
%check luminances ranges
% minval = min(squeeze(ex.rectSWave(1,1,1,:,:)),[],'all');
% maxval = max(squeeze(ex.rectSWave(1,1,1,:,:)),[],'all');
%% Create "Full" grating condition
ex.fullSWave = nan(ex.stim.gapSize,ex.rawGaborWidth,length(ex.test.oscillation1),nconds);
ex.fullSWaveID = nan(length(ex.test.oscillation1),nconds);
clear c r
for l = 1:length(ex.stim.contrastMultiplicator)
for f =1:length(ex.test.oscillation1)
phase = ex.stim.phases(l,f);
ex.fullSWave(:,:,f,l) = makeSineGrating(ex.stim.gapSize,ex.rawGaborWidth,ex.stim.spatialFreqDeg,...
ex.stim.orientation,phase,ex.stim.contrastOffset(l),ex.stim.contrastMultiplicator(l),...
ex.ppd);
ex.fullSWaveID(f,l) = Screen('MakeTexture', w, squeeze(ex.fullSWave(:,:,f,l)));
end
end
%% create dynamic grating image as a test for other eye
% phase = repmat((0:360/60:360-360/60),1,ex.testLength);
phases1 = ex.test.tmpphase;%squeeze(ex.stim.phases(c,l,r,:)).*360.*ex.rawProbeHeight./ex.ppd;%
spphase = 0;
ex.testStim = nan(ex.rawProbeHeight,ex.rawProbeWidth,ex.testLength*ex.flipsPerSec,length(ex.test.contrastOffset));
for l = 1:length(ex.test.contrastOffset)
for n = 1:length(phases1)
ex.testStim(:,:,n,l) = makeCounterPhasingGrating(ex.rawProbeHeight,ex.rawProbeWidth,ex.stim.spatialFreqDeg,...
ex.stim.orientation,spphase,ex.test.contrastOffset(l),ex.test.contrastMultiplicator(l), ex.test.tmpphase(n),...
ex.ppd);
ex.testStimID(n,l) = Screen('MakeTexture', w, squeeze(ex.testStim(:,:,n,l)));
end
end
% check luminances ranges
% min(squeeze(ex.testStim(:,:,1)),[],'all')
% max(squeeze(ex.testStim(:,:,1)),[],'all')
% % repmat(max(max(squeeze(ex.lcSWave(1,:,:)),[],1)),1)
% figure();
% plot(squeeze(ex.testStim(200,41,:)))
% for i =1:60
% figure();
% imshow(squeeze(ex.testStim(:,:,1))/255)
% end
% figure();
% imshow(squeeze(ex.lcSWave(2,:,:))/255)
%% Sine wave gratings locations
xc = rect(3)/2; % rect and center, with the flexibility to resize & shift center - change vars to zero if not used.
yc = rect(4)/2; %+e.vertOffset;
%left grating
xL = xc;
% grating y locations
yL = yc-ex.stim.distFromFix;
%right grating
xR = xc;
yR = yc+ex.stim.distFromFix;
%% Fixation cross coordinates
%%%horizontal line
xhorilinel = xc - ex.horiLineW/2;
xhoriliner = xc + ex.horiLineW/2;
yhoriline = yc;
%%%vertical line
xline = xc;
ylineBot = yc + ex.lineH/2;
ylineTop = yc - ex.lineH/2;
%% %%%% initial window - wait for backtick
DrawFormattedText(w,'Fixate the fixation dot as best as you can. \n\n After each drifting stimulus disappears, \n\n report your initial percept using the following keys: \n\n press the left arrow if the test stimulus appears to move leftward, \n\n the right arrow if the test stimulus appears to move rightward, \n\n or down arrow if the motion direction is ambiguous \n\n Press Space to start'... % : '...
,xc/2, yc/2,[0 0 0]);
Screen(w, 'Flip', 0);
%WaitSecs(2);
KbTriggerWait(KbName('Space'), deviceNumber);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% experiment %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% START task TASK/FLIPPING
clear l n
n = 1;
blockCnt = 1;
cnt = 0; %trial count
ex.responses = [];
ex.responseTimes=[];
ex.resp = [];
% onOffs = [diff([0 ex.longFormBlocks])];
% bLength = ex.blockLength(1);
ex.flipTime = nan(length(ex.trialFlips),length(ex.condShuffle));
KbQueueCreate(deviceNumber,responseKeys);
%%% initial fixation
if n == 1 && blockCnt == 1 %for first block
ex.tasktstart = clock;
ex.startRun = GetSecs();
Screen('FillRect', w, ex.backgroundColor);
% Screen('DrawDots', w, [xc yc], ex.fixSize, [255 255 255], [], 2);
Screen('DrawLines', w, [xline, xline; ylineBot, ylineTop], ex.lineW, [30 30 30]);
Screen('DrawLines', w, [xhorilinel, xhoriliner; yhoriline, yhoriline],ex.lineW, [30 30 30]);
Screen(w, 'Flip', 0);
WaitSecs(ex.initialFixation);
end
%%% Launch the task
for c = 1:length(ex.condShuffle)
condNum = ex.condShuffle(c);
condName = conditions(condNum).name{:};
f = 1;
l = ceil(condNum/2); % indices for each contrast level
%l = condNum;
%flip through the block and following between block time
while n <= length(ex.longFormBlocks) %true
KbQueueStart(); % response time
ex.longFormBlocks(n)
%%%% draw sine wave grating stimulus %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Screen('FillRect', w, ex.stim.backgroundLum);
if nnz(find(ex.longFormStimOnSecs(n)))
ex.rectLRect = CenterRectOnPoint([0 0 ex.rawGaborWidth ex.rawGaborHeight],xL,yL);
ex.rectRRect = CenterRectOnPoint([0 0 ex.rawGaborWidth ex.rawGaborHeight],xR,yR);
ex.fullRect = CenterRectOnPoint([0 0 ex.rawGaborWidth ex.stim.gapSize],xc,yc);
% stim
if contains(condName, 'Right')
Screen('DrawTexture', w, ex.rectSWaveID(f,l),[],ex.rectLRect);
Screen('DrawTexture', w, ex.rectSWaveID(f,l),[],ex.rectRRect);
Screen('DrawTexture', w, ex.fullSWaveID(f,l),[],ex.fullRect);
elseif contains(condName, 'Left')
Screen('DrawTexture', w, ex.rectSWaveID(end-(f-1),l),[],ex.rectLRect);
Screen('DrawTexture', w, ex.rectSWaveID(end-(f-1),l),[],ex.rectRRect);
Screen('DrawTexture', w, ex.fullSWaveID(end-(f-1),l),[],ex.fullRect);
end
% Screen('DrawTexture',w,ph1LPaperture);
end
% Screen('DrawDots', w, [xc yc+ex.yoffset], ex.fixSize, [255 255 255], [], 2);
Screen('DrawLines', w, [xline, xline; ylineBot, ylineTop], ex.lineW, [30 30 30]);
Screen('DrawLines', w, [xhorilinel, xhoriliner; yhoriline, yhoriline],ex.lineW, [30 30 30]);
%% Draw Test stimulus on the screen
if length(ex.longFormBlocks(1:n))/60 >= ex.blockLength && length(ex.longFormBlocks(1:n))/60 < ex.blockLength+ex.testLength%(cnt/2 == 1 && GetSecs-time >= 0) && c ~= length(ex.condShuffle)
ex.lcLRect = CenterRectOnPoint([0 0 ex.rawProbeWidth ex.rawProbeHeight],xc,yc);
% test stim
Screen('DrawTexture', w, ex.testStimID(f),[],ex.lcLRect,[],[],[]);
%Fixation
% Screen('DrawDots', w, [xc yc], ex.fixSize, [255 255 255], [], 2);
Screen('DrawLines', w, [xline, xline; ylineBot, ylineTop], ex.lineW, [30 30 30]);
Screen('DrawLines', w, [xhorilinel, xhoriliner; yhoriline, yhoriline],ex.lineW, [30 30 30]);
end
%%%%%%%%%%% FLIP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if n == 1
[VBLT, ex.startTrial, FlipT, missed] = Screen(w, 'Flip', 0);%[VBLTimestamp StimulusOnsetTime FlipTimestamp Missed] = Screen('Flip', windowPtr [, when] [, dontclear]...
flipTimes = ex.startTrial;
else
[VBLT,flipTime, FlipT, missed] = Screen(w, 'Flip',ex.startTrial + ex.trialFlips(n) - slack); %, %%% ex.flipTime(n,c)
flipTimes = [flipTimes, flipTime];
end
if length(ex.longFormBlocks(1:n))/60 == ex.blockLength+ex.testLength && c ~= length(ex.condShuffle) %(cnt/2 == 1 && GetSecs-time >= ex.blockLength+ex.testLength) && c ~= length(ex.condShuffle)
WaitSecs(ex.ITI1);
if mod(c,10)
DrawFormattedText(w,'Press Space whenever \n\n you feel ready \n\n for the next trial',(4/5)*xc, yc/2,[0 0 0]); %left instruction
else
DrawFormattedText(w,sprintf('Press Space whenever \n\n you feel ready \n\n for trial %d / %d', c+1, length(ex.condShuffle)),(4/5)*xc, yc/2,[0 0 0]); %left instruction
end
%% Fixation
% Screen('DrawDots', w, [xc yc], ex.fixSize, [255 255 255], [], 2);
Screen('DrawLines', w, [xline, xline; ylineBot, ylineTop], ex.lineW, [30 30 30]);
Screen('DrawLines', w, [xhorilinel, xhoriliner; yhoriline, yhoriline],ex.lineW, [30 30 30]);
Screen(w, 'Flip', 0);
[~,~,~] =KbWait(deviceNumber,2);
Screen('FillRect', w, ex.stim.backgroundLum);
% Screen('DrawDots', w, [xc yc], ex.fixSize, [255 255 255], [], 2);
Screen('DrawLines', w, [xline, xline; ylineBot, ylineTop], ex.lineW, [30 30 30]);
Screen('DrawLines', w, [xhorilinel, xhoriliner; yhoriline, yhoriline],ex.lineW, [30 30 30]);
Screen(w, 'Flip', 0);
WaitSecs(ex.ITI2);
cnt = cnt+1;
end
if mod(cnt,10) == 0 && cnt >=1
WaitSecs(ex.ITI3);
cnt = 0;
end
KbQueueStop();
[pressed, firstPress]= KbQueueCheck();
if (pressed == 1) && ((firstPress(KbName('RightArrow')) > 0 || firstPress( KbName('LeftArrow')) > 0)||(firstPress(KbName('DownArrow')) > 0)) %%
ex.responses = [ex.responses, 1];
if (firstPress(KbName('RightArrow')) > 0)
ex.resp = [ex.resp, 1];
ex.responseTimes = [ex.responseTimes, firstPress(KbName('RightArrow')) - ex.startRun];
elseif (firstPress(KbName('LeftArrow')) > 0)
ex.resp = [ex.resp, 2];
ex.responseTimes = [ex.responseTimes, firstPress(KbName('LeftArrow')) - ex.startRun];
elseif (firstPress(KbName('DownArrow')) > 0)
ex.resp = [ex.resp, 3];
ex.responseTimes = [ex.responseTimes, firstPress(KbName('DownArrow')) - ex.startRun];
end
pressed = 0;
end
%%%% refresh queue for next character
KbQueueFlush();
f = f+1;
n = n+1;
if f == ex.flipsPerSec/ex.stim.cycPerSec
f = 1;
end
end
ex.flipTime(:,c) = flipTimes;
n = 1;
end
%%%%%%%%%%%%%%%%%%
% done! wrap up %
%%%%%%%%%%%%%%%%%%
ex.runTime = GetSecs - ex.startRun;
savedir = fullfile(ex.root,'data/dyn_MAE',sprintf('%s/%s_%s/',ex.version,subject,ex.version));
if ~exist(savedir); mkdir(savedir); end
savename = fullfile(savedir, strcat(sprintf('/%s_dyn_MAE_fulltest_%s_date%s_fix',subject,ex.version,num2str(ex.date)), '.mat'));
%save(savename,'ex');
save(savename,'ex','-v7.3')
clear c cnt condNum
teststarts = ex.flipTime(ex.blockLength*ex.flipsPerSec,1:end)-ex.startRun;
testends = ex.flipTime(end,1:end)-ex.startRun+ex.ITI1;
responseType = [1, 2, 3];
condNums = ex.condShuffle;
conditions = ex.conds;
% respFreq = nan(length(responseType), condNums);
respDat = zeros(length(responseType), max(ex.repsPerRun), length(conditions)); %response time data better organized
cnt = zeros(length(condNums),1);
for c =1:length(condNums)
condNum = condNums(c);
cnt(condNum) = cnt(condNum)+1; %counting rep number
tstart = teststarts(c);
tend = testends(c);
respTimes = ex.responseTimes(ex.responseTimes > tstart & ex.responseTimes < tend);
resps = ex.resp(ex.responseTimes > tstart & ex.responseTimes < tend);
if ~isempty(resps)
respDat(resps(end), cnt(condNum), condNum) = respDat(resps(end), cnt(condNum), condNum)+1; %resps(end) to take the last response in case the participant corrected a mistake
end
end
respFreq = squeeze(sum(respDat,2));
same_diff = zeros(length(responseType),length(conditions)/2);
for c =1:length(conditions)
for t = 1:size(respFreq,1)
%same direction
if contains(conditions(c), 'Right') && t == 1
condcnt = ceil(c/2);
same_diff(1,condcnt) = same_diff(1,condcnt)+ respFreq(t,c);
elseif contains(conditions(c), 'Left') && t == 2
condcnt = ceil(c/2);
same_diff(1,condcnt) = same_diff(1,condcnt)+ respFreq(t,c);
%ambiguous direction
elseif t == 3
condcnt = ceil(c/2);
same_diff(2,condcnt) = same_diff(2,condcnt)+ respFreq(t,c);
%opposite direction
elseif contains(conditions(c), 'Right') && t == 2
condcnt = ceil(c/2);
same_diff(3,condcnt) = same_diff(3,condcnt)+ respFreq(t,c);
elseif contains(conditions(c), 'Left') && t == 1
condcnt = ceil(c/2);
same_diff(3,condcnt) = same_diff(3,condcnt)+ respFreq(t,c);
end
end
end
%bias
score=[0; 0.5;1]; %score each percept type: 0 = same, 0.5 = ambiguous, 1 = opposite
bias = same_diff.*score;
percentBias = nan(size(bias,2),1);
for c =1:size(bias,2)
percentBias(c) = 100*sum(bias(:,c))/sum(same_diff(:,c));
fprintf('Percent Bias %d\n', percentBias(c));
end
ShowCursor;
Screen('Close');
Screen('CloseAll');
fclose all;