-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_stim_20201026.m
270 lines (199 loc) · 7.64 KB
/
basic_stim_20201026.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
function basic_stim_serial()
%% Installation instruction for Psychtoolbox
% bash
% sudo apt-get install octave
% sudo apt-get install liboctave-dev
% sudo apt-get install octave-psychtoolbox-3
% sudo octave --no-gui
%% Installing instrument crontol package
% octave
% pkg install -forge instrument-control
% pkg load instrument-control
sca;
close all;
clear;
%% Experimental parameters
SAVEFOLDER = '/mnt/data/ptb/';
paramsField = {'animalName', ...
'time', ...
'nDirection', ...
'nBlock', ...
'nTrialPerBlock', ...
'stimulusDuration', ...
'itiStart', ...
'itiMean', ...
'itiEnd', ...
'viewAngle', ...
'spatialFrequency', ...
'temporalFrequency', ...
'laserLatency'};
paramsDefault = {'ptb', ... % animal name
strftime('%Y%m%d_%H%M%S', localtime(time())), ... % time
12, ... % n direction
4, ... % n block
5, ... % n trial per block
0.5, ... % stimulus duration in second
0.5, ... % iti start in second
1.5, ... % iti mean in second
3, ... % iti end in second
90, ... % view angle
0.05, ... % spatial frequency
2, ... % temporal frequency
0}; % laser latency in ms
paramsValue = inputdlg(paramsField, '', 1, paramsDefault);
if isempty(paramsValue); return; end
for iParams = 3:length(paramsValue)
paramsValue{iParams} = str2double(paramsValue{iParams});
end
params = cell2struct(paramsValue, paramsField);
params.nTrial = params.nDirection * params.nTrialPerBlock * params.nBlock;
%% Serial commnunication
pkg load instrument-control
ser = serial('/dev/ttyACM0');
srl_write(ser, '0');
pause(0.05);
srl_write(ser, ['l', params.laserLatency]);
%% Psychtoolbox setup
% Change preference
PsychDefaultSetup(2);
Screen('Preference', 'VisualDebuglevel', 3);
oldEnableFlag = Screen('Preference', 'Verbosity', [1]);
oldSyncFlag = Screen('Preference', 'SkipSyncTests', 2);
% Get screen information
screens = Screen('Screens');
screenNumber = max(screens);
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
grey = white / 2;
% Open window
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
% Make this top priority
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);
% Set the blend function for the screen (I don't know what it means...)
%Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
% Get window information
[params.screenXpixels, params.screenYpixels] = Screen('WindowSize', window);
halfDiag = round(sqrt(params.screenXpixels ^ 2 + params.screenYpixels ^ 2) / 2);
fullDiag = halfDiag * 2 + 1;
params.ifi = Screen('GetFlipInterval', window); % inter-flip interval
params.frameRate = 1 / params.ifi;
params.stimulusFrame = round(params.stimulusDuration * params.frameRate);
durationTotal = params.nTrial * (params.stimulusDuration + params.itiMean);
fprintf('\nDuration: %d\n', durationTotal / 60);
% Open shader (I don't know what it means...)
AssertGLSL;
glsl = MakeTextureDrawShader(window, 'SeparateAlphaChannel');
% Keyboard information
startKey = KbName('F12');
escapeKey = KbName('SCROLLLOCK');
%% Make stimuli
% Square
squareRect = [params.screenXpixels-75, params.screenYpixels-75, params.screenXpixels, params.screenYpixels];
% Grating stimuli
gratingRect = [0, 0, fullDiag, fullDiag]; % grating size
fp = params.spatialFrequency * params.viewAngle / params.screenXpixels; % spatial frequency per pixel
p = 1 / fp; % number of pixels for one period
fr = fp * 2 * pi; % spatial radian per pixel
x = meshgrid(-halfDiag:halfDiag + ceil(p), -halfDiag:halfDiag);
grating = 0.5 + 0.5 * cos(fr * x); % sine wave grating
grating(:, :, 2) = 0; % secondary mask for temporal shifting
grating(1:fullDiag, 1:fullDiag, 2) = 1;
gratingTex = Screen('MakeTexture', window, grating, [], [], [], [], glsl);
angle = 360 / params.nDirection;
directionBase = repmat((0:11)' * angle, params.nTrialPerBlock, 1);
%% Plot
% Initialize window
Screen('FillRect', window, black, squareRect);
Screen('TextSize', window, 40);
DrawFormattedText(window, 'Press F12 to start, and SCROLLLOCK to exit', 'center', params.screenYpixels * 0.975, [0.25, 0.25, 0.25]);
Screen('Flip', window);
inExperiment = checkWaitKey(startKey, escapeKey);
while inExperiment
% Generate stimulus
directions = [];
for iBlock = 1:params.nBlock
idx = randperm(params.nDirection * params.nTrialPerBlock);
directions = [directions; directionBase(idx)];
end
iti = exprnd(params.itiMean - params.itiStart, params.nTrial, 1) + params.itiStart;
iti(iti > params.itiEnd) = params.itiEnd;
itiFrame = round(iti / params.ifi);
% Save setup data
fileName = [params.animalName, '_', strftime('%Y%m%d_%H%M%S', localtime(time())), '.mat'];
fullFileName = fullfile(SAVEFOLDER, fileName);
% Initial iti
Screen('FillRect', window, black, squareRect);
srl_write(ser, '0');
vbl = Screen('Flip', window);
% Start trial
vbl = Screen('Flip', window, vbl + 2);
for iTrial = 1:params.nTrial
% Trial
%srl_write(ser, '1');
for iFrame = 1:params.stimulusFrame
yoffset = mod(iFrame * params.temporalFrequency * p * params.ifi, p);
Screen('DrawTexture', window, gratingTex, gratingRect, [], directions(iTrial), [], [], [], [], [], [0, yoffset, 0, 0]);
Screen('FillRect', window, white, squareRect);
if checkKey(escapeKey)
srl_write(ser, '0');
directions = directions(1:(iTrial-1));
itiFrame = itiFrame(1:(iTrial-1));
save('-mat7-binary', fullFileName, 'directions', 'itiFrame', 'params');
finishTask();
return
end
if iFrame == 3
srl_write(ser, '1');
end
vbl = Screen('Flip', window, vbl + 0.5 * params.ifi);
end
% Inter-trial interval
%srl_write(ser, '0');
for iFrame = 1:itiFrame(iTrial)
Screen('FillRect', window, black, squareRect);
if checkKey(escapeKey)
srl_write(ser, '0');
directions = directions(1:(iTrial-1));
itiFrame = itiFrame(1:(iTrial-1));
save('-mat7-binary', fullFileName, 'directions', 'itiFrame', 'params');
finishTask();
return
end
if iFrame == 3
srl_write(ser, '0');
end
vbl = Screen('Flip', window, vbl + 0.5 * params.ifi);
end
end
save('-mat7-binary', fullFileName, 'directions', 'itiFrame', 'params');
Screen('FillRect', window, black, squareRect);
DrawFormattedText(window, 'Press F12 to start, and SCROLLLOCK to exit', 'center', params.screenYpixels * 0.975, [0.25, 0.25, 0.25]);
Screen('Flip', window);
inExperiment = checkWaitKey(startKey, escapeKey);
end
srl_write(ser, '0');
finishTask();
fclose(ser);
function inExperiment = checkWaitKey(startKey, escapeKey)
notDone = true;
while notDone
[~, keyCode] = KbStrokeWait;
if keyCode(escapeKey)
inExperiment = false;
notDone = false;
elseif keyCode(startKey)
inExperiment = true;
notDone = false;
end
end
function stop = checkKey(escapeKey)
[keyDown, ~, keyCode] = KbCheck;
stop = false;
if keyCode(escapeKey)
stop = true;
end
function finishTask()
% Clear the screen
Priority(0);
sca;