-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtempfilter.m
546 lines (492 loc) · 15.5 KB
/
tempfilter.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
function [fd, X, w] = tempfilter(d, opts)
% tempfilter - filter temporal data
%
% FORMAT: [fd, X, w] = tempfilter(d, opts)
%
% Input fields:
%
% d data to filter
% opts mandatory struct but with optional fields
% .nuisreg nuisance regressors, also added to filter matrix
% .orthpoly orthogonalize polynomials (faster computation later)
% .pbar handle to xfigure::Progress or xprogress object
% .prange range to display progress over (default: [0, 1])
% .spat enable spatial filtering (default: false)
% .spkern smoothing kernel in sampling units (default: [2, 2, 2])
% .tdim temporal filter dimension (default: 1)
% .temp enable temporal filtering (default: true)
% .tempdct DCT-based filtering (min. wavelength, default: Inf)
% .tempdt detrend (default: true, is overriden by dct/sc)
% .temphp temporal highpass (inv. smoothing) in units (def: 0)
% .temphpr regress highpass instead of subtract (default: false)
% .templp temporal lowpass (smoothing) kernel in units (def: 0)
% .temppoly set of orthogonal polynomials (default: 0)
% .tempsc sin/cos set of frequencies (number of pairs, def: 0)
% .trobust perform temporal filtering robustly (default: false)
%
% Output fields:
%
% fd filtered data (in input datatype, scaled if needed)
% X filtering matrix
% w weights of robust regression
%
% Note: low-pass filtering now uses flexinterpn (speed gain) and the
% smoothing kernel is determined using a R=8 sinc window interpolator
% Version: v0.9d
% Build: 14072317
% Date: Jul-23 2014, 5:44 PM EST
% Author: Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://neuroelf.net/
% Copyright (c) 2010, 2011, 2014, Jochen Weber
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * 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.
% * Neither the name of Columbia University nor the
% names of its contributors may be used to endorse or promote products
% derived from this software without specific prior written permission.
%
% 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 HOLDERS 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.
% argument check
if nargin < 2 || ...
(~isnumeric(d) && ...
~istransio(d)) || ...
~isstruct(opts) || ...
numel(opts) ~= 1
error( ...
'neuroelf:BadArgument', ...
'Invalid or missing argument in call.' ...
);
end
if ~isfield(opts, 'nuisreg') || ...
~isa(opts.nuisreg, 'double') || ...
ndims(opts.nuisreg) ~= 2 || ...
any(isinf(opts.nuisreg(:)) | isnan(opts.nuisreg(:)))
opts.nuisreg = [];
end
if ~isfield(opts, 'orthpoly') || ...
(~islogical(opts.orthpoly) && ...
~isnumeric(opts.orthpoly)) || ...
numel(opts.orthpoly) ~= 1
opts.orthpoly = false;
else
opts.orthpoly = (double(opts.orthpoly) ~= 0);
end
if ~isfield(opts, 'pbar') || ...
numel(opts.pbar) ~= 1 || ...
~any(strcmpi(class(opts.pbar), {'xfigure', 'xprogress'}))
opts.pbar = [];
end
if ~isfield(opts, 'prange') || ...
~isa(opts.prange, 'double') || ...
numel(opts.prange) ~= 2 || ...
any(isinf(opts.prange) | isnan(opts.prange) | opts.prange < 0 | opts.prange > 1)
opts.prange = [0, 1];
else
opts.prange = sort(opts.prange(:))';
if opts.prange(2) == opts.prange(1)
opts.prange(2) = opts.prange(1) + sqrt(eps);
end
end
if ~isfield(opts, 'tdim') || ...
~isa(opts.tdim, 'double') || ...
numel(opts.tdim) ~= 1 || ...
~any(opts.tdim == (1:ndims(d)))
opts.tdim = 1;
end
if numel(d) == size(d, opts.tdim)
opts.spat = false;
end
if ~isfield(opts, 'spat') || ...
(~islogical(opts.spat) && ...
~isnumeric(opts.spat)) || ...
numel(opts.spat) ~= 1
opts.spat = false;
else
opts.spat = (opts.spat ~= 0);
end
if ~isfield(opts, 'spkern') || ...
~isa(opts.spkern, 'double') || ...
numel(opts.spkern) ~= 3 || ...
any(isinf(opts.spkern) | isnan(opts.spkern) | opts.spkern < 0 | opts.spkern > 10)
opts.spkern = [2, 2, 2];
else
opts.spkern = opts.spkern(:)';
end
if all(opts.spkern) < 0.5
opts.spat = false;
end
if ~isfield(opts, 'temp') || ...
(~islogical(opts.temp) && ...
~isnumeric(opts.temp)) || ...
numel(opts.temp) ~= 1
opts.temp = true;
else
opts.temp = (opts.temp ~= 0);
end
if ~isfield(opts, 'tempdct') || ...
~isa(opts.tempdct, 'double') || ...
numel(opts.tempdct) ~= 1 || ...
isinf(opts.tempdct) || ...
isnan(opts.tempdct) || ...
opts.tempdct < 4
opts.tempdct = Inf;
end
if ~isfield(opts, 'tempdt') || ...
~islogical(opts.tempdt) || ...
numel(opts.tempdt) ~= 1
opts.tempdt = true;
end
if ~isfield(opts, 'temphp') || ...
~isa(opts.temphp, 'double') || ...
numel(opts.temphp) ~= 1 || ...
isinf(opts.temphp) || ...
isnan(opts.temphp) || ...
opts.temphp < 0
opts.temphp = 0;
elseif opts.temphp > 128
warning(...
'neuroelf:LongComputation', ...
'Overlarge high-pass filter. Long computation, try using different method.' ...
);
end
if ~isfield(opts, 'temphpr') || ...
~islogical(opts.temphpr) || ...
numel(opts.temphpr) ~= 1
opts.temphpr = false;
end
if ~isfield(opts, 'templp') || ...
~isa(opts.templp, 'double') || ...
numel(opts.templp) ~= 1 || ...
isinf(opts.templp) || ...
isnan(opts.templp) || ...
opts.templp <= 0
opts.templp = 0;
elseif opts.templp > 128
warning(...
'neuroelf:LongComputation', ...
'Overlarge low-pass filter. Long computation, try using different method.' ...
);
end
if ~isfield(opts, 'temppoly') || ...
~isa(opts.temppoly, 'double') || ...
numel(opts.temppoly) ~= 1 || ...
isinf(opts.temppoly) || ...
isnan(opts.temppoly) || ...
opts.temppoly < 0 || ...
opts.temppoly >= (size(d, opts.tdim) - 1)
opts.temppoly = 0;
else
opts.temppoly = floor(opts.temppoly);
end
if ~isfield(opts, 'tempsc') || ...
~isa(opts.tempsc, 'double') || ...
numel(opts.tempsc) ~= 1 || ...
~any(opts.tempsc == (1:floor(0.5 * (size(d, opts.tdim)-1))))
opts.tempsc = 0;
end
if isinf(opts.tempdct) && ...
~opts.tempdt && ...
opts.temphp == 0 && ...
opts.templp == 0 && ...
opts.temppoly == 0 && ...
opts.tempsc == 0
opts.temp = false;
elseif opts.temppoly > 0
opts.tempdct = Inf;
opts.tempdt = false;
opts.tempsc = 0;
elseif opts.tempsc > 0
opts.tempdct = Inf;
opts.tempdt = false;
end
if ~isfield(opts, 'trobust') || ...
~islogical(opts.trobust) || ...
numel(opts.trobust) ~= 1
opts.trobust = false;
end
% return early if neither spatial/temporal filtering and no nuisreg
if ~opts.spat && ...
~opts.temp && ...
isempty(opts.nuisreg)
fd = d;
return;
end
% initialize filtering matrix
X = zeros(size(d, opts.tdim), 0);
% get data
dt = class(d);
if istransio(d)
if ~strcmp(dt, 'double')
fd = single(resolve(d));
else
fd = resolve(d);
end
elseif ~strcmp(dt, 'double')
fd = single(d);
else
fd = d;
end
% permute if necessary
di = opts.tdim;
tperm = 1:ndims(fd);
if di > 1
tperm = [di, tperm(tperm ~= di)];
[sperm, fperm] = sort(tperm(:));
fd = permute(fd, tperm);
end
% get original sizing and number of time points
vs = size(fd);
if numel(vs) < numel(tperm)
vs(end+1:numel(tperm)) = 1;
end
nv = vs(1);
% use nuisance regressors ?
if size(opts.nuisreg, 1) ~= nv
opts.nuisreg = zeros(nv, 0);
elseif ~isempty(opts.nuisreg)
% remove invalid entries
opts.nuisreg(:, any(isinf(opts.nuisreg) | isnan(opts.nuisreg)) | ...
sum(abs(diff(opts.nuisreg))) < sqrt(eps)) = [];
% z-transform (for matrix)
opts.nuisreg = ztrans(opts.nuisreg);
% and force temporal filtering
opts.temp = true;
end
% DCT overrides detrending
if opts.temp && ...
~isinf(opts.tempdct)
opts.tempdct = floor(2 * nv / opts.tempdct);
opts.tempdt = false;
end
% spatial filter first if needed
if opts.spat
% if too few dims, make it so
fds = size(fd);
if numel(fds) < 4
fd = reshape(fd, [fds(1), ones(1, 4 - numel(fds)), fds(2:end)]);
opts.spkern(1:(4 - numel(fds))) = 0;
end
ssr = size(fd);
ssf = ssr(2:end);
ssr(1) = 1;
% use smoothdata3 to iterate over volumes
for vc = 1:nv
fd(vc, :, :, :, :) = reshape( ...
smoothdata3(reshape(fd(vc, :, :, :, :), ssf), opts.spkern), ssr);
end
% reshaping again?
if numel(fds) < 4
fd = reshape(fd, fds);
end
end
% temporal filter
if opts.temp
% reshape if necessary
if numel(vs) > 2
fd = reshape(fd, nv, prod(vs(2:end)));
end
% only detrend
if opts.tempdt
% build X
X = [ones(nv, 1), (-1:(2/(nv-1)):1)', opts.nuisreg];
% DCT-based filtering
elseif ~isinf(opts.tempdct)
% build X
X = [ones(nv, 1), zeros(nv, opts.tempdct), opts.nuisreg];
n = 0:(nv - 1);
for dc = 1:opts.tempdct
X(:, dc + 1) = cos(pi * (2 * n + 1) * dc / (2 * nv));
end
% (legendre) polynomial based filtering
elseif opts.temppoly > 0
% build X
X = [ones(nv, 1), zeros(nv, opts.temppoly), opts.nuisreg];
n = (1 / nv) .* ((-nv + 1):2:(nv - 1))';
n = 0.5 .* (abs(n) + abs(n(end:-1:1)));
n(1:(floor(0.5*nv))) = -n(1:(floor(0.5*nv)));
if mod(nv, 2) ~= 0
n(ceil(0.5*nv)) = 0;
end
X(:, 2) = n;
for dc = 2:opts.temppoly
X(:, dc + 1) = (1 / (dc + 1)) .* ...
((2 * dc + 1) .* n .* X(:, dc) - dc .* X(:, dc - 1));
end
% enforce orthogonality
if opts.orthpoly
for dc = opts.temppoly:-1:2
X(:, dc + 1) = X(:, dc + 1) - X(:, 1:dc) * ...
((X(:, 1:dc)' * X(:, 1:dc)) \ (X(:, 1:dc)' * X(:, dc + 1)));
end
end
% re-scale
X(:, 2:end) = X(:, 2:end) ./ (ones(nv, 1) * max(abs(X(:, 2:end)), [], 1));
% sin/cos set filtering
elseif opts.tempsc > 0
% build X
X = [ones(nv, 1), zeros(nv, 2 * opts.tempsc), opts.nuisreg];
n = 0:(nv - 1);
for pc = 1:opts.tempsc
X(:, (pc * 2)) = sin(pi * (2 * n + 1) * pc / nv);
X(:, (pc * 2 + 1)) = cos(pi * (2 * n + 1) * pc / nv);
end
% only nuisance regressors
else
% simply use those
X = [ones(nv, 1), opts.nuisreg];
end
% perform high-pass filter first
if opts.temphp > 0
% for regression
if opts.temphpr
% perform lowpass filter
fdl = ztrans(tempfilter(fd, struct('tempdt', false, 'templp', opts.temphp)));
% then regress out
b = sum((1 / nv) .* (fdl .* ztrans(fd)));
fd = fd - repmat(b, nv, 1) .* fdl;
else
% subtraction (simply inverse of low-pass filter)
fd = ones(size(fd, 1), 1) * mean(fd, 1) + ...
(fd - tempfilter(fd, struct('tempdt', false, 'templp', opts.temphp)));
end
end
% perform regression
if size(X, 2) > 1
if opts.trobust
if nargout < 3
b = fitrobustbisquare_img(X, fd', [], [], opts)';
else
[b, w] = fitrobustbisquare_img(X, fd', [], [], opts);
b = b';
w = reshape(w', vs);
if di > 1
w = permute(w, fperm');
end
end
else
b = inv(X' * X);
b(abs(b) < sqrt(eps)) = 0;
if sum(b(:) ~= 0) <= (0.25 * numel(b))
b = sparse(b);
end
b = b * (X' * double(fd));
if nargout > 2
if di > 1
w = ones(vs(fperm));
else
w = ones(vs);
end
end
end
else
b = 0;
if nargout > 2
if di > 1
w = ones(vs(fperm));
else
w = ones(vs);
end
end
end
% keep mean
b(1, :) = 0;
% subtract from data
if size(b, 1) > 1
fd = fd - X * b;
end
% low-pass filter?
if opts.templp > 0
% get kernel
if opts.templp > 3
k = smoothkern(opts.templp, 1.42e-5 / sqrt(opts.templp), ...
false, 'lanczos8');
else
k = smoothkern(opts.templp, 1e-6, false, 'lanczos8');
end
% use flexinterpn to do the smoothing
if size(fd, 2) > 1
ficrd = [Inf, Inf; 1, 1; 1, 1; size(fd)];
fikern = {k, [0;1;0]};
fiksiz = {1, 1};
else
ficrd = [Inf; 1; 1; numel(fd)];
fikern = k;
fiksiz = 1;
end
% use flexinterpn
fd = flexinterpn(fd, ficrd, fikern, fiksiz);
end
% reshape again
fd = reshape(fd, vs);
% remove mean from filter matrix
if nargout > 1
X(:, 1) = [];
end
end
% datatype
if ~strcmp(dt, class(fd))
% if not single
if ~strcmpi(dt, 'single')
% get minmaxmean
mmm = minmaxmean(fd);
rng = eps + (mmm(2) - mmm(1));
% then recompute
switch(dt)
case {'int16'}
if mmm(1) < -32768 || ...
mmm(2) >= 32767.5
fd = -32768 + (65535 / rng) * (fd - mmm(1));
end
case {'int32'}
if mmm(1) < -2147483648 || ...
mmm(2) >= 2147483647.5
fd = -2147483648 + (4294967295 / rng) * (fd - mmm(1));
end
case {'int8'}
if mmm(1) < -128 || ...
mmm(2) >= 127.5
fd = -128 + (255 / rng) * (fd - mmm(1));
end
case {'uint16'}
if mmm(1) < 0 || ...
mmm(2) >= 65535.5
fd = (65535 / rng) * (fd - mmm(1));
end
case {'uint32'}
if mmm(1) < 0 || ...
mmm(2) >= 4294967295.5
fd = (4294967295 / rng) * (fd - mmm(1));
end
case {'uint8'}
if mmm(1) < 0 || ...
mmm(2) >= 255.5
fd = (255 / rng) * (fd - mmm(1));
end
end
% round and cast
eval(['fd=' dt '(round(fd));']);
% for single
else
% just convert
fd = single(fd);
end
end
% re-permute if necessary
if di > 1
fd = permute(fd, fperm');
end