-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPinna_grating_withline.m
executable file
·492 lines (446 loc) · 15.4 KB
/
Pinna_grating_withline.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
function Pinna_grating_withline(is_with_line, benchmark)
if ~exist('is_with_line','var'); is_with_line=false;end
if ~exist('benchmark','var'); benchmark=false;end
PsychDefaultSetup(0);
KbName('UnifyKeyNames');
esc = KbName('escape');
Screen('Preference', 'SkipSyncTests', 0)
% screen
screenID = max(Screen('Screens'));
% viewing parameters ------------------------------------------------------
sizePixel = 10/35; %in mm, calculated from different Screen ,has different value
stdDis = 600; %mm
% one_trials = 5;
% approach = ones(trials); %[0 1]; % simulate approaching (1) or leaving (0)
approach = 1;
% directions = [0 1]; % whether the inner ring has CW (0) or CCW (1) rotational direction when approaching, equivalently CCW (0) or CW (1) when leaving
speeds = 200; % translation speed, in mm/sec
angSpeed = 0; % rotation speeds, in degrees/sec, +: CW, -: CCW
allAngle1 = 45; %angle of inclination ,absolute value no + -
num_rings = 10;
is_mask_outer = 1;
is_mask_inner = 0;
f = 0.05;
grating_size = 15;
maskinner_radius = 2;
maskouter_radius = 10;
eachConditionSecs = 3; %1s
% number of elements
num1 = 20;
%for 1st ring
offset1 = 0;
ang1 = mod(offset1+linspace(0,360-360/num1,num1),360); %
ang1 = degtorad(ang1); %%
offset2 = 180/num1; % when different rings ,second ring's position is different
ang2 = mod(offset2+linspace(0,360-360/num1,num1),360); %
ang2 = degtorad(ang2); %%
% radius of ring of elements
r1 = 0.2; % in degree %% 初始ring的半径
% fixation point rectangle
fixSide = 8;% radius of fixation point is 8 pixel
fixColor = 0; %black
%--------------------------------------------------------------------------
try
white = WhiteIndex(screenID);
black = BlackIndex(screenID);
grey = (white+black)/2; % index for white, black and gray
if grey == white
grey = white/2;
end
inc = white-grey;
[w,wrect] = Screen('OpenWindow',screenID,grey,[]);
ifi = Screen('GetFlipInterval',w);
halfifi = ifi/2;
xCen = wrect(3)/2;
yCen = wrect(4)/2;
ScreenWidth = round(wrect(3)*sizePixel);
ppd = wrect(3)/2/atand(ScreenWidth/2/stdDis);
[os,od,oc]=Screen('BlendFunction',w);
fixRect = [xCen-fixSide/2,yCen-fixSide/2,xCen+fixSide/2,yCen+fixSide/2];
%make grating texture
f=f*2*pi; %used for angle1 and angle2
for jj = 1:1 %number of the MakeTexture is 20 ,at least
[x,y]=meshgrid(-30:30,-30:30); %used for angle1 and angle2
angle1=(-allAngle1-90)*pi/180;
a1=cos(angle1)*f;
b1=sin(angle1)*f;
m1=exp(-((x/grating_size).^2)-((y/grating_size).^2)).*sin(a1*x+b1*y-5);
eleTex1P = Screen('MakeTexture',w,grey+inc*m1); %CCW
end
%%%%%%%%%%%%%%%%%%%%%%outside mask & inside mask
maskinner_radius = maskinner_radius * ppd;% convert degrees to pixels
maskouter_radius = maskouter_radius * ppd;% convert degrees to pixels
if is_mask_outer == 1
masksize = [wrect(4),wrect(3)];
mask = ones(masksize(1),masksize(2),2)*grey;
[x,y] = meshgrid(0:masksize(2)-1,0:masksize(1)-1);
mask(:, :, 2)=white *(exp(-((x-xCen).^2+(y-yCen).^2)/(maskouter_radius)^2)); % gaussian mask
mask(mask>white)=white;
masktex = Screen('MakeTexture', w, mask);
end
if is_mask_inner == 1
masksize = [wrect(4),wrect(3)];
mask2 = ones(masksize(1),masksize(2),2)*grey;
[x,y] = meshgrid(0:masksize(2)-1,0:masksize(1)-1);
mask2(:, :, 2)=white * (1-exp(-((x-xCen).^2+(y-yCen).^2)/((maskinner_radius)*2)^2)); % gaussian mask
mask2(mask2>white)=white;
mask2(mask2<30) = black; %
masktex2 = Screen('MakeTexture', w, mask2);
end
%----------------------
r1Origin = r1 * ppd;% convert degrees to pixels
% radius of ring of elements
onFrames = fix(stdDis/(speeds*ifi) +1); %li:为了必须超出screen范围,取最大的帧数,同时不能超过半径公式的极大值, controllde through d0s and speeds
shiftAng = deg2rad(angSpeed*ifi); % degree of rotation per frame %%真实旋转
if speeds == 0
onFrames = 200; %
approach = 2; %when speed = 0, approach = 2;
end
breakLoop = false;
Priority(MaxPriority(w));
Screen('FillRect',w,grey,[]);
Screen('FillOval',w,fixColor,fixRect);
Screen('Flip',w);
WaitSecs(1);
while ~breakLoop
% %%%%%%%judge row and column ,get element
% if randAngleArray(iii)>= 0 %CCW
% row = 1;
% else
% row = 2;
% end
% column = find(allAngle1 == abs(randAngleArray(iii)));
%------------------------
i = 1; %when only has one ring
ii = 1; %%%%%%%%%%%%%%%%%%%ii,jj,kk,ll,mm,nn只针对真实旋转的而递增的变量,除了了speed=0 时 与i,j,k,l,m,n一致,且speed = 0时 一直递增!!!
jj = 1; kk = 1;
ll = 1; mm = 1;
nn = 1; oo = 1;
pp = 1; qq = 1;
rr = 1; ss = 1;
tt = 1; j = 1;
k = 1; l = 1;
m = 1; n = 1;
o = 1; p = 1;
q = 1; r = 1;
s = 1; t = 1;
switch num_rings
case 8
j = round(onFrames/8);
k = round(2*onFrames/8);
l = round(3*onFrames/8);
m = round(4*onFrames/8);
n = round(5*onFrames/8);
o = round(6*onFrames/8);
p = round(7*onFrames/8);
jj = j; kk = k;
ll = l; mm = m;
nn = n; oo = o;
pp = p;
case 9
j = round(onFrames/9);
k = round(2*onFrames/9);
l = round(3*onFrames/9);
m = round(4*onFrames/9);
n = round(5*onFrames/9);
o = round(6*onFrames/9);
p = round(7*onFrames/9);
q = round(8*onFrames/9);
jj = j; kk = k;
ll = l; mm = m;
nn = n; oo = o;
pp = p; qq = q;
case 10
j = round(onFrames/10);
k = round(2*onFrames/10);
l = round(3*onFrames/10);
m = round(4*onFrames/10);
n = round(5*onFrames/10);
o = round(6*onFrames/10);
p = round(7*onFrames/10);
q = round(8*onFrames/10);
r = round(9*onFrames/10);
jj = j; kk = k;
ll = l; mm = m;
nn = n; oo = o;
pp = p; qq = q;
rr = r;
case 11
j = round(onFrames/11);
k = round(2*onFrames/11);
l = round(3*onFrames/11);
m = round(4*onFrames/11);
n = round(5*onFrames/11);
o = round(6*onFrames/11);
p = round(7*onFrames/11);
q = round(8*onFrames/11);
r = round(9*onFrames/11);
s = round(10*onFrames/11);
jj = j; kk = k;
ll = l; mm = m;
nn = n; oo = o;
pp = p; qq = q;
rr = r; ss = s;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if approach ==2 %%每个trial ,ii ,jj ……初始化
ii = i; jj = j;
kk = k; ll = l;
mm = m; nn = n;
oo = o; pp = p;
qq = q; rr = r;
ss = s; tt = t;
end
max_r = 800;
min_size = 12;
max_size = 120;
r_a = 2*(max_r-r1Origin)/(onFrames)^2;
size_a = 2*(max_size-min_size)/(onFrames)^2;
%%%%%%
xy1 = zeros(2,2*num1);
xy2 = zeros(2,2*num1);
xy1(1,2:2:end) = 800 * sin(ang1);
xy1(2,2:2:end) = -800 * cos(ang1);
xy2(1,2:2:end) = 800 * sin(ang2);
xy2(2,2:2:end) = -800 * cos(ang2);
vbl = Screen('Flip',w);
ts = vbl;
count = 0;
vblendtime = vbl + eachConditionSecs;
%-----------
while GetSecs < vblendtime
count = count + 1;
a1 = rad2deg(mod(ang1+(ii-1)*shiftAng,2*pi));
r1 = r1Origin + 0.5*r_a*i^2;
side1P(1) = min_size + 0.5*size_a*i^2;
side1P(2) = side1P(1);
dstRect1 = [xCen+r1*sin(mod(ang1+(ii-1)*shiftAng,2*pi))-side1P(2)/2;yCen-r1*cos(mod(ang1+(ii-1)*shiftAng,2*pi))-side1P(1)/2;...
xCen+r1*sin(mod(ang1+(ii-1)*shiftAng,2*pi))+side1P(2)/2;yCen-r1*cos(mod(ang1+(ii-1)*shiftAng,2*pi))+side1P(1)/2];
a2 = rad2deg(mod(ang2+(jj-1)*shiftAng,2*pi));
r2 = r1Origin + 0.5*r_a*j^2;
side2P(1) = min_size + 0.5*size_a*j^2;
side2P(2) = side2P(1);
dstRect2 = [xCen+r2*sin(mod(ang2+(jj-1)*shiftAng,2*pi))-side2P(2)/2;yCen-r2*cos(mod(ang2+(jj-1)*shiftAng,2*pi))-side2P(1)/2;...
xCen+r2*sin(mod(ang2+(jj-1)*shiftAng,2*pi))+side2P(2)/2;yCen-r2*cos(mod(ang2+(jj-1)*shiftAng,2*pi))+side2P(1)/2];
a3 = rad2deg(mod(ang1+(kk-1)*shiftAng,2*pi));
r3 = r1Origin + 0.5*r_a*k^2;
side3P(1) = min_size + 0.5*size_a*k^2;
side3P(2) = side3P(1);
dstRect3 = [xCen+r3*sin(mod(ang1+(kk-1)*shiftAng,2*pi))-side3P(2)/2;yCen-r3*cos(mod(ang1+(kk-1)*shiftAng,2*pi))-side3P(1)/2;...
xCen+r3*sin(mod(ang1+(kk-1)*shiftAng,2*pi))+side3P(2)/2;yCen-r3*cos(mod(ang1+(kk-1)*shiftAng,2*pi))+side3P(1)/2];
a4 = rad2deg(mod(ang2+(ll-1)*shiftAng,2*pi));
r4 = r1Origin + 0.5*r_a*l^2;
side4P(1) = min_size + 0.5*size_a*l^2;
side4P(2) = side4P(1);
dstRect4 = [xCen+r4*sin(mod(ang2+(ll-1)*shiftAng,2*pi))-side4P(2)/2;yCen-r4*cos(mod(ang2+(ll-1)*shiftAng,2*pi))-side4P(1)/2;...
xCen+r4*sin(mod(ang2+(ll-1)*shiftAng,2*pi))+side4P(2)/2;yCen-r4*cos(mod(ang2+(ll-1)*shiftAng,2*pi))+side4P(1)/2];
a5 = rad2deg(mod(ang1+(mm-1)*shiftAng,2*pi));
r5 = round(r1Origin + 0.5*r_a*m^2);
side5P(1) = round(min_size + 0.5*size_a*m^2);
side5P(2) = side5P(1);
dstRect5 = [xCen+r5*sin(mod(ang1+(mm-1)*shiftAng,2*pi))-side5P(2)/2;yCen-r5*cos(mod(ang1+(mm-1)*shiftAng,2*pi))-side5P(1)/2;...
xCen+r5*sin(mod(ang1+(mm-1)*shiftAng,2*pi))+side5P(2)/2;yCen-r5*cos(mod(ang1+(mm-1)*shiftAng,2*pi))+side5P(1)/2];
a6 = rad2deg(mod(ang2+(nn-1)*shiftAng,2*pi));
r6 = r1Origin + 0.5*r_a*n^2;
side6P(1) = min_size + 0.5*size_a*n^2;
side6P(2) = side6P(1);
dstRect6 = [xCen+r6*sin(mod(ang2+(nn-1)*shiftAng,2*pi))-side6P(2)/2;yCen-r6*cos(mod(ang2+(nn-1)*shiftAng,2*pi))-side6P(1)/2;...
xCen+r6*sin(mod(ang2+(nn-1)*shiftAng,2*pi))+side6P(2)/2;yCen-r6*cos(mod(ang2+(nn-1)*shiftAng,2*pi))+side6P(1)/2];
r7 = r1Origin + 0.5*r_a*o^2;
side7P(1) = min_size + 0.5*size_a*o^2;
side7P(2) = side7P(1);
dstRect7 = [xCen+r7*sin(mod(ang1+(oo-1)*shiftAng,2*pi))-side7P(2)/2;yCen-r7*cos(mod(ang1+(oo-1)*shiftAng,2*pi))-side7P(1)/2;...
xCen+r7*sin(mod(ang1+(oo-1)*shiftAng,2*pi))+side7P(2)/2;yCen-r7*cos(mod(ang1+(oo-1)*shiftAng,2*pi))+side7P(1)/2];
a7 = rad2deg(mod(ang1+(oo-1)*shiftAng,2*pi));
dstRectA = [dstRect1, dstRect2, dstRect3, dstRect4, dstRect5, dstRect6, dstRect7];
aA = [a1 a2 a3 a4 a5 a6 a7];
if num_rings >=8
a8 = rad2deg(mod(ang2+(pp-1)*shiftAng,2*pi));
r8 = r1Origin + 0.5*r_a*p^2;
side8P(1) = min_size + 0.5*size_a*p^2;
side8P(2) = side8P(1);
dstRect8 = [xCen+r8*sin(mod(ang2+(pp-1)*shiftAng,2*pi))-side8P(2)/2;yCen-r8*cos(mod(ang2+(pp-1)*shiftAng,2*pi))-side8P(1)/2;...
xCen+r8*sin(mod(ang2+(pp-1)*shiftAng,2*pi))+side8P(2)/2;yCen-r8*cos(mod(ang2+(pp-1)*shiftAng,2*pi))+side8P(1)/2];
dstRectA = [dstRectA, dstRect8];
aA = [aA, a8];
if num_rings >=9
a9 = rad2deg(mod(ang1+(qq-1)*shiftAng,2*pi));
r9 = r1Origin + 0.5*r_a*q^2;
side9P(1) = min_size + 0.5*size_a*q^2;
side9P(2) = side9P(1);
dstRect9 = [xCen+r9*sin(mod(ang1+(qq-1)*shiftAng,2*pi))-side9P(2)/2;yCen-r9*cos(mod(ang1+(qq-1)*shiftAng,2*pi))-side9P(1)/2;...
xCen+r9*sin(mod(ang1+(qq-1)*shiftAng,2*pi))+side9P(2)/2;yCen-r9*cos(mod(ang1+(qq-1)*shiftAng,2*pi))+side9P(1)/2];
dstRectA = [dstRectA, dstRect9];
aA = [aA, a9];
if num_rings>= 10
a10 = rad2deg(mod(ang2+(rr-1)*shiftAng,2*pi));
r10 = r1Origin + 0.5*r_a*r^2;
side10P(1) = min_size + 0.5*size_a*r^2;
side10P(2) = side10P(1);
dstRect10 = [xCen+r10*sin(mod(ang2+(rr-1)*shiftAng,2*pi))-side10P(2)/2;yCen-r10*cos(mod(ang2+(rr-1)*shiftAng,2*pi))-side10P(1)/2;...
xCen+r10*sin(mod(ang2+(rr-1)*shiftAng,2*pi))+side10P(2)/2;yCen-r10*cos(mod(ang2+(rr-1)*shiftAng,2*pi))+side10P(1)/2];
dstRectA = [dstRectA, dstRect10];
aA = [aA, a10];
if num_rings >=11
a11 = rad2deg(mod(ang1+(ss-1)*shiftAng,2*pi));
r11 = r1Origin + 0.5*r_a*s^2;
side11P(1) = round(min_size + 0.5*size_a*s^2);
side11P(2) = side11P(1);
dstRect11 = [xCen+r11*sin(mod(ang1+(ss-1)*shiftAng,2*pi))-side11P(2)/2;yCen-r11*cos(mod(ang1+(ss-1)*shiftAng,2*pi))-side11P(1)/2;...
xCen+r11*sin(mod(ang1+(ss-1)*shiftAng,2*pi))+side11P(2)/2;yCen-r11*cos(mod(ang1+(ss-1)*shiftAng,2*pi))+side11P(1)/2];
dstRectA = [dstRectA, dstRect11];
aA = [aA, a11];
end %11
end %10
end %9
end %8
%%%%%%withline
if is_with_line
Screen('Drawlines',w,xy1,1,200,[xCen yCen],[]);
Screen('Drawlines',w,xy2,1,200,[xCen yCen],[]);
end
Screen('DrawTextures',w,eleTex1P,[],dstRectA, aA,1,[],[],[],[],[]); % 0 for nearest neighboring filtering, 1 for bilinear filtering
%%%%%%%%************************** add mask
if is_mask_outer == 1
Screen('BlendFunction',w,GL_ONE_MINUS_SRC_ALPHA,GL_SRC_ALPHA,[1 1 1 0]);
Screen('DrawTexture',w,masktex);
Screen('BlendFunction',w,GL_ONE,GL_ZERO,[1 1 1 1]);
end
if is_mask_inner == 1
Screen('BlendFunction',w,GL_ONE_MINUS_SRC_ALPHA,GL_SRC_ALPHA,[1 1 1 0]);
Screen('DrawTexture',w,masktex2);
Screen('BlendFunction',w,GL_ONE,GL_ZERO,[1 1 1 1]);
end
%---------------------------------
Screen('FillOval',w,fixColor,fixRect);
Screen('DrawingFinished', w);
if approach ==1
i = i+1;
ii = i;
if i == onFrames
i = 1;
ii = i;
end
j = j+1;
jj = j;
if j == onFrames
j = 1;
jj = j;
end
k = k+1;
kk = k;
if k == onFrames
k = 1;
kk = k;
end
l = l+1;
ll = l;
if l == onFrames
l = 1;
ll = l;
end
m = m+1;
mm = m;
if m == onFrames
m = 1;
mm = m;
end
n = n+1;
nn = n;
if n == onFrames
n = 1;
nn = n;
end
o = o+1;
oo = o;
if o == onFrames
o = 1;
oo = o;
end
if num_rings >=8
p = p +1;
pp = p;
if p == onFrames
p = 1;
pp = p;
end
if num_rings >=9
q = q +1;
qq = q;
if q == onFrames
q = 1;
qq = q;
end
if num_rings >= 10
r = r+1;
rr = r;
if r == onFrames
r = 1;
rr = r;
end
if num_rings >=11
s = s+1;
ss = s;
if s == onFrames
s = 1;
ss = s;
end
if num_rings >=12
t = t+1;
tt = t;
if t == onFrames
t = 1;
tt = t;
end
end %12
end %11
end %10
end %9
end %8
elseif approach == 2 % 只为了静止时,真实旋转可以实现
ii = ii+1;
jj = jj+1;
kk = kk+1;
ll = ll+1;
mm = mm+1;
nn = nn+1;
oo = oo +1;
pp = pp+1;
qq = qq +1;
rr = rr +1;
ss = ss +1;
tt = tt +1;
end
if benchmark == 0
[vbl,~,~,missed] = Screen('Flip',w,vbl+halfifi);
if missed>0;fprintf('---!!! Missed frame !!!---\n');end
else
Screen('Flip',w,0,2,2);
end
[~,~,keycode] = KbCheck(-1);
if keycode(esc)
breakLoop = true;
break;
end
end
if benchmark > 0
avgfps = count / (GetSecs - ts);
fprintf('--->>> The average FPS was: %f fps.\n',avgfps);
end
end
%save result .mat
% save(file.sta_fileName,'ana');
% save('ana');
Screen('FillRect',w,grey,[]);
Screen('FillOval',w,fixColor,fixRect);
Priority(0);
Screen('Flip',w);
WaitSecs(1); % wait for 2 seconds
Screen('CloseAll');
ShowCursor;
catch
Screen('CloseAll');
Priority(0);
psychrethrow(psychlasterror);
end
return;