-
Notifications
You must be signed in to change notification settings - Fork 1
/
stimMakeTactileFingerMappingExp.m
210 lines (182 loc) · 10.8 KB
/
stimMakeTactileFingerMappingExp.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
function stimMakeTactileFingerMappingExp(stimParams, runNum, directions, condition, makeFigure)
% Makes a simple tactile hand experiment where the stimulators vibrate in
% several patterns and assumes one stimulator per finger.
%
% set some stimulus properties related to the hardware
stimulus = [];
stimulus.NIdaqRate = stimParams.NIdaqRate;
stimulus.NIdaqNames = stimParams.NIdaqNames;
stimulus.numOfStimulators = stimParams.numOfStimulators;
stimulus.frameRate = stimParams.display.frameRate;
stimulus.cmap = stimParams.stimulus.cmap;
stimulus.srcRect = stimParams.stimulus.srcRect;
stimulus.dstRect = stimParams.stimulus.destRect;
stimulus.display = stimParams.display;
stimParams.fingerIdx = [1:stimulus.numOfStimulators, 6 7];
stimParams.fingers = {'thumb','index', 'middle', 'ring', 'little','all', 'blank'};
stimParams.stimDurSamples = round(stimParams.stimDurSecs * stimParams.NIdaqRate);
stimParams.stimDurFrames = stimParams.stimDurSecs * stimParams.display.frameRate;
stimParams.onDurSamples = stimParams.onDurSecs * stimParams.NIdaqRate;
stimParams.offDurSamples = stimParams.offDurSecs * stimParams.NIdaqRate;
stimParams.onOffReps = stimParams.stimDurSamples / (stimParams.onDurSamples + stimParams.offDurSamples);
stimParams.tactileIntensity = 1.0; % maximal value is 2, check state of amplifier, too
switch lower(stimParams.modality)
case 'fmri'
% no trigger sequence needed
otherwise
% store how many samples needed for a trigger signal
stimParams.triggerSamples = 50;
end
% check whether timings match
if (floor(stimParams.onOffReps) ~= stimParams.onOffReps)
error('stimulus duration cannot be calculated');
end
% warn if samples don't match secs
if (stimParams.stimDurFrames / stimParams.display.frameRate ~= stimParams.stimDurSecs)
error('stimulus duration cannot be calculated');
end
%% stimulus sequence timing
for jj = 1:length(directions)
switch directions{jj}
case 'Random'
%duration of the whole experiment
stimParams.expDurSecs = stimParams.numReps * ... %number of repetitions
stimParams.numOfStimulators * ... %number of fingers
(stimParams.stimDurSecs ...%duration of one stimulus
+ stimParams.interStimIntervalSecs)...%pause after each stimulus
+ stimParams.preScanDurSecs + stimParams.postScanDurSecs;%pauses at the beginning and the end of the experiment
%durations of different (non-)stimulation periods
stimParams.periodDursSecsVect = [stimParams.preScanDurSecs, ... %pre-stimulation
repmat(...
[stimParams.stimDurSecs,... %one tactile stimulation
stimParams.interStimIntervalSecs],... % pause after one stimulation
1,stimParams.numReps * stimParams.numOfStimulators)... % repeat for each repetition and finger
stimParams.postScanDurSecs];% add post-experimental pause
%onsets of tactile stimuli(
stimParams.stimOnsetsSecs = cumsum([stimParams.preScanDurSecs, ... %first stimulus starts after pre-experiment pause
repmat(...
stimParams.stimDurSecs... %one stimulation
+ stimParams.interStimIntervalSecs,... % add pause after each sweep = stimulation
1,(stimParams.numReps * stimParams.numOfStimulators)-1)... % repeat for all sweeps
]);
end
end
% calculate in frames
stimParams.expDurFrames = round(stimParams.expDurSecs * stimParams.display.frameRate);%duration of the whole experiment in frames
stimParams.stimOnsetsSamples = stimParams.stimOnsetsSecs*stimParams.NIdaqRate;%onsets of tactile stim in samples
stimParams.stimOnsetsSecsFrameNormed = round(stimParams.stimOnsetsSecs*stimParams.display.frameRate)/stimParams.display.frameRate;
stimParams.stimOnsetsFrames = stimParams.stimOnsetsSecsFrameNormed*stimParams.display.frameRate;%onsets of new tactile stim in frames
stimParams.seqtiming = 0:(1/stimParams.display.frameRate):stimParams.expDurSecs;%frame onsets
stimParams.fixSeq = ones(size(stimParams.seqtiming)); %determines whether and which type of fixation is shown
stimParams.seq = ones(size(stimParams.seqtiming));
stimParams.trigSeq = zeros(size(stimParams.seqtiming));
stimParams.trigSeq(stimParams.stimOnsetsFrames + 1) = 1;
stimParams.trigSeq(1) = 1;
stimParams.trigSeq(end) = 1;
stimulus.seqtiming = stimParams.seqtiming;
stimulus.fixSeq = stimParams.fixSeq;
stimulus.seq = stimParams.seq;
stimulus.trigSeq = stimParams.trigSeq;
%debug (check timings of nidaq onsets vs psychtoolbox trigger onsets)
% figure;stem(stimulus.seqtiming, stimulus.trigSeq); hold on
% stem(stimParams.stimOnsetsSecs, ones(1,length(stimParams.stimOnsetsSecs)))
% make a blank to insert between simulus presentations
screenRect = size(zeros(stimulus.dstRect(4)-stimulus.dstRect(2): stimulus.dstRect(3)-stimulus.dstRect(1)));
images = ones([screenRect 1], 'uint8');
images(:,:,1) = 127;
stimulus.images = images;
%% Make stimulus for the experiment
% signal for one tactile stimulus (e.g., per finger)
% carrier signal for one tactile stimulus
stimulusSignalBase = 0.5 + 0.5 * sin(-pi/2+linspace(0, ...
(2*pi*stimParams.carrierFreq/stimParams.NIdaqRate * stimParams.stimDurSamples), ...
stimParams.stimDurSamples)');
% modulating signal for one tactile stimulus
stimulusSignalModulator = repmat([ones(stimParams.onDurSamples, 1); zeros(stimParams.offDurSamples,1)], ...
stimParams.onOffReps,1);
% final signal for one tactile stimulus
stimParams.stimulusSignal = stimulusSignalBase .* stimulusSignalModulator * stimParams.tactileIntensity;
% replace last entry with 0 to inactivate tactile stimulator
stimParams.stimulusSignal(end) = 0;
%initialize matrix to hold activation of each stimulator during each
%sample of the nidaq for one whole experiment
stimParams.vibrotactileStimulus = zeros(round(stimParams.expDurSecs*stimParams.NIdaqRate), stimParams.numOfStimulators);
for jj = 1:length(directions)
switch directions{jj}
case 'Random'
stimulatorOrder = repmat(1:stimParams.numOfStimulators,1, stimParams.numReps);
stimulatorOrder = stimulatorOrder(randperm(length(stimulatorOrder)));
end
end
for jj = 1:length(directions)
% Loop through the stimulus sequence
for ii = 1:length(stimParams.stimOnsetsSamples)
% Insert the tactile signal at the respective time points and stimulator
stimParams.vibrotactileStimulus(int64(stimParams.stimOnsetsSamples(ii) + 1):int64(stimParams.stimOnsetsSamples(ii) + 1) + stimParams.stimDurSamples - 1, ...
stimulatorOrder(ii)) = stimParams.stimulusSignal;
end
switch lower(stimParams.site)
case 'nyuecog'
% Write trigger sequence
% add fake tactile channel
stimParams.vibrotactileStimulus = [stimParams.vibrotactileStimulus, ...
zeros(size(stimParams.vibrotactileStimulus,1),1)];
% Insert the trigger signal at the onset of the run
stimParams.vibrotactileStimulus(int64(1):int64(1 +stimParams.triggerSamples - 1), ...
stimParams.numOfStimulators + 1) = ones(stimParams.triggerSamples,1);
% Loop through the stimulus sequence
for ii = 1:length(stimParams.stimOnsetsSamples)
% Insert the trigger signal at the respective time points
% of the stimulator
stimParams.vibrotactileStimulus(int64(stimParams.stimOnsetsSamples(ii) + 1):int64(stimParams.stimOnsetsSamples(ii) + 1) +stimParams.triggerSamples - 1, ...
stimParams.numOfStimulators + 1) = ones(stimParams.triggerSamples,1);
end
% Insert the trigger signal at the end of the run
stimParams.vibrotactileStimulus(end - stimParams.triggerSamples : end - 1, ...
stimParams.numOfStimulators + 1) = ones(stimParams.triggerSamples,1);
end
%Save stimulus matrix, tsv info and make figures
fname = sprintf('%s_%s%s_%d.mat', stimParams.site, condition, directions{jj}, runNum);
% check whether figures should be made
if makeFigure
% save figure with images of stimulus
f = figure('visible', 'off');
imagesc(stimParams.vibrotactileStimulus')
title (sprintf('%s', directions{jj}))
ylabel('Tactile stimulators');
xlabel('Time (s)')
yticks(stimParams.fingerIdx(1:stimParams.numOfStimulators))
yticklabels(stimParams.fingers(1:stimParams.numOfStimulators))
xticks(0:stimParams.NIdaqRate * 10:stimParams.expDurSecs * stimParams.NIdaqRate)
xticklabels(0:10:stimParams.expDurSecs)
saveas(f, fullfile(vistadispRootPath, 'StimFiles', sprintf('%s.png',fname(1:end-6))))
end
% Sparsify the visual stimulus sequence and the triggers (apart
% from the tactile one)
maxUpdateInterval = 0.25;
stimulus = sparsifyStimulusStruct(stimulus, maxUpdateInterval);
% Set TSV file information
onsets = round(stimParams.stimOnsetsSecs,3)';
duration = repmat(stimParams.stimDurSecs, length(stimParams.stimOnsetsSecs),1);
ISI = repmat(stimParams.offDurSecs, length(stimParams.stimOnsetsSecs),1);
trial_type = stimParams.fingerIdx(stimulatorOrder)';
trial_name = stimParams.fingers(stimulatorOrder)';
stim_frequency = repmat(stimParams.carrierFreq, length(stimParams.stimOnsetsSecs),1);
stim_amplitude = repmat(stimParams.tactileIntensity,length(stimParams.stimOnsetsSecs),1);
stim_file = repmat(fname, length(stimParams.stimOnsetsSecs),1);
stim_order = repmat(directions(jj), length(stimParams.stimOnsetsSecs),1);
stimParams.tsv = table(onsets, duration, ISI, trial_type, trial_name, ...
stim_frequency, stim_amplitude, stim_file, stim_order);
stimulus.tsv = table(onsets, duration, ISI, trial_type, trial_name, ...
stim_frequency, stim_amplitude, stim_file, stim_order);
% store for main script calls
stimulus.vibrotactileStimulus = stimParams.vibrotactileStimulus;
% Save
if ~exist(fullfile(vistadispRootPath, 'StimFiles', fname), 'file')
fprintf('[%s]: Saving stimuli in: %s\n', mfilename, fullfile(vistadispRootPath, 'StimFiles', fname));
save(fullfile(vistadispRootPath, 'StimFiles', fname), 'stimulus')
save(fullfile(vistadispRootPath, 'StimFiles', [sprintf('%s_%s', stimParams.site, directions{jj}(1:4)),'_stimParams.mat']), 'stimParams');
else
error('stimulus files for this experiment already exist, move or delete old files')
end
end