-
Notifications
You must be signed in to change notification settings - Fork 0
/
RET_localizerWEDGE.m
225 lines (207 loc) · 9.11 KB
/
RET_localizerWEDGE.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
%% RET LOCALIZER WITH WEDGE TEST
% PK: Feb. 23, 2010
% 96 TRs (2 sec) = 3 min, 12 secs
%% STARTUP COMMANDS
close all;
clear all;
clear mex;
IOPort('CloseAll');
AssertOpenGL; %Break if installed Psychtoolbox is not based on OpenGL or Screen() is not working properly.
Screen('Preference','SkipSyncTests',1); %skip hardware test
%% GET SUBJECT INFORMATION
subName = input('Initials of subject? (default="tmp") ','s'); % get subject's initials from user
if length(subName) < 1; subName = 'tmp'; end;
CurRun = str2double(input('Run Number? ','s'));
listen = str2double(input('Listen for scanner [1=yes], 2=no? ','s'));
if isnan(listen); listen = 1; end;
% name of data files to save relevant variables
sequence = sprintf('%s%d_RET_WEDGEsequence.mat', subName, CurRun);
buttons = sprintf('%s%d_RET_WEDGEbuttons.mat',subName,CurRun);
triggersfile = sprintf('%s%d_RET_WEDGEtriggers.mat',subName,CurRun);
listfile = sprintf('%s%d_RET_WEDGElist.mat',subName,CurRun);
%% DECLARE VARIABLES
DEBUG_PRINTOUTS = 1;
TotalPulseTime = [];
TotalShowTime = [];
data = [];
buttonpress = [];
triggers = [];
presentations = cell(97,5);
ang_list = [];
RunOrder = zeros(97,2);
repetitions = 5; % number of circulations
bigsize = 6;
smallsize = 3;
rectcolor = [0 255 0; 255 0 0; 0 0 255];
%all the below related to checkerboard pattern
rcycles = 8; % number of white/black circle pairs
tcycles = 16; % number of white/black angular segment pairs (integer)
flicker_freq = 4; % full cycle flicker frequency (Hz)
flick_dur = 1/flicker_freq/2;
flick = 1;
num_flick = 1;
TR = 2.;
ScreenNum = max(Screen('Screens'));
%% MAKE PRESENTATIONS LISTS
ang = (360/tcycles):(360/tcycles):360;
for z=1:repetitions
ang_list = [ang_list, ang];
end
if mod(CurRun,2)~=0; % odd run, wedge counterclockwise
temp = ang_list(1:length(ang_list));
else % even run, wedge clockwise
temp = ang_list(length(ang_list):-1:1);
end
RunOrder(9:length(ang_list)+8,1) = temp;
steps = [-1, 0];
while length(find(RunOrder(:,2)~=0)) < 48
for z = 1:length(RunOrder);
if mod(z,2) == 0;
tempstep = steps(randperm(2)); % pick a random step
RunOrder(z+tempstep(1),2) = round(rand)+1; % make that position 1 or 2
if tempstep(1) ~= 0; RunOrder(z,2) = 0; end;
else
RunOrder(z,2) = 0;
end
end
end
%% SUBJECT PROMPT
HideCursor; %hides the cursor
[w,rect]=Screen('OpenWindow',ScreenNum,[127 127 127], [], [], [], [], kPsychNeedFastOffscreenWindows); %kPsychNeedFastBackingStore
Priority(2); %sets the priority to the maximum possible
x0 = rect(3)/2; % screen center
y0 = rect(4)/2;
prompt = imread('RETprompt.png');
[ysize,xsize,ncolors]=size(prompt);
xsize = (rect(3)/6)*(xsize/ysize);
ysize = rect(3)/6;
t=Screen('MakeTexture',w,prompt); %converting image matrix to a texture
destrect = [x0-xsize, y0-ysize, x0+xsize, y0+ysize];
Screen('DrawTexture',w,t,[],destrect);
Screen('Flip',w);
if listen ~= 1
WaitSecs(1); %% ONLY FOR DEBUGGING
else
end
%% MAKE CHECKERBOARDS
hi_index=255; % black color
lo_index=0; % white color
bg_index =128; %background
xysize = sqrt(power(rect(3),2)+power(rect(4),2)); % wedge diameter
s = xysize/sqrt(2); % size used for mask
xylim = 2*pi*rcycles;
[x,y] = meshgrid(-xylim:2*xylim/(xysize-1):xylim, -xylim:2*xylim/(xysize-1):xylim);
at = atan2(y,x);
checks = ((1+sign(sin(at*tcycles)+eps) .* ...
sign(sin(sqrt(x.^2+y.^2))))/2) * (hi_index-lo_index) + lo_index;
circle = x.^2 + y.^2 <= xylim^2;
checks = circle .* checks + bg_index * ~circle;
t(1) = Screen('MakeTexture', w, checks);
t(2) = Screen('MakeTexture', w, hi_index - checks); % reversed contrast
%% PRE-PRESENTATION
if listen == 1
IOPort('Verbosity', 10);
[P4, openerror] = IOPort('OpenSerialPort', '/dev/ttyUSB0','BaudRate=115200'); %opens port for receiving scanner pulse
IOPort('Flush', P4); %flush event buffer
else
end
DynScan = 1;
temp_data=1;
bigrect = [x0-bigsize, y0-bigsize, x0+bigsize,y0+bigsize];
smallrect = [x0-smallsize, y0-smallsize, x0+smallsize,y0+smallsize];
sprintf('Ready to present. listen:%d\n', listen)
% %% IMAGE PRESENTATION
NRuns = length(RunOrder)-1
while DynScan <= NRuns
if listen == 1;
tprev = GetSecs;
pulse = [];
while isempty(pulse)
[pulse,temptime,readerror] = IOPort('read',P4,0,1);
end
if length(triggers) == 0; t0 = temptime; end;
if DEBUG_PRINTOUTS;
tnow = GetSecs;
navail = IOPort('BytesAvailable', P4);
fprintf('%3d Got %c at %.5f. / %.5f waited %.5f avail: %d',
DynScan, pulse, temptime, tnow, tnow - tprev, navail);
if length(triggers)>0;
fprintf(' dt=%.5f', temptime-triggers(end));
if pulse == 53; fprintf(' delay=%.5f', tnow-t0-(DynScan-1)*TR); end;
end
fprintf('\n');
end;
else
pulse = 53;
temptime = GetSecs;
end
if pulse == 'q'; break; end; % quit if 'q'
if pulse == 53; %if a pulse has been received from the scanner
triggers = [ triggers; temptime ];
if RunOrder(DynScan,1) == 0; % THIS IS FOR THE BLANK
Screen('FillRect',w,[127 127 127]); %fills the whole screen - changes background color%
Screen('FillRect',w,rectcolor(RunOrder(DynScan,2)+1,:),bigrect);
Screen('FillRect',w,[255 255 0],smallrect);
Screen('Flip',w);
ShowTime = GetSecs;
WaitSecs(.240); %after 240 ms
Screen('FillRect',w,rectcolor(1,:),bigrect);
Screen('FillRect',w,[255 255 0],smallrect);
Screen('Flip',w);
presentations(DynScan,:)= {CurRun,DynScan,RunOrder(DynScan,1),RunOrder(DynScan,2),ShowTime};
if listen ~= 1 || DynScan == NRuns % So if is the last trial
WaitSecs(1.75); % ONLY FOR DEBUGGING - TO MIMIC SCANNER BEHAVIOR
end
DynScan = DynScan + 1;
else % PRESENT IMAGE
while num_flick <=(TR/flick_dur) % 2 seconds of flicker
Screen('DrawTexture', w, t(flick));
theta1 = deg2rad(RunOrder(DynScan,1));
theta2 = deg2rad((180-(360/tcycles)+RunOrder(DynScan,1))); %offset
st1 = sin(theta1); ct1 = cos(theta1);
st2 = sin(theta2); ct2 = cos(theta2);
xy1 = s * [0,0; -st1,-ct1; -st1-ct1,st1-ct1; st1-ct1,st1+ct1; -st2,-ct2] + ones(5,1) * [x0 y0];
xy2 = s * [0,0; st1,ct1; -st2-ct2,st2-ct2; st2-ct2,st2+ct2; st2,ct2] + ones(5,1) * [x0 y0];
Screen('FillPoly', w, bg_index, xy1);
Screen('FillPoly', w, bg_index, xy2);
Screen('FillRect',w,rectcolor(RunOrder(DynScan,2)+1,:),bigrect);
Screen('FillRect',w,[255 255 0],smallrect);
if num_flick == 1; ShowTime = GetSecs;
elseif num_flick > 3; % after 240 ms
Screen('FillRect',w,rectcolor(1,:),bigrect);
Screen('FillRect',w,[255 255 0],smallrect);
end
Screen('Flip',w,[], 2, 0); %usually 2 (what for?), 1 for do not sync
tnow = GetSecs;
sleep_dur = temptime + num_flick*flick_dur - tnow;
flick = 3-flick;
if num_flick < (TR/flick_dur)
% Skip waiting on the last one -- just wait on the trigger above
WaitSecs(sleep_dur); % 8 hz flicker
end
num_flick = num_flick + 1;
end
presentations(DynScan,:)= {CurRun,DynScan,RunOrder(DynScan,1),RunOrder(DynScan,2),ShowTime};
DynScan = DynScan + 1;
num_flick = 1;
end
TotalShowTime = [TotalShowTime; ShowTime];
ShowTime = [];
pulse = [];
else
if ~isempty(pulse); buttonpress = [buttonpress; pulse, temptime]; end% add to buttonpresses
pulse = [];
end
end
tend = GetSecs;
%% CLOSING COMMANDS
cd './data';
save(sequence,'presentations'); %saves the sequence data
save(buttons, 'buttonpress'); %saves the buttonpresses
save(triggersfile,'triggers'); %saves the list
Screen('CloseAll');
IOPort('CloseAll');
cd ..
dt = triggers(2:end)-triggers(1:end-1) - 2;
fprintf('Post-analysis of triggers delays. Total runtime=%.3f Min=%.3f Max=%.3f\n',
tend-t0, min(dt), max(dt));