-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnt_detrend.m
316 lines (270 loc) · 8.75 KB
/
nt_detrend.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
function [y,w,r]=nt_detrend(x,order,w0,basis,thresh,niter,wsize)
%[y,w,r]=nt_detrend(x,order,w,basis,thresh,niter,wsize) - robustly remove trend
%
% y: detrended data
% w: updated weights
% r: basis matrix used
%
% x: raw data
% order: order of polynomial or number of sin/cosine pairs
% w0: weights
% basis: 'polynomials' [default] or 'sinusoids', or user-provided matrix
% thresh: threshold for outliers [default: 3 sd]
% niter: number of iterations [default: 3]
% wsize: window size for local detrending [default: all]
%
% This NEW (circa Oct 2019) version of detrend allows detrending to be
% applied to smaller overlapping windows, which are then stitched together
% using overlap-add. This is not described in the paper.
nt_greetings;
%% arguments
if nargin<2; error('!'); end
if nargin<3; w0=[]; end
if nargin<4||isempty(basis); basis='polynomials'; end
if nargin<5||isempty(thresh); thresh=3; end
if nargin<6||isempty(niter); niter=3; end
if nargin<7; wsize=[]; end
if iscell(x)
if ~isempty(w0); error('not implemented'); end
y={}; w={}; r={};
for iTrial=1:numel(x);
[y{iTrial},w{iTrial},r{iTrial}]=nt_detrend(x{iTrial},order,w0,basis,thresh,niter,wsize);
end
return
end
w=w0;
% if ~isempty(w)
% w=w(:);
% if numel(w)<size(x,1)
% % assume that w contains indices, set them to 1
% idx=w;
% w=zeros(size(x,1),1);
% w(idx)=1;
% end
% end
if isempty(wsize) || ~wsize
% standard detrending (trend fit to entire data)
[y,w,r]=nt_detrend_helper(x,order,w,basis,thresh,niter);
else
wsize=2*floor(wsize/2);
% do some sanity checks because many parameters
if numel(order)>1; error('!'); end
if ~isempty(w) && ~(size(w,1)==size(x,1)) ; disp(size(w)); error('!'); end
if ~(isempty(basis) || isstring(basis) || ~(isnumeric(basis)&&size(basis,1)==size(x,1))); error('!'); end
if thresh==0; error('thresh=0 is not what you want...'); end % common mistake
if numel(thresh)>1; error('!'); end
if numel(niter)>1; error('!'); end
dims=size(x); nchans=dims(2);
x=x(:,:); % concatenates dims >= 2
w=w(:,:);
if isempty(w); w=ones(size(x)); end
if size(w,2)==1; w=repmat(w,1,size(x,2)); end
% (1) divide into windows, (2) detrend each, (3) stitch together, (4)
% estimate w
for iIter=1:niter
y=zeros(size(x));
trend=zeros(size(x));
a=zeros(size(x,1),1);
% figure(1); clf
offset=0;
while true
start=offset+1;
stop=min(size(x,1),offset+wsize);
% if not enough valid samples grow window:
counter=5;
while any (sum(min(w(start:stop),2))) <wsize
if counter <= 0 ; break; end
start=max(1,start-wsize/2);
stop=min(size(x,1),stop+wsize/2);
counter=counter-1;
end
if rem(stop-start+1,2)==1; stop=stop-1; end
wsize2=stop-start+1;
% detrend this window
NITER=1;
yy=nt_detrend_helper(x(start:stop,:),order,w(start:stop,:),basis,thresh,NITER);
% triangular weighting
if start==1
b=[ones(1,wsize2/2)*wsize2/2, wsize2/2:-1:1]';
elseif stop==size(x,1)
b=[1:wsize2/2, ones(1,wsize2/2)*wsize2/2]';
else
b=[1:wsize2/2, wsize2/2:-1:1]';
end
% overlap-add to output
y(start:stop,:)=y(start:stop,:)+bsxfun(@times,yy,b);
trend(start:stop,:)=trend(start:stop,:)+bsxfun(@times,x(start:stop,:)-yy,b);
a(start:stop,1)=a(start:stop)+b;
offset=offset+wsize/2;
if offset>size(x,1)-wsize/2; break; end
end
if stop<size(x,1); y(end,:)=y(end-1,:); a(end,:)=a(end-1,:); end; % last sample can be missed
y=bsxfun(@times,y,1./a); y(find(isnan(y)))=0;
trend=bsxfun(@times,trend,1./a); trend(find(isnan(trend)))=0;
% find outliers
d=x-trend;
if ~isempty(w); d=bsxfun(@times,d,w); end
ww=ones(size(x));
ww(find(abs(d)>thresh*repmat(std(d),size(x,1),1))) = 0;
clear d
% update weights
if isempty(w);
w=ww;
else
w=min(w,ww);
end
clear ww;
end % for iIter...
w=[];r=[]; % not informative
end % if isempty(wsize)...
if ~nargout
% don't return, just plot
subplot 411; plot(x); title('raw'); xlim([1,size(x,1)])
subplot 412; plot(y); title('detrended'); xlim([1,size(x,1)])
subplot 413; plot(x-y); title('trend'); xlim([1,size(x,1)])
subplot 414; nt_imagescc(w'); title('weight');
clear y w r
end
end
%% Original version of detrend (no windows) is called by new version (windows)
function [y,w,r]=nt_detrend_helper(x,order,w,basis,thresh,niter)
%[y,w,r]=nt_detrend(x,order,w,basis,thresh,niter) - robustly remove trend
%
% y: detrended data
% w: updated weights
% r: basis matrix used
%
% x: raw data
% order: order of polynomial or number of sin/cosine pairs
% w: weights
% basis: 'polynomials' [default] or 'sinusoids', or user-provided matrix
% thresh: threshold for outliers [default: 3 sd]
% niter: number of iterations [default: 3]
%
% Noise tools
% See nt_regw().
%
% The data are fit to the basis using weighted least squares. The weight is
% updated by setting samples for which the residual is greater than 'thresh'
% times its std to zero, and the fit is repeated at most 'niter'-1 times.
%
% The choice of order (and basis) determines what complexity of the trend
% that can be removed. It may be useful to first detrend with a low order
% to avoid fitting outliers, and then increase the order.
%
% Examples:
% Fit linear trend, ignoring samples > 3*sd from it, and remove:
% y=nt_detrend(x,1);
% Fit/remove polynomial order=5 with initial weighting w, threshold = 4*sd:
% y=nt_detrend(x,5,w,[],4);
% Fit/remove linear then 3rd order polynomial:
% [y,w]=nt_detrend(x,1);
% [yy,ww]=nt_detrend(y,3);
%
nt_greetings;
% arguments
if nargin<2; error('!'); end
if nargin<3; w=[]; end
if nargin<4||isempty(basis); basis='polynomials'; end
if nargin<5||isempty(thresh); thresh=3; end
if nargin<6||isempty(niter); niter=3; end
if thresh==0; error('thresh=0 is not what you want...'); end % common mistake
dims=size(x);
x=x(:,:); % concatenates dims >= 2
w=w(:,:);
if size(w,2)==1; w=repmat(w,1,size(x,2)); end
%% regressors
if isnumeric(basis)
r=basis;
else
switch basis
case 'polynomials'
r=zeros(size(x,1),numel(order));
lin=linspace(-1,1,size(x,1));
for k=1:order
r(:,k)=lin.^k;
end
case 'sinusoids'
r=zeros(size(x,1),numel(order)*2);
lin=linspace(-1,1,size(x,1));
for k=1:order
r(:,2*k-1)=sin(2*pi*k*lin/2);
r(:,2*k)=cos(2*pi*k*lin/2);
end
otherwise
error('!');
end
end
% remove trends
% The tricky bit is to ensure that weighted means are removed before
% calculating the regression (see nt_regw).
for iIter=1:niter
% weighted regression on basis
[~,y]=nt_regw(x,r,w);
% find outliers
d=x-y;
if ~isempty(w); d=bsxfun(@times,d,w); end
ww=ones(size(x));
ww(find(abs(d)>thresh*repmat(std(d),size(x,1),1))) = 0;
% update weights
if isempty(w);
w=ww;
else
w=min(w,ww);
end
clear ww;
end
y=x-y;
y=reshape(y,dims);
w=reshape(w,dims);
end
%% test code
function test_me
if 0
% basic
x=(1:100)'; x=x+ randn(size(x));
WSIZE=30;
y=nt_detrend2(x,1,[],[],[],[],WSIZE);
figure(1); clf; plot([x,y]);
end
if 0
% basic
x=(1:100)'; x=x+ randn(size(x));
x(40:50)=0;
WSIZE=30;
[yy,ww]=nt_detrend2(x,1,[],[],2,[],WSIZE);
[y,w]=nt_detrend(x,1);
figure(1); clf; subplot 211;
plot([x,y,yy]);
subplot 212; plot([w,ww],'.-');
end
if 0
% detrend biased random walk
x=cumsum(randn(1000,1)+0.1);
WSIZE=200;
[y1,w1]=nt_detrend(x,1,[]);
[y2,w2]=nt_detrend2(x,1,[],[],[],[],WSIZE);
figure(1); clf;
plot([x,y1,y2]); legend('before', 'after');
end
if 0
% weights
x=linspace(0,100,1000)';
x=x+3*randn(size(x));
x(1:100,:)=100;
w=ones(size(x)); w(1:100,:)=0;
y=nt_detrend2(x,3,[],[],[],[],WSIZE);
yy=nt_detrend2(x,3,w,[],[],[],WSIZE);
figure(1); clf; plot([x,y,yy]); legend('before', 'unweighted','weighted');
end
if 0
[p,x]=nt_read_data('/data/meg/theoldmanandthesea/eeg/mc/MC_aespa_speech_43.mat'); x=x'; x=x(:,1:128); %x=x(1:10000,:);
%[p,x]=nt_read_data('/data/meg/arzounian/ADC_DA_140521_p20/ADC_DA_140521_p20_01_calib'); x=x'; x=x(1:10000,:);
x=nt_demean(x);
figure(1);
nt_detrend(x,3);
figure(2);
WSIZE=1000;
nt_detrend2(x(:,:),3,[],[],[],[],WSIZE);
end
end