-
Notifications
You must be signed in to change notification settings - Fork 14
/
osl_erase_trigger_artefact.m
281 lines (227 loc) · 9.33 KB
/
osl_erase_trigger_artefact.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
function D = osl_erase_trigger_artefact(D, triggerChan, triggerTime, deleteWindow, trialInds, doPlot)
%OSL_ERASE_TRIGGER_ARTEFACT zeros out data around OHBA's 2015 trigger artefact
%
% Use this function to clean your images before publication, and to make it
% clear in any subsequent analyses if you've forgotten to exclude this
% window. Use it after any filtering processes you intend to do.
% Alternatively, run your whole analyses, and zero out the artefact from
% your final TF/ERF plots.
%
% D = OSL_ERASE_TRIGGER_ARTEFACT(D, TRIGGERCHAN) zeros a window of 40ms
% around t = 0s every trial. TRIGGERCHAN is the string giving the label
% of the trigger channel
%
% The function will clean the data, over-writing D. It will set the
% zeroed-out data as artefact events, flagging the bad samples.
%
% D = OSL_ERASE_TRIGGER_ARTEFACT(D, TRIGGERCHAN, TRIGGERTIME, DELETEWINDOW, TRIALINDS)
% optionally pass in a set of times to use, TRIGGERTIME, in seconds.
% Use one value (the same for each trial), or one value per trial.
% You can set the window size DELETEWINDOW in ms for zeroing (default
% 40ms). You can also choose a subset of trials, specified by TRIALINDS.
%
% D = OSL_ERASE_TRIGGER_ARTEFACT(D, TRIGGERCHAN, [], ...) will attempt to
% auto-detect the times to zero using data in the trigger channel given by
% label TRIGGERCHAN. (e.g. 'STI101'). Check your data after using this
% option: success is not guaranteed!
%
% D = OSL_ERASE_TRIGGER_ARTEFACT(..., DOPLOT) will provide rude
% epoch-averaged plots to check the result.
%
% You should ensure that you exclude a short window centred on the trigger
% from all further analyses, so that these zeros are not included.
%
% How to descibe this analysis in your methods / supplementary methods:
% We have removed data associated with a trigger-locked artefact from our
% figure for clarity. We ignored a window of X ms centred on the
% trigger in all analyses.
% Copyright 2015 OHBA
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% $LastChangedBy: GilesColclough $
% $Revision: 763 $
% $LastChangedDate: 2015-10-21 11:52:19 +0100 (Wed, 21 Oct 2015) $
% Contact: [email protected]
% Originally written on: GLNXA64 by Giles Colclough, 01-Dec-2015 14:30:16
%% Input checking
% D should be an epoched, sensor-space, Neuromag object
D = spm_eeg_load(D);
assert(~strcmpi(D.type, 'continuous'), ...
[mfilename ':NotEpochedD'], ...
'Input spm object should be epoched data. \n');
assert(any(strcmp('MEGMAG', D.chantype)) && any(strcmp('MEGPLANAR', D.chantype)), ...
[mfilename ':NotNeuromag'], ...
'Input spm object should be a sensor-space Neuromag recording. \n');
% plot result?
if nargin < 6 || ~exist('doPlot', 'var'),
doPlot = false;
else
doPlot = logical(doPlot);
end%if
% select trials to use
if nargin < 5 || ~exist('trialInds', 'var') || isempty(trialInds),
trialInds = 1:D.ntrials;
else
assert(all(ismember(trialInds, 1:D.ntrials)), ...
[mfilename ':BadTrialSelection'], ...
'Selected trials did not match number of trials in object. \n');
end%if
nTrialsToUse = length(trialInds);
% set size of window to zero out
if nargin < 4 || ~exist('deleteWindow', 'var') || isempty(deleteWindow),
deleteWindow = 40; %ms
else
assert(isnumeric(deleteWindow) && all([1,1] == size(deleteWindow)), ...
[mfilename ':BadWindowSpec'], ...
'deleteWindow should be a time in ms. \n');
end%if
% check that the window matches length of trial
trialLength = max(D.time) - min(D.time); % in s
assert(deleteWindow./1000 < trialLength, ...
[mfilename ':WindowTooLong'], ...
'Window to zero is longer than trial length. \n');
% trigger chan should be the name of one of the channels in D
assert(ischar(triggerChan) && any(strcmp(triggerChan, D.chanlabels)), ...
[mfilename ':BadChanInput'], ...
'TRIGGERCHAN should be the name of a channel in D. \n');
if nargin < 3 || ~exist('triggerTime', 'var'),
triggerTime = 0;
end%if
if isempty(triggerTime),
autoDetect = true;
triggerTime = 0;
else
autoDetect = false;
end%if
if all(1 == size(triggerTime,1) && 1 == size(triggerTime,2)),
triggerTime = repmat(triggerTime, nTrialsToUse, 1);
end%if
assert(all(triggerTime < max(D.time) & triggerTime > min(D.time)), ...
[mfilename ':BadTriggerTimes'], ...
'Trigger times should be within time range of trial. \n');
%% Preliminaries
% pull out helpful channels
triggerInd = find(strcmp(triggerChan, chanlabels(D)));
megInd = find(strncmpi('MEG', chantype(D), 3)); % spm's function routinely misses channels
% have a look at data before cleanup
if doPlot,
hf = figure('Color', 'w', 'Name', 'Trigger artefact before cleanup');
plot_problem(hf, D, triggerInd, trialInds);
end%if
%% Zero relevant data
if autoDetect,
zeroInds = auto_detect_zero_inds(D, triggerInd, deleteWindow, trialInds);
else
zeroInds = get_zero_inds(D, triggerTime, deleteWindow);
end%if
% we might have differing zero lengths in each trial
for iTrial = 1:nTrialsToUse,
trial = trialInds(iTrial);
toZero = zeroInds{iTrial};
D(megInd, toZero, trial) = zeros(length(megInd), length(toZero));
end%for
D = set_bad(D, zeroInds, trialInds);
%% Plot cleaned data and save result before returning
D.save;
if doPlot,
hf = figure('Color', 'w', 'Name', 'Trigger artefact after cleanup');
plot_problem(hf, D, triggerInd, trialInds);
end%if
end%osl_remove_trigger_artefact
function zeroInds = get_zero_inds(D, triggerTime, deleteWindow)
%GET_ZERO_INDS parse information to produce indices to zero
% number of zeros either side of trigger time
padding = round(deleteWindow./1000 * D.fsample ./ 2);
for iTrial = length(triggerTime):-1:1,
trigInd = nearest(D.time, triggerTime(iTrial));
zeroInds{iTrial} = trigInd-padding:trigInd+padding;
end%for
end%get_zero_inds
function zeroInds = auto_detect_zero_inds(D, triggerInd, deleteWindow, trialInds)
%AUTO_DETECT_ZERO_INDS finds indices to zero pased on trigger channel
% number of zeros either side of trigger time
padding = round(deleteWindow./1000 * D.fsample ./ 2);
for iTrial = length(trialInds):-1:1,
trigger = D(triggerInd,:,trialInds(iTrial));
% assume the baseline in a trigger channel is its mode
triggerOn = trigger ~= mode(trigger);
diffOn = [0 diff(triggerOn)];
onSamples = find(1 == diffOn);
for iS = 1:length(onSamples),
triggerOn(onSamples-padding:onSamples+padding) = 1;
end%for
offSamples = find(-1 == diffOn);
for iS = 1:length(offSamples),
triggerOn(offSamples-padding:offSamples+padding) = 1;
end%for
zeroInds{iTrial} = find(triggerOn);
end%for
end%get_zero_inds
function D = set_bad(D, zeroInds, trialInds)
%SET_BAD marks artefact chunks
for iTrial = 1:length(trialInds),
trial = trialInds(iTrial);
% get old events
Events = D.events(trial);
BadEvents = struct([]);
if ~isempty(zeroInds{iTrial}),
BadEvents(1).type = 'artefact_OHBA_trigger';
BadEvents(1).value = 'all';
BadEvents(1).time = D.time(zeroInds{iTrial}(1));
BadEvents(1).duration = (zeroInds{iTrial}(end) - zeroInds{iTrial}(1) + 1) ...
/ D.fsample;
BadEvents(1).offset = 0;
end%if
% Concatenate new and old events
if ~isempty(BadEvents)
Events = [Events{:}; BadEvents(:)];
end
% Save new events with previous
D = events(D,trial,Events);
end%for
end%set_bad
% make a quick function to create epoch averages
function average = epoch_average(D, chanInds, trialInds)
if strcmpi(D.type, 'continuous'),
average = 0;
for iTrial = 1:length(trialInds),
trialSamples = trialInds{iTrial};
average = average + D(chanInds, trialSamples, :);
end%for
average = average ./ length(trialInds);
else
average = mean(D(chanInds,:,trialInds),3);
end%if
end%epoch_average
% make a quick function to create plots
function [] = plot_problem(hf, D, triggerInd, trialInds)
% we've already checked for neuromag data so this is a safe assumption
magInds = find(strcmp('MEGMAG', D.chantype));
planarInds = find(strcmp('MEGPLANAR', D.chantype));
figure(hf);
subplot(3,1,1);
plot(D.time, epoch_average(D, magInds, trialInds)'); % D.time,
% xlim([-0.4 0.6])
ylabel('fT');
subplot(3,1,2);
plot(D.time, epoch_average(D, planarInds, trialInds)'); % D.time,
% xlim([-0.4 0.6])
ylabel('fT/mm');
subplot(3,1,3);
plot(D.time, epoch_average(D, triggerInd, trialInds)'); % D.time,
% xlim([-0.4 0.6])
ylabel('uV');
xlabel('time / s');
drawnow;
end%plot_problem
% [EOF]