-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathpop_rmbase.m
217 lines (205 loc) · 9.24 KB
/
pop_rmbase.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
% POP_RMBASE - remove channel baseline means from an epoched or
% continuous EEG dataset. Calls RMBASE.
% Usage:
% >> OUTEEG = pop_rmbase( EEG ); % pop up an interactive arg entry window
% >> OUTEEG = pop_rmbase( EEG, timerange, pointrange, chanlist); % call RMBASE
%
% Graphic interface:
% "Baseline latency range" - [edit box] Latency range for the baseline in ms.
% Collects the 'timerange' command line input.
% Empty or [] input -> Use whole epoch as baseline
% "Baseline points vector" - [edit box] Collects the 'pointrange' command line
% option (below). (Overwritten by 'timerange' above).
% Empty or [] input -> Use whole epoch as baseline
% Inputs:
% EEG - Input dataset
% timerange - [min_ms max_ms] Baseline latency range in milliseconds.
% Empty or [] input -> Use whole epoch as baseline
% pointrange - [min:max] Baseline points vector (overwritten by timerange).
% Empty or [] input -> Use whole epoch as baseline
% chanlist - [cell] List of channels. Default is all.
%
% Outputs:
% OUTEEG - Output dataset
%
% Note: If dataset is continuous, channel means are removed separately
% for each continuous data region, respecting 'boundary' events
% marking boundaries of excised or concatenated data portions.
%
% Author: Arnaud Delorme, CNL / Salk Institute, 2001
%
% See also: RMBASE, EEGLAB
% Copyright (C) 2001 Arnaud Delorme, Salk Institute, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
% 01-25-02 reformated help & license -ad
function [EEG, com] = pop_rmbase( EEG, timerange, pointrange, chanlist)
com ='';
if nargin < 1
help pop_rmbase;
return;
end
if isempty(EEG(1).data)
disp('pop_rmbase(): cannot remove baseline of an empty dataset'); return;
end
if nargin < 1
help pop_rmbase;
return;
end
if nargin < 4 || isempty(chanlist)
chanlist = 1:EEG(1).nbchan;
end
if nargin < 2, timerange = []; end
if nargin < 3, pointrange = []; end
if nargin < 2
% popup window parameters
% -----------------------
defaultbase = [num2str(EEG(1).times(1)) ' 0'];
if EEG(1).times(1) >= 0
defaultbase = '[ ]';
end
if EEG(1).trials > 1
uilist = { { 'style' 'text' 'string' 'Baseline latency range ([min max] in ms) ([ ] = whole epoch):' } ...
{ 'style' 'edit' 'string' defaultbase } ...
{ 'style' 'text' 'string' 'Or remove baseline points vector (ex:1:56):' } ...
{ 'style' 'edit' 'string' '' } ...
{ 'style' 'text' 'string' 'Note: press Cancel if you do not want to remove the baseline' } };
uigeom = { [3 1] [3 1] [1] };
else
uilist = { { 'style' 'text' 'string' 'Removing the mean of each data channel (press cancel to skip)' } };
uigeom = { [1] };
end
% add channel selection
% --------------
if ~isempty(EEG(1).chanlocs)
tmpchanlocs = EEG(1).chanlocs;
else
tmpchanlocs = [];
for index = 1:EEG(1).nbchan
tmpchanlocs(index).labels = int2str(index);
tmpchanlocs(index).type = '';
end
end
cb_type = 'pop_chansel(get(gcbf, ''userdata''), ''field'', ''type'', ''handle'', findobj(''parent'', gcbf, ''tag'', ''chantypes''));';
cb_chan = 'pop_chansel(get(gcbf, ''userdata''), ''field'', ''labels'', ''handle'', findobj(''parent'', gcbf, ''tag'', ''channels''));';
enableChan = fastif(length(EEG) > 1, 'off', 'on') ;
uilist = { uilist{:} ...
{ 'style' 'text' 'string' 'Channel type(s)' 'enable' enableChan} ...
{ 'style' 'edit' 'string' '' 'tag' 'chantypes' 'enable' enableChan} ...
{ 'style' 'pushbutton' 'string' '...' 'callback' cb_type 'enable' enableChan} ...
{ 'style' 'text' 'string' 'OR channel(s) (default all)' 'enable' enableChan} ...
{ 'style' 'edit' 'string' '' 'tag' 'channels' 'enable' enableChan} ...
{ 'style' 'pushbutton' 'string' '...' 'callback' cb_chan 'enable' enableChan}
};
uigeom = { uigeom{:} [2 1.5 0.5] [2 1.5 0.5] };
[result, usrdat, sres2, sres] = inputgui( 'uilist', uilist, 'geometry', uigeom, 'title', 'Baseline removal - pop_rmbase()', 'helpcom', 'pophelp(''pop_rmbase'');', 'userdata', tmpchanlocs);
if isempty(result), return; end
% decode parameters
% -----------------
if EEG(1).trials > 1
if numel(result) < 2 || ((isempty(result{1}) || strcmp(result{1},'[]') ) ...
&& (isempty(result{2}) || strcmp(result{2},'[]')))
timerange = [ EEG(1).times(1) EEG(1).times(end) ]; % whole epoch latency range
pointrange = [];
fprintf('pop_rmbase(): using whole epoch as baseline.\n');
else
timerange = eval( [ '[' result{1} ']' ] );
pointrange = eval( [ '[' result{2} ']' ] );
end
end
if ~isempty(sres.chantypes)
chanlist = eeg_decodechan(EEG.chanlocs, parsetxt(sres.chantype), 'type');
elseif ~isempty(sres.channels)
chanlist = eeg_decodechan(EEG(1).chanlocs, sres.channels);
end
end
% process multiple datasets
% -------------------------
if length(EEG) > 1
if nargin < 2
[ EEG, com ] = eeg_eval( 'pop_rmbase', EEG, 'warning', 'on', 'params', { timerange pointrange } );
else
[ EEG, com ] = eeg_eval( 'pop_rmbase', EEG, 'params', { timerange pointrange } );
end
return;
end
flag_timerange = 1; % provide timerange as input (so use in history)
if ~isempty(timerange)
if timerange(1) < EEG.times(1) || timerange(end) > EEG.times(end)
error('pop_rmbase(): Bad time range');
end
pointrange = find( EEG.times >= timerange(1) & EEG.times <= timerange(2));
elseif ~isempty(pointrange)
flag_timerange = 0;
if pointrange(1) < 1, pointrange(1) = 1; end
if pointrange(end) > EEG.pnts, pointrange(end) = EEG.pnts; end
else
pointrange = [1:EEG.pnts];
end
%
% Respect excised data boundaries if continuous data
% ---------------------------------------------------
fprintf('pop_rmbase(): Removing baseline...\n');
if EEG.trials == 1 && ~isempty(EEG.event) ...
&& isfield(EEG.event, 'type') ...
&& ischar(EEG.event(1).type)
tmpevent = EEG.event;
boundaries = eeg_findboundaries(tmpevent);
if ~isempty(boundaries) % this is crashing
fprintf('Pop_rmbase(): finding continuous data discontinuities\n');
boundaries = round([ tmpevent(boundaries).latency ] -0.5-pointrange(1)+1);
boundaries(boundaries>=pointrange(end)-pointrange(1)) = [];
boundaries(boundaries<1) = [];
boundaries = [0 boundaries pointrange(end)-pointrange(1)+1];
for index=1:length(boundaries)-1
tmprange = [boundaries(index)+1:boundaries(index+1)];
if length(tmprange) > 1
EEG.data(chanlist,tmprange) = rmbase( EEG.data(:,tmprange), length(tmprange), ...
[1:length(tmprange)]);
elseif length(tmprange) == 1
EEG.data(chanlist,tmprange) = 0;
end
end
else
EEG.data(chanlist,:) = rmbase( EEG.data(chanlist,:), EEG.pnts, pointrange );
end
else
for indc = chanlist
tmpmean = mean(double(EEG.data(indc,pointrange,:)),2);
EEG.data(indc,:,:) = EEG.data(indc,:,:) - repmat(tmpmean, [1 EEG.pnts 1]);
end
% EEG.data = rmbase( reshape(EEG.data, EEG.nbchan, EEG.pnts*EEG.trials), EEG.pnts, pointrange );
end
EEG.data = reshape( EEG.data, EEG.nbchan, EEG.pnts, EEG.trials);
EEG.icaact = [];
if isequal(chanlist, [1:EEG.nbchan]), chanlist = []; end
if flag_timerange, pointrange = []; else, timerange = []; end
if isempty(chanlist)
com = sprintf('EEG = pop_rmbase( EEG, %s);',vararg2str({timerange pointrange}));
else
com = sprintf('EEG = pop_rmbase( EEG, %s);',vararg2str({timerange pointrange chanlist}));
end
return;