-
Notifications
You must be signed in to change notification settings - Fork 7
/
avgref.m
59 lines (53 loc) · 1.73 KB
/
avgref.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
function [data] = avgref(data, sel);
% AVGREF computes the average reference in each column
% [data] = avgref(data)
%
% or it computes the re-referenced data relative to the
% average over the selected channels
% [data] = avgref(data, sel)
% Copyright (C) 1998-2002, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
% for the documentation and details.
%
% FieldTrip 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.
%
% FieldTrip 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 FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id: avgref.m 946 2010-04-21 17:51:16Z roboos $
% determine the dimension of the data
if length(size(data))==3
% multiple epochs
dim=3;
else
% single epoch with multiple channels
dim=2;
end
if nargin==1
% default is to use all channels for average referencing
if dim==3
sel=1:size(data,2);
else
sel=1:size(data,1);
end
end
if dim==3
% the data contains multiple epochs
for epoch=1:size(data,1)
reference = mean(squeeze(data(epoch,sel,:)), 1);
data(epoch,:,:) = squeeze(data(epoch,:,:)) - repmat(reference, size(data,2), 1);
end
else
% the data contains a single epoch
reference = mean(data(sel,:), 1);
data = data - repmat(reference, size(data,1), 1);
end