-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLD_phantom_oscill_v16_03092023.m
553 lines (475 loc) · 27.9 KB
/
LD_phantom_oscill_v16_03092023.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
function LD_phantom_oscill_v16_03092023(subject, session, debug)
%In this version, we add multiple velocities
% subject = 'Dave';
% session = 1;
% debug = 1;
ex.version = 'v16';
global EyeData rect w xc yc eye_used
%%%% resolution
if debug == 1
% eyetracking on (1) or off (0)
ET = 0;
ex.screenWidth = 53.1; % in cm; %laptop=27.5,office=43, %19=%T3b, miniHelm=39;
ex.viewingDist = 53.5; % in cm; 3Tb/office=43, miniHelm=57;
ex.resolution = SetResolution(max(Screen('Screens')),1600,900,60); % laptop 1920,1080/ 2880, 1800 ,0
ex.gammaCorrection = 0; % make sure this = 1 when you're at the scanner!
else
ET = 1;
ex.screenWidth = 53.1; % in cm; % 16 in eye tracking room 425%laptop=27.5,office=43, %19=%T3b, miniHelm=39;
ex.viewingDist = 53.5; % in cm; %23 in eye tracking room 425 3Tb/office=43, miniHelm=57;
ex.resolution = SetResolution(max(Screen('Screens')),1600,900,60); % scanner 1600,900,60
ex.gammaCorrection = 1; % make sure this = 1 when you're at the scanner!
end
%%%% keyboard
[keyboardIndices, productNames, ~] = GetKeyboardIndices ;
deviceNumber = keyboardIndices(1);
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; %0.286; % cycles per degree of visual angle
ex.stim.contrast = 0.3 ; % in %, maybe??
ex.stim.orientation = [90]; %[90 180]; % in degrees
ex.stim.degFromFix = .6; % in degrees of visual angle
ex.stim.gaborHDeg = 8; % in degrees of visual angle
ex.stim.gaborWDeg = 8;
%ex.stim.rectGaborWDeg = 8;
ex.stim.contrast = 0.15;%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.stim.contrastMultiplicator = ex.stim.contrast/2; % for sine wave 0.5 = 100% contrast, 0.2 = 40%
ex.stim.contrastOffset = .5; %.5 .5 0]; % for procedural gabor
ex.stim.cycPerSec = [1.13*1/2,1.13*3/2]; % try multiple speeds
ex.stim.motionRate = ex.stim.cycPerSec.*360; % 1.13 cycles per second = 360 deg of phase *1.13 per sec
ex.stim.cycles =[1, 3]; %number of cycles shifted per lap (modified to half the number of cycles per lap)
%%%%additional properties for single grating
ex.match.contrastMults = mean([0.0072 0.0062]);
ex.match.contrastOffset = nan(1,length(ex.match.contrastMults));
for i =1:length(ex.match.contrastMults)
ex.match.contrastOffset(i) = ex.stim.contrastOffset-ex.stim.contrastMultiplicator-ex.match.contrastMults(i);
end
%%%% sine wave grating timing (within block scale)
ex.initialFixation = 6; % in seconds
ex.finalFixation = 2; % in seconds
ex.trialFixation = 1; % in seconds
ex.stimDur = (ex.stim.cycles./ex.stim.cycPerSec)*2; % in seconds. 1.77 sec refers to sine wave grating 1.77 = 2cycles/1.13cyc.sec-1 mutiplied by 2 for back and forth
ex.stimsPerBlock = 4.5; % number of back-and-forth laps of the stimulus drift
ex.blockLength = ex.trialFixation+ ceil(ex.stimDur*ex.stimsPerBlock); % in seconds
ex.betweenBlocks = 2; % in seconds
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
%e.numBlocks = 12; % 6 for single and 6 for pair...
%%%% conditions & layout (across blocks scale)
ex.conds = {'MeanbgRedDotVel1','RectMinbgSingleVel1'...
'RectMinbgSp8Vel1', 'RectMeanbgSp8Vel1',...
'MeanbgRedDotVel2','RectMinbgSingleVel2','RectMinbgSp8Vel2', 'RectMeanbgSp8Vel2',...
}; %...
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.repsPerRun = 20; % repetitions of each condition per run
ex.numBlocks = ex.numConds*ex.repsPerRun;
%condShuffle1 = Shuffle(repmat([1:ex.numConds/2],1,ex.repsPerRun)); % %e.stimsPerBlock make same number of blocks with each condition, randomize order
%condShuffle2 = Shuffle(repmat([ex.numConds/2+1:ex.numConds],1,ex.repsPerRun));
%condShuffle3 = Shuffle(repmat([ex.numConds*2/(length(ex.conds)/2)+1:ex.numConds],1,ex.repsPerRun));
%ex.condShuffle = [condShuffle1 condShuffle2];
ex.condShuffle = Shuffle(repmat([1:ex.numConds],1,ex.repsPerRun));
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.betweenBlocks))]);
elseif t <length(ex.blockLength) && t > 1
ex.totalTime = sum([ex.totalTime, (ex.numBlocks/length(ex.blockLength) * (ex.blockLength(t) + ex.betweenBlocks))]);
elseif t == length(ex.blockLength)
ex.totalTime = sum([ex.totalTime, ((ex.numBlocks/length(ex.blockLength)-1) * (ex.blockLength(t) + ex.betweenBlocks)) + 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.betweenBlocks);
ex.trialFlips = ex.trialFlips(1:end-1);
%%%% fixation
ex.fixSizeDeg = .5; % in degrees, the size of the biggest white dot in the fixation
%%%% screen
ex.backgroundColor = [127 127 127];%[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.betweenBlocks)];
ex.longFormBlocks = Expand(ex.onSecs,ex.flipsPerSec,1); %1 when block, 0 when between block
length(ex.longFormBlocks)
% %% 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);
%gamma correction, file prepared for room 425
if ex.gammaCorrection
%load gamma correction file
load('/Users/tonglab/Desktop/monitor_calibration/425dell_22-12-09/phase2_photometry_22-12-09.mat');
Screen('LoadNormalizedGammaTable', w, inverseCLUT);
end
%%%% timing optimization
frameInt = Screen('GetFlipInterval',w);
slack = frameInt/2;
frameRate = 1/frameInt;%Screen('NominalFrameRate',w);
clear t
for t =1:length(ex.stimDur)
flipTimes = [0:frameInt*frameRate/ex.flipsPerSec:ex.stimDur(t)]; %multiply frameInt by 60/12 = 5 to flip the image every 5 frames
flipTimes = flipTimes(1:length(flipTimes)-1);
flipt =sprintf('flipTimes%d',t);
ex.(flipt) = flipTimes;
%%% Still red dot for 1 sec before trial starts
ex.stillDotPhase = zeros(1,ex.flipsPerSec*ex.trialFixation);
%%% Stimulus phases
% (in a linear/triangle scenario) ex.stim.dphasePerFlip = ex.stim.motionRate*frameInt * frameRate/ex.flipsPerSec; %degrees per flip, here we multiply by 60/12 =5 to move the phase 5 times more after each flip since we have 5 times less flips than frame refresh
ex.stim.tempPhase =pi; % pi/2; %this will make the oscillation start on the fast segment of the oscillation, with red dot on the center of the screen
oscillation = sprintf('oscillation%d',t);
ex.stim.(oscillation) = cos(2*pi*(1/ex.stimDur(t))*flipTimes+ex.stim.tempPhase);
ex.stim.spatialPhase = 90; %this makes the grating phase so that the center of the dark stripe is on the center of the screen
phases = sprintf('phases%d',t);
ex.stim.(phases) = ex.stim.(oscillation).*180*ex.stim.cycles(t)+ex.stim.spatialPhase; %./ex.stimDur-2*pi*flipTimes./ex.stimDur make it oscillatory
end
%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.fixSize = round(ex.fixSizeDeg*ex.ppd);
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.rectGaborWidth = round(ex.stim.gaborWDeg*2*ex.ppd);
%ex.driftSpeedDeg = ex.stim.cycPerSec/ex.stim.spatialFreqDeg;
%% create sine wave gratings and store all phase transitions in structure
%%% along with pointers
%rect
rectLWaveID = nan(length(ex.longFormBlocks),length(ex.stim.cycPerSec));
rectRWaveID = nan(length(ex.longFormBlocks),length(ex.stim.cycPerSec));
rectCWaveID = nan(length(ex.longFormBlocks),length(ex.stim.cycPerSec));
clear t
% for o =1:length(ex.stim.orientation)
for t =1:length(ex.stim.cycPerSec)
flipt =sprintf('flipTimes%d',t);
flipTimes = ex.(flipt);
%rect
rectLWave = sprintf('rectLWave%d',t);
rectRWave = sprintf('rectRWave%d',t);
rectCWave = sprintf('rectCWave%d',t);
ex.(rectLWave) = nan(length(flipTimes),ex.gaborHeight,ex.gaborWidth*2);
ex.(rectRWave) = nan(length(flipTimes),ex.gaborHeight,ex.gaborWidth*2);
ex.(rectCWave) = nan(length(flipTimes),ex.gaborHeight,ex.gaborWidth*2);
phaseNum = sprintf('phases%d',t);
phases = ex.stim.(phaseNum) ;
% clear tmprectLWaveID tmprectRWaveID
for f = 1:length(flipTimes)
LPhase = phases(f);
RPhase = phases(f);
CPhase = phases(f)-180;
%ih in pixels %iw in pixels %spatial freq in cycles per dva
%tilt/orientation of the grating in degrees %phase in degrees (not degrees of visual angle)
%contrast offset in percent %contrast multiplicator %ppd = 0 if freq already in cycles per stimulus
%background color (unused if the grating is not an annulus)
% rect
ex.(rectLWave)(f,:,:) = makeSineGrating(ex.gaborHeight,ex.gaborWidth*2,ex.stim.spatialFreqDeg,...
ex.stim.orientation,LPhase,ex.stim.contrastOffset(1),ex.stim.contrastMultiplicator,...
ex.ppd);
ex.(rectRWave)(f,:,:) = makeSineGrating(ex.gaborHeight,ex.gaborWidth*2,ex.stim.spatialFreqDeg,...
ex.stim.orientation,RPhase,ex.stim.contrastOffset(1),ex.stim.contrastMultiplicator,...
ex.ppd);
ex.(rectCWave)(f,:,:,t) = makeSineGrating(ex.gaborHeight,ex.gaborWidth*2,ex.stim.spatialFreqDeg,...
ex.stim.orientation,CPhase,ex.match.contrastOffset,ex.match.contrastMults,...
ex.ppd);
% figure();
% imshow(squeeze(ex.(rectRWave)(f,:,:,o))./max(squeeze(ex.(rectRWave)(f,:,:,o)),[],'all'));
%
tmprectLWaveID(f,1) = Screen('MakeTexture', w, squeeze(ex.(rectLWave)(f,:,:)));
tmprectRWaveID(f,1) = Screen('MakeTexture', w, squeeze(ex.(rectRWave)(f,:,:)));
tmprectCWaveID(f,t) = Screen('MakeTexture', w, squeeze(ex.(rectCWave)(f,:,:,t)));
end
%% extend stimulus matrix to include the same total number of flips as the whole experiment
rectLWaveID(:,t) = [zeros(length(ex.stillDotPhase),1); ...
repmat(tmprectLWaveID(:),floor((ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec/length(tmprectLWaveID(:))),1);...
tmprectLWaveID(1:mod((ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec,length(tmprectLWaveID(:))));...
zeros(ex.betweenBlocks*ex.flipsPerSec,1)];
rectRWaveID(:,t) = [zeros(length(ex.stillDotPhase),1);...
repmat(tmprectRWaveID(:),floor((ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec/length(tmprectRWaveID(:))),1);...
tmprectRWaveID(1:mod((ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec,length(tmprectRWaveID(:))));...
zeros(ex.betweenBlocks*ex.flipsPerSec,1)];
rectCWaveID(:,t) = [zeros(length(ex.stillDotPhase),1);...
repmat(tmprectCWaveID(:,t),floor((ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec/length(tmprectCWaveID(:,t))),1);...
tmprectCWaveID(1:mod((ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec,length(tmprectCWaveID(:,t))),t);...
zeros(ex.betweenBlocks*ex.flipsPerSec,1)];
end
ex.rectLWaveID = rectLWaveID;
ex.rectRWaveID = rectRWaveID;
ex.rectCWaveID = rectCWaveID;
%% Sine wave gratings locations (in the task loop since it changes)
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;
xL = rect(3)/2; % % = stimulus center located on the horizontal center of the screen
xR = rect(3)/2; % = stimulus center located on the horizontal center of the screen
xS = rect(3)/2;
yL = rect(4)/2 - ex.stim.gaborHDeg*ex.ppd+0*ex.ppd; % stimulus located 4 degrees above screen center
yR = rect(4)/2+ ex.stim.gaborHDeg*ex.ppd-0*ex.ppd; % stimulus located 4 degrees below screen cente
yS = rect(4)/2;
%% create drifting red dots position
for t =1:length(ex.stimDur)
oscillation = sprintf('oscillation%d',t);
driftPosDeg = sprintf('driftPosDeg%d',t);
ex.(driftPosDeg) = ex.stim.(oscillation).*ex.stim.cycles(t).*1/(2*ex.stim.spatialFreqDeg);
ex.fixSpatialPhase = 0; %(1/(4*ex.stim.spatialFreqDeg))*ex.ppd;
driftPos = sprintf('driftPos%d',t);
ex.(driftPos) = ex.(driftPosDeg).*ex.ppd +ex.fixSpatialPhase;
stillDotPhase = sprintf('stillDotPhase%d',t);
ex.(stillDotPhase)= ex.(driftPos)(1);
longDriftPos = sprintf('longDriftPos%d',t);
ex.(longDriftPos) = [repmat(ex.(stillDotPhase),1,ex.flipsPerSec*ex.trialFixation) ex.(driftPos) ex.(driftPos)(1:length(ex.(driftPos))/4) ... %drift for 1.25 (5/4) periods of oscilation
zeros(1,(ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec-floor((5/4)*length(ex.(driftPos)))) ...
zeros(1,ex.betweenBlocks*ex.flipsPerSec)];
% make lonDriftPos for the red dot only condition
longDriftPosRed = sprintf('longDriftPosRed%d',t);
ex.(longDriftPosRed) =[repmat(ex.(stillDotPhase),1,ex.flipsPerSec*ex.trialFixation) repmat(ex.(driftPos),1,4) ...
ex.(driftPos)(1:ex.flipsPerSec*(ex.blockLength(t)-ex.trialFixation)-length(repmat(ex.(driftPos),1,4))) ... %zeros(1,(ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec-floor(4.5*length(ex.(driftPos))))...
zeros(1,ex.betweenBlocks*ex.flipsPerSec)];
end
%% Eyetracking parameters
if ET
EyelinkSetup(0);
eye_used = Eyelink('EyeAvailable');
% ex.ShowRealTimeGaze = [ ]; % [] or [ something ]
% ex.nGazetoShow = [ 60 ]; % current~past N fixations
end
%% %%%% initial window - wait for backtick
DrawFormattedText(w,'Follow the oscillating grating or visual phantom within the gap at the center of the screen \n\n as best as you can using the red dot as a guide, even after the red dot is gone. \n\n Do your best not to blink during a trial. \n\n Press Space to start'... % : '...
,'center', 'center',[0 0 0]);
Screen(w, 'Flip', 0);
%WaitSecs(2);
KbTriggerWait(KbName('Space'), deviceNumber);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% experiment %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% START task TASK/FLIPPING
gray = repmat(mean(squeeze(ex.rectLWave1(1,1,:))), [1,3]);
n=1;
blockCnt = 1;
cnt = 0; %stim onset/ stime offset count
onOffs = [diff(ex.longFormBlocks) 0];
bLength = ex.blockLength(1);
ex.flipTime = nan(length(ex.trialFlips),length(ex.condShuffle));
%%% initial fixation
if n == 1 && blockCnt == 1 %for first block
ex.tasktstart = clock;
ex.startRun = GetSecs();
Screen('FillRect', w, gray);
Screen(w, 'Flip', 0);
WaitSecs(ex.initialFixation);
end
%%% Launch the task
for c =1:length(ex.condShuffle)
cnt = cnt+1;
thisCond = ex.condShuffle(c);
condName = conditions(thisCond).name{:};
%%for each condition, we specify the parameters values before we flip
%%over the gratings phases
%screen background color
if contains(condName, 'Minbg')
gray = repmat(min(min(squeeze(ex.rectLWave1(1,:,:)),[],1)), [1,3]);
ex.fixCol1Grad = linspace(255,gray(1),90);% make red dot disapear in 90 flips = 1.5 sec ; logspace(log10(255),log10(gray(1)));
ex.fixCol2Grad = linspace(0,gray(1),90);
elseif contains(condName, 'Meanbg')
gray = repmat(mean(squeeze(ex.rectLWave1(1,1,:))), [1,3]);
ex.fixCol1Grad = linspace(255,gray(1),90);% make red dot disapear in 90 flips = 1.5 sec ; logspace(log10(255),log10(gray(1)));
ex.fixCol2Grad = linspace(0,gray(1),90);
end
%draw guide red dot
if contains(condName, 'Vel1')
stillDotPhase = 'stillDotPhase1';
driftPos = 'driftPos1';
longDriftPos = 'longDriftPos1';
t =1;
elseif contains(condName, 'Vel2')
stillDotPhase = 'stillDotPhase2';
driftPos = 'driftPos2';
longDriftPos = 'longDriftPos2';
t = 2;
end
if contains(condName, 'Sp8') % is there gonna be a grating inducers pair? If yes, indicate the location
ex.rectLRect = CenterRectOnPoint([0 0 ex.rectGaborWidth ex.gaborHeight],xL,yL);
ex.rectRRect = CenterRectOnPoint([0 0 ex.rectGaborWidth ex.gaborHeight],xR,yR);
timeOn = length([repmat(ex.(stillDotPhase),1,ex.flipsPerSec*ex.trialFixation) ex.(driftPos) ex.(driftPos)(1:length(ex.(driftPos))/4)]);
guideFlipCnt = 1;
elseif contains(condName, 'Single')
ex.rectCRect = CenterRectOnPoint([0 0 ex.rectGaborWidth ex.gaborHeight],xS,yS);
timeOn = length([repmat(ex.(stillDotPhase),1,ex.flipsPerSec*ex.trialFixation) ex.(driftPos) ex.(driftPos)(1:length(ex.(driftPos))/4)]);
guideFlipCnt = 1;
elseif contains(condName, 'RedDot')
timeOn = length([repmat(ex.(stillDotPhase),1,ex.flipsPerSec*ex.trialFixation) repmat(ex.(driftPos),1,4) ex.(driftPos)(1:(ex.blockLength(t)-ex.trialFixation)*ex.flipsPerSec-length(repmat(ex.(driftPos),1,4)))]);
longDriftPosRed = sprintf('longDriftPosRed%d',t);
redFlipCnt = 1;
end
%flip through the block and following between block time
while n <= length(ex.trialFlips)
ex.longFormBlocks(n)
%%%% draw sine wave grating stimulus %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Screen('FillRect', w, gray);
if contains(condName, 'Sp8')
if nnz(find(ex.rectLWaveID(n,t)))
% top stim
Screen('DrawTexture', w, ex.rectLWaveID(n,t),[],ex.rectLRect);
% bottom stim
Screen('DrawTexture', w, ex.rectRWaveID(n,t), [], ex.rectRRect);
end
if guideFlipCnt <= timeOn - length(ex.fixCol1Grad)
xOffset = ex.(longDriftPos)(n);
Screen('FillOval', w,[255 0 0], [xc+xOffset-round(ex.fixSize/4) yc-round(ex.fixSize/4) xc+xOffset+round(ex.fixSize/4) yc+round(ex.fixSize/4)]);%black fixation solid circle
elseif guideFlipCnt > timeOn - length(ex.fixCol1Grad) && guideFlipCnt < timeOn
xOffset = ex.(longDriftPos)(n);
Screen('FillOval', w,[ex.fixCol1Grad(guideFlipCnt-timeOn+length(ex.fixCol1Grad)) ex.fixCol2Grad(guideFlipCnt-timeOn+length(ex.fixCol1Grad)) ex.fixCol2Grad(guideFlipCnt-timeOn+length(ex.fixCol1Grad))], [xc+xOffset-round(ex.fixSize/4) yc-round(ex.fixSize/4) xc+xOffset+round(ex.fixSize/4) yc+round(ex.fixSize/4)]);% fixation solid circle
elseif guideFlipCnt == length(ex.trialFlips)%bLength*ex.flipsPerSec+1 %make sure to reset the flipCnt once the trial ended, so that the red dot doe not reappear before the end of the trial
guideFlipCnt = 0;
end
guideFlipCnt = guideFlipCnt+1;
elseif contains(condName, 'Single')
if nnz(find(ex.rectCWaveID(n,t)))
% center stim
Screen('DrawTexture', w, ex.rectCWaveID(n,t),[],ex.rectCRect);
end
if guideFlipCnt <= timeOn - length(ex.fixCol1Grad)
xOffset = ex.(longDriftPos)(n);
Screen('FillOval', w,[255 0 0], [xc+xOffset-round(ex.fixSize/4) yc-round(ex.fixSize/4) xc+xOffset+round(ex.fixSize/4) yc+round(ex.fixSize/4)]);%black fixation solid circle
elseif guideFlipCnt > timeOn - length(ex.fixCol1Grad) && guideFlipCnt < timeOn
xOffset = ex.(longDriftPos)(n);
Screen('FillOval', w,[ex.fixCol1Grad(guideFlipCnt-timeOn+length(ex.fixCol1Grad)) ex.fixCol2Grad(guideFlipCnt-timeOn+length(ex.fixCol1Grad)) ex.fixCol2Grad(guideFlipCnt-timeOn+length(ex.fixCol1Grad))], [xc+xOffset-round(ex.fixSize/4) yc-round(ex.fixSize/4) xc+xOffset+round(ex.fixSize/4) yc+round(ex.fixSize/4)]);% fixation solid circle
elseif guideFlipCnt == length(ex.trialFlips)%bLength*ex.flipsPerSec+1 %make sure to reset the flipCnt once the trial ended, so that the red dot doe not reappear before the end of the trial
guideFlipCnt = 0;
end
guideFlipCnt = guideFlipCnt+1;
elseif contains(condName, 'RedDot')
if redFlipCnt < timeOn
xOffset = ex.(longDriftPosRed)(n);
Screen('FillOval', w,[255 0 0], [xc+xOffset-round(ex.fixSize/4) yc-round(ex.fixSize/4) xc+xOffset+round(ex.fixSize/4) yc+round(ex.fixSize/4)]);%black fixation solid circle
elseif redFlipCnt >= timeOn && redFlipCnt < (bLength)*ex.flipsPerSec
Screen('FillOval', w,[gray], [xc+xOffset-round(ex.fixSize/4) yc-round(ex.fixSize/4) xc+xOffset+round(ex.fixSize/4) yc+round(ex.fixSize/4)]);%black fixation solid circle
elseif redFlipCnt == length(ex.trialFlips)%(bLength)*ex.flipsPerSec+1 %make sure to reset the flipCnt once the trial ended, so that the red dot doe not reappear before the end of the trial
redFlipCnt =0;
%clear timeOn
end
redFlipCnt = redFlipCnt+1;
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;
%%%%% send messages to Eyelink for event times
if ET == 1
Eyelink('Message', char(sprintf('Cond %s', condName)));
Eyelink('Message', 'TRIALID %d', c);
Eyelink('Message', 'STIM_ONSET');
end
else
[VBLT,flipTime, FlipT, missed] = Screen(w, 'Flip',ex.startTrial + ex.trialFlips(n) - slack); %, %%% ex.flipTime(n,c)
flipTimes = [flipTimes, flipTime];
if ET == 1 && n == bLength*ex.flipsPerSec+1
Eyelink('Message', 'STIM_OFFSET');
end
end
if nnz(onOffs(n)) == 1
time = GetSecs;
cnt = cnt+1;
end
if (cnt/2 == 1 && GetSecs-time >= 1) && c ~= length(ex.condShuffle)
DrawFormattedText(w,'Press Space whenever you feel ready'... % : press 1 as soon as letter J appears on the screen,\n\n and press 2 as soon as letter K appears on the screen. \n\n Press Space to start'...
,'center', 'center',[0 0 0]);
Screen(w, 'Flip', 0);
KbTriggerWait(KbName('Space'), deviceNumber);
cnt = 0;
end
n = n+1;
end
ex.flipTime(:,c) = flipTimes;
n = 1;
end
% if n == 1382%360%421%420%359%1382%1561%
% break;
% end
%%%%%%%%%%%%%%%%%%
% done! wrap up %
%%%%%%%%%%%%%%%%%%
ex.runTime = GetSecs - ex.startRun;
savedir = fullfile(ex.root,'data',sprintf('s%s_%s/',subject,ex.version));
if ~exist(savedir); mkdir(savedir); end
savename = fullfile(savedir, strcat(sprintf('/s%s_smooth_pursuit_%s_date%s_fix',subject,ex.version,num2str(ex.date)), '.mat'));
%save(savename,'ex');
save(savename,'ex','-v7.3')
ShowCursor;
Screen('Close');
Screen('CloseAll');
fclose all;
if ET
Eyelink('StopRecording');
Eyelink('CloseFile');
%download edf file
Eyelink('Command', 'set_idle_mode');
try
fprintf('Receiving data file ''%s''\n', EyeData.edfFile);
status = Eyelink('ReceiveFile');
if status > 0
fprintf('ReceiveFile status %d\n', status);
end
if 2==exist(EyeData.edfFile, 'file')
fprintf('Data file ''%s'' can be found in ''%s''\n', EyeData.edfFile, pwd );
end
catch
fprintf('Problem receiving data file ''%s''\n', EyeData.edfFile);
end
Eyelink('Shutdown');
% savename = fullfile(savedir, strcat(sprintf('/s%s_smooth_pursuit_%s_date%s_fix_eyeDat',subject,ex.version,num2str(ex.date)), '.mat'));
% save(savename, 'EyeData')
end