-
Notifications
You must be signed in to change notification settings - Fork 239
/
eeg_regepochs.m
197 lines (178 loc) · 7.88 KB
/
eeg_regepochs.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
% EEG_REGEPOCHS - Convert a continuous dataset into consecutive epochs of
% a specified regular length by adding dummy events of type
% and epoch the data around these events. Alternatively
% only insert events for extracting these epochs.
% May be used to split up continuous data for
% artifact rejection followed by ICA decomposition.
% The computed EEG.icaweights and EEG.icasphere matrices
% may then be exported to the continuous dataset and/or
% to its epoched descendents.
% Usage:
% >> EEGout = eeg_regepochs(EEG); % use defaults
% >> EEGout = eeg_regepochs(EEG, 'key', val, ...);
%
% Required input:
% EEG - EEG continuous data structure (EEG.trials = 1)
%
% Optional inputs:
% 'recurrence' - [in s] the regular recurrence interval of the dummy
% events used as time-locking events for the
% consecutive epochs {default: 1 s}
% 'limits' - [minsec maxsec] latencies relative to the time-locking
% events to use as epoch boundaries. Stated epoch length
% will be reduced by one data point to avoid overlaps
% {default: [0 recurrence_interval]}
% 'rmbase' - [NaN|latency] remove baseline (s). NaN does not remove
% baseline. 0 remove the pre-0 baseline. To
% remove the full epoch baseline, enter a value
% larger than the upper epoch limit. Default is 0.
% 'eventtype' - [string] name for the time locking event type. Default is 'X'
% 'eventdata' - [struct|cell] additional data to store with each event
% For example { 'condition' 'a' 'run' 1 }
% 'extractepochs' - ['on'|'off'] extract data epochs ('on') or simply
% insert events ('off'). Default is 'on'.
%
% Outputs:
% EEGout - the input EEG structure separated into consecutive
% epochs.
%
% See also: POP_EDITEVENTVALS, POP_EPOCH, RMBASE
%
% Authors: Hilit Serby, Arnaud Delorme & Scott Makeig, SCCN/INC/UCSD, Sep 02, 2005
% Copyright (C) Hilit Serby, SCCN, INC, UCSD, Sep 02, 2005, hilit@sccn.ucsd.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.
function EEG = eeg_regepochs(EEG, varargin)
if nargin < 1
help eeg_regepochs;
return;
end
if length(EEG) > 1
EEG = pop_mergeset(EEG, [1:length(EEG)]);
end
% test input variables
% --------------------
if ~isstruct(EEG) || ~isfield(EEG,'event')
error('first argument must be an EEG structure')
elseif EEG.trials > 1
error('input dataset must be continuous data (1 epoch)');
end
if nargin > 1 && ~ischar(varargin{1})
options = {};
if nargin >= 2, options = { options{:} 'recurrence' varargin{1} }; end
if nargin >= 3, options = { options{:} 'limits' varargin{2} }; end
if nargin >= 4, options = { options{:} 'rmbase' varargin{3} }; end
else
options = varargin;
end
g = finputcheck(options, { 'recurrence' 'real' [] 1;
'limits' 'real' [] [0 1];
'rmbase' 'real' [] 0;
'eventtype' 'string' {} 'X';
'eventdata' {'cell' 'struct'} [] {};
'extractepochs' 'string' { 'on','off' } 'on' }, 'eeg_regepochs');
if ischar(g), error(g); end
if g.recurrence < 0 || g.recurrence > EEG.xmax
error('recurrence_interval out of bounds');
end
if iscell(g.eventdata)
g.eventdata = struct(g.eventdata{:});
end
if nargin < 3
g.limits = [0 g.recurrence];
end
if length(g.limits) ~= 2 || g.limits(2) <= g.limits(1)
error('epoch limits must be a 2-vector [minsec maxsec]')
end
% calculate number of events to add
% ---------------------------------
bg = 0; % beginning of data
en = EEG.xmax; % end of data in sec
nu = floor((EEG.xmax+1/EEG.srate)/g.recurrence); % number of type 'X' events to add and epoch on
% bg = EEG.event(1).latency/EEG.srate; % time in sec of first event
% en = EEG.event(end).latency/EEG.srate; % time in sec of last event
% nu = length((bg+g.recurrence):g.recurrence:(en-g.recurrence)); % number of 'X' events, one every 'g.recurrence' sec
if nu < 1
error('specified recurrence_interval too long')
end
% print info on commandline
% -------------------------
eplength = g.limits(2)-g.limits(1);
fprintf('The input dataset will be split into %d epochs of %g s\n',nu,eplength);
if (eplength-g.recurrence)/eplength*100 > 0
fprintf('Epochs will overlap by %2.0f%%.\n',(eplength-g.recurrence)/eplength*100);
end
% insert events and urevents at the end of the current (ur)event tables
% ---------------------------------------------------------------------
fprintf('Inserting %d type ''%s'' events: ', nu, g.eventtype);
nevents = length(EEG.event);
% convert all event types to strings
for k = 1:nevents
EEG.event(k).type = num2str(EEG.event(k).type);
end
nurevents = length(EEG.urevent);
for k = 1:nu
if rem(k,40)
fprintf('.')
else
fprintf('%d',k)
end
if k==40 || ( k>40 && ~rem(k-40,70))
fprintf('\n');
end
EEG.event(nevents+k).type = g.eventtype;
EEG.event(nevents+k).latency = g.recurrence*(k-1)*EEG.srate+1;
EEG.urevent(nurevents+k).type = g.eventtype;
EEG.urevent(nurevents+k).latency = g.recurrence*(k-1)*EEG.srate+1;
EEG.event(nevents+k).urevent = nurevents+k;
% add additional information
if ~isempty(g.eventdata)
fNames = fieldnames(g.eventdata);
for iField = 1:length(fNames)
EEG.event( nevents+k).(fNames{iField}) = g.eventdata.(fNames{iField});
EEG.urevent(nurevents+k).(fNames{iField}) = g.eventdata.(fNames{iField});
end
end
end
fprintf('\n');
% sort the events based on their latency
% --------------------------------------
fprintf('Sorting the event table.\n');
EEG = pop_editeventvals( EEG, 'sort', {'latency' 0});
% split the dataset into epochs
% ------------------------------
if strcmpi(g.extractepochs, 'on')
fprintf('Splitting the data into %d %2.2f-s epochs\n',nu,eplength);
setname = sprintf('%s - %g-s epochs', EEG.setname, g.recurrence);
EEG = pop_epoch( EEG, { g.eventtype }, g.limits, 'newname', ...
setname, 'epochinfo', 'yes');
% baseline zero the epochs
% ------------------------
if ~isnan(g.rmbase) && g.limits(1) < g.rmbase
fprintf('Removing the pre %3.2f second baseline mean of each epoch.\n', g.rmbase);
EEG = pop_rmbase( EEG, [g.limits(1) g.rmbase]*1000);
end
end;