forked from nigelrogasch/TESA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesa_detrend.m
More file actions
137 lines (121 loc) · 5 KB
/
tesa_detrend.m
File metadata and controls
137 lines (121 loc) · 5 KB
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
% tesa_detrend() - detrends the data by fiting and subtracting a function from
% each channel. Either a linear (fitted and subtratcted from
% each trial), exponential or double exponential function
% (fitted to average and subtracted from each trial) can be fitted.
% Note that the Curve Fitting Toolbox is required
% to run either the exponential fit or the double
% exponential fit.
%
% Usage:
% >> EEG = tesa_detrend( EEG, detrend, timeWin );
%
% Inputs (required):
% EEG - EEGLAB EEG structure
% detrend - string with type of detrend to perform; 'linear' |
% 'exponetial' | 'double'
% timeWin - vector with time range for detrending [t1,t2]
%
% Outputs:
% EEG - EEGLAB EEG structure
%
% Examples:
% EEG = tesa_detrend( EEG, 'linear', [11,500]);
% EEG = tesa_detrend( EEG, 'exponential', [11,500]);
% EEG = tesa_detrend( EEG, 'double', [11,500]);
%
% See also:
% tesa_fastica, tesa_edm, tesa_pcasupress
% Copyright (C) 2016 Nigel Rogasch, Monash University,
% nigel.rogasch@monash.edu
%
% 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 2 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, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
function EEG = tesa_detrend( EEG, detrend, timeWin )
if nargin < 3
error('Not enough input arguments.');
end
%Check that two time points have been specified
if size(timeWin,2) ~= 2
error('Please provide two time values for the detrend window: [start, end]');
end
%Check that time values given for detrend window are in range of data
if timeWin(1,1) < EEG.times(1,1) || timeWin(1,2) > EEG.times(1,end)
error('Time values for the detrend window are out of data range. Note that time values are in ms.');
end
%Check that the type of detrend is give correctly
if ~(strcmp(detrend,'linear') || strcmp(detrend,'exponential') || strcmp(detrend,'double'))
error('The type of detrend to apply is incorrect. Please use either ''linear'', ''exponential'' or ''double''.');
end
%find the time values to detrend
[val1,tp1] = min(abs(EEG.times-timeWin(1,1)));
[val2,tp2] = min(abs(EEG.times-timeWin(1,2)));
%Check that the detrend window does not over lap with removed data
if sum(mean(EEG.data(:,tp1,:),3)) == 0 || sum(mean(EEG.data(:,tp2,:),3)) == 0
error('The window for detrending contains 0s. Please ensure that this window does not overlap with the window of removed data.');
end
%Extract data and times for fitting
data = double(EEG.data(:,tp1:tp2,:));
time = double(EEG.times(1,tp1:tp2));
%##### Fit linear curve ######
if strcmp(detrend,'linear');
dataC = zeros(size(data,1),size(data,2),size(data,3));
fprintf('Start linear detrend.\n');
for a = 1:size(data,1)
for b = 1:size(data,3)
linP = polyfit(time,data(a,:,b),1); %fit the data
dataC(a,:,b) = data(a,:,b) - polyval(linP,time); %correct the data
end
end
%display message
fprintf('Linear detrend complete.\n');
end
%##### Fit exponential curve #####
if strcmp(detrend,'exponential');
v = ver;
if ~sum(strcmp('Curve Fitting Toolbox',extractfield(v,'Name')'))
error('You need the Curve Fitting Toolbox to run this function');
end
dataC = zeros(size(data,1),size(data,2),size(data,3));
fprintf('Start exponential detrend.\n');
dataMean = mean(data,3);
for a = 1:size(data,1)
expP = fit(time',dataMean(a,:)','exp1');% search for solution
for b = 1:size(data,3)
dataC(a,:,b) = data(a,:,b) - expP(time)';% correct the data
end
% fprintf('%s detrend is done\n',EEG.chanlocs(a).labels);
end
fprintf('Exponential detrend complete.\n');
end
%##### Fit double exponential #####
if strcmp(detrend,'double');
v = ver;
if ~sum(strcmp('Curve Fitting Toolbox',extractfield(v,'Name')'))
error('You need the Curve Fitting Toolbox to run this function');
end
dataC = zeros(size(data,1),size(data,2),size(data,3));
fprintf('Start double exponential detrend.\n');
dataMean = mean(data,3);
for a = 1:size(data,1)
exp2P = fit(time',dataMean(a,:)','exp2');
for b = 1:size(data,3)
dataC(a,:,b) = data(a,:,b) - exp2P(time)';% correct the data
end
% fprintf('%s detrend is done\n',EEG.chanlocs(a).labels);
end
fprintf('Double exponential detrend complete.\n');
end
%Insert corrected data
EEG.data(:,tp1:tp2,:) = dataC;
end