-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop_par_autobssemg.m
289 lines (255 loc) · 9.36 KB
/
pop_par_autobssemg.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
% pop_autobssemg() - Automatic EMG correction using Blind Source Separation
%
% Usage:
% >> OUTEEG = pop_autobssemg(INEEG, wl, ws, bss_alg, bss_opt, crit_alg, crit_opt)
%
% Inputs:
% INEEG - input EEG dataset
% wl - analysis window length (in seconds)
% ws - shift between correlative analysis windows (in seconds)
% bss_alg - name of the BSS algorithm to use
% bss_opt - options to pass to the BSS algorithm as
% {opt_name1, opt_value1,opt_name2,opt_value2,...}
% crit_alg - name of the criterion for selecting the components to remove
% crit_opt - options to pass to the criterion function as
% {opt_name1, opt_value1,opt_name2,opt_value2,...}
%
% Outputs:
% OUTEEG - output dataset
%
% Notes:
%
% (*) The automatic method for correcting EMG artifacts using CCA was
% originally proposed by Wim et al. in:
%
% De Clercq, W. et al., A new muscle artifact removal technique to improve
% interpretation of the ictal scalp electroencephalogram, Proceedings of
% EMBC 2005, Shanghai, China, pp. 1136-1139.
%
% (*) When using pop_autobssemg from the command line a pop_up window will
% be displayed ONLY if the user did not provide the value of the window
% length, that is, only if there is a single input parameter.
%
% See also:
% AUTOBSS, EMG_PSD, EEGLAB
% Copyright (C) <2007> German Gomez-Herrero, http://germangh.com
function [EEG, com] = pop_par_autobssemg(EEG, wl, ws, bss_alg, bss_opt, crit_alg, crit_opt)
com = '';
if nargin < 2,
% at least the first 2 params are required or the pop-up window will appear
showpopup = true;
else
showpopup = false; % do not show pop-up window unless completely necessary
end
if nargin < 1,
help pop_autobssemg;
return;
end
if isempty(EEG.data)
disp('(pop_autobssemg) error: cannot clean an empty dataset'); return;
end;
% default window length/shift (just a rule of thumb)
% ---------------------------
N = EEG.pnts*size(EEG.data,3);
def_wl_samples = EEG.srate*0.02*EEG.nbchan^2;
if size(EEG.data,3) > 1,
def_wl_samples = def_wl_samples-mod(def_wl_samples,EEG.pnts);
end
def_wl_samples = min(def_wl_samples,N);
def_wl = def_wl_samples/EEG.srate;
def_ws = def_wl;
% use default wl and ws?
% ---------------------------
if nargin < 2 || (isempty(wl) && isempty(ws)),
wl = def_wl;
ws = def_ws;
elseif nargin < 3,
ws = wl;
end
% find available algorithms
% -----------------------------
allalgs = {'bsscca','iwasobi','efica','multicombi','fcombi','sobi','fpica', ...
'runica','jader','fastica','pca'};
selectalg = {};
for index = 1:length(allalgs)
if exist([allalgs{index} '_ifc'],'file') && exist([allalgs{index}],'file'),
selectalg = {selectalg{:} allalgs{index}};
end
end
if isempty(selectalg),
error('(pop_autobssemg) I could not find an interface function for any BSS algorithm');
end
% use default BSS algorithm with default options?
% ----------------------------------------------------
if nargin < 4 || isempty(bss_alg),
bss_alg = selectalg{1};
elseif isempty(bss_alg) && nargin > 4 && ~isempty(bss_opt),
showpopup = true; % wrong input -> show pop-up window!
end
if nargin < 5 || isempty(bss_opt),
switch lower(bss_alg),
case 'bsscca',
bss_opt = {'eigratio',1e6};
otherwise,
bss_opt = {};
end
end
% find available criteria to remove components
% ---------------------------------------------
sptver = ver('signal');
if isempty(sptver) || (str2double(sptver.Version)<6.2 && datenum(sptver.Date)<datenum('1-Jan-2007')),
error('(pop_autobssemg) all available criteria require MATLAB''s Signal Processing Toolbox v.6.2 or newer');
else
allcrits = {'emg_psd'};
end
selectcrit = {};
for index = 1:length(allcrits)
if exist([allcrits{index}],'file') && exist([allcrits{index}],'file'),
selectcrit = {selectcrit{:} allcrits{index}};
end
end
if isempty(selectcrit),
error('(pop_autobssemg) I could not find any criterion function');
end
% user wants to use the default criterion with default options
% ----------------------------------------------------
if nargin < 6 || isempty(crit_alg),
crit_alg = selectcrit{1};
elseif isempty(crit_alg) && nargin > 6 && ~isempty(crit_alg),
showpopup = true; % wrong input -> show pop-up window!
end
if nargin < 7 || isempty(crit_opt),
% user wants def. criterion options
switch lower(crit_alg),
case 'emg_psd',
sl = min(floor(EEG.srate/2),floor(wl/5)*EEG.srate); % 5 windows will be used by default
estimator = spectrum.welch({'Hamming'},sl);
crit_opt = {'ratio',10,'fs',num2str(EEG.srate),'femg',15,'estimator',...
estimator,'range',[0 floor(EEG.nbchan/2)]};
otherwise,
crit_opt = {};
end
end
% default spectral estimator (assumes default criterion is emg_psd)
% ---------------------------
if ~strcmpi(selectcrit{1},'emg_spd'),
sl = min(floor(EEG.srate/2),floor(wl/5)*EEG.srate); % 5 windows will be used by default
def_estimator = ['spectrum.welch({''Hamming''},' num2str(sl) ')'];
else
def_estimator = '';
end
% build the string of default criterion options
% ---------------------------
switch lower(selectcrit{1}),
case 'emg_psd',
def_criterion_opts = ['''ratio'',10,''fs'',' num2str(EEG.srate) ...
',''femg'',15,''estimator'',' def_estimator ',''range'',[' ...
num2str([0 floor(EEG.nbchan/2)]) ']'];
otherwise,
def_criterion_opts = '';
end
% display input dialog
% ---------------------------
if showpopup,
uigeom = {[1.5 1] [1.5 1] [1.5 1] 1 1 1 [1.5 1] 1 1};
uilist = { { 'style' 'text' 'string' 'BSS algorithm:'} ...
{ 'style' 'popupmenu' 'string' selectalg} ...
{'style' 'text' 'string' 'Analysis window length (seconds):'} ...
{'style' 'edit' 'string' num2str(def_wl)} ...
{'style' 'text' 'string' 'Shift between correlative windows (seconds):'} ...
{'style' 'edit' 'string' num2str(def_ws)} ...
{'style' 'text' 'string' 'Options to pass to the BSS algorithm ([option_name],[value],...):'} ...
{'style' 'edit' 'string' '''eigratio'',1e6'} ...
{'style' 'text' 'string' ''} ...
{ 'style' 'text' 'string' 'Criterion to remove components:'} ...
{ 'style' 'popupmenu' 'string' selectcrit} ...
{'style' 'text' 'string' 'Options to pass to the criterion ([option_name],[value],...):'} ...
{'style' 'edit' 'string' def_criterion_opts} ...
};
guititle = 'Correct EMG using BSS -- pop_autobssemg()';
result = inputgui( uigeom, uilist, 'pophelp(''pop_autobssemg'')', guititle, [], 'normal');
if isempty(result), return; end;
% reading params
% -------------------
bss_alg = selectalg{result{1}};
wl = eval(['[' result{2} ']']);
ws = eval(['[' result{3} ']']);
% read BSS parameters
% -------------------
bss_opt = eval(['{' result{4} '}']);
% criterion
% -----------
crit_alg = selectcrit{result{5}};
% criterion parameters
% ---------------------
crit_opt = eval(['{' result{6} '}']);
end
% correct wl,ws so that they will be an integer number of trials
% -------------------
wl = min(floor(wl*EEG.srate),EEG.pnts*EEG.trials);
ws = min(floor(ws*EEG.srate),EEG.pnts*EEG.trials);
if size(EEG.data,3) > 1,
wl = wl-mod(wl,EEG.pnts);
ws = ws-mod(ws,EEG.pnts);
end
opt = struct;
% build BSS parameters structure
% -------------------------------
if exist('bss_opt','var'),
opt.bss_opt = struct;
for i = 1:2:length(bss_opt),
opt.bss_opt.(bss_opt{i}) = bss_opt{i+1};
end
end
% build criterion parameters structure
% -------------------------------
if exist('crit_opt','var'),
opt.crit_opt = struct;
for i = 1:2:length(crit_opt),
opt.crit_opt.(crit_opt{i}) = crit_opt{i+1};
end
end
opt.crit_opt.eogref = [];
% rest of parameters
% -------------------
opt.wl = wl;
opt.ws = ws;
opt.bss_alg = bss_alg;
opt.crit_alg = crit_alg;
opt.crit_opt.fs = EEG.srate;
% run the EMG correction
% -------------------
EEG.data = par_autobss(reshape(EEG.data,EEG.nbchan,EEG.pnts*EEG.trials),opt);
EEG.data = reshape(EEG.data,[EEG.nbchan,EEG.pnts,EEG.trials]);
% command history
% -------------------
bss_opt_str = [];
for i = 1:2:length(bss_opt),
if isnumeric(bss_opt{i+1}),
bss_opt_str = [bss_opt_str ',''' bss_opt{i} ''', [' num2str(bss_opt{i+1}) ']'];
elseif ischar(bss_opt{i+1}),
bss_opt_str = [bss_opt_str ',''' bss_opt{i} ''',''' bss_opt{i+1} ''''];
else
bss_opt_str = [bss_opt_str ',''' bss_opt{i} ''',''' class(bss_opt{i+1}) ''''];
end
end
if ~isempty(bss_opt_str),
bss_opt_str(1)=[];
end
crit_opt_str = [];
for i = 1:2:length(crit_opt),
if isnumeric(crit_opt{i+1}),
crit_opt_str = [crit_opt_str ',''' crit_opt{i} ''', [' num2str(crit_opt{i+1}) ']'];
elseif ischar(crit_opt{i+1}),
crit_opt_str = [crit_opt_str ',''' crit_opt{i} ''',''' crit_opt{i+1} ''''];
else
crit_opt_str = [crit_opt_str ',''' crit_opt{i} ''',' class(crit_opt{i+1})];
end
end
if ~isempty(crit_opt_str),
crit_opt_str(1)=[];
end
com = sprintf( '%s = pop_autobssemg( %s, [%s], [%s], ''%s'', {%s}, ''%s'', {%s});', inputname(1), ...
inputname(1), num2str(wl/EEG.srate), num2str(ws/EEG.srate), bss_alg, ...
bss_opt_str, crit_alg, crit_opt_str);
return;