/
ft_resampledata.m
443 lines (379 loc) · 16.7 KB
/
ft_resampledata.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
function [data] = ft_resampledata(cfg, data)
% FT_RESAMPLEDATA performs a resampling or downsampling of the data to a specified
% new sampling frequency, or an inperpolation of the data measured with one sampling
% frequency to another. The latter is useful when merging data measured on two
% different acquisition devices, or when the samples in two recordings are slightly
% shifted.
%
% Use as
% [data] = ft_resampledata(cfg, data)
%
% The data should be organised in a structure as obtained from the FT_PREPROCESSING
% function. The configuration should contain
% cfg.resamplefs = frequency at which the data will be resampled
% cfg.method = resampling method, see RESAMPLE, DOWNSAMPLE, DECIMATE (default = 'resample')
% cfg.detrend = 'no' or 'yes', detrend the data prior to resampling (no default specified, see below)
% cfg.demean = 'no' or 'yes', whether to apply baseline correction (default = 'no')
% cfg.baselinewindow = [begin end] in seconds, the default is the complete trial (default = 'all')
% cfg.feedback = 'no', 'text', 'textbar', 'gui' (default = 'text')
% cfg.trials = 'all' or a selection given as a 1xN vector (default = 'all')
% cfg.sampleindex = 'no' or 'yes', add a channel with the original sample indices (default = 'no')
%
% Rather than resapling to a specific sampling frequency, you can also specify a time
% axis on which you want the data to be resampled. This is useful for merging data
% from two acquisition devices, after resampledata you can call FT_APPENDDATA to
% concatenate the channels from the different acquisition devices.
% cfg.time = cell-array with one time axis per trial (i.e., from another dataset)
% cfg.method = interpolation method, see INTERP1 (default = 'pchip')
% cfg.extrapval = extrapolation behaviour, scalar value or 'extrap' (default is as in INTERP1)
%
% The default method is 'resample' when you specify cfg.resamplefs, and 'pchip' when
% you specify cfg.time.
%
% The methods 'resample' and 'decimate' automatically apply an anti-aliasing low-pass
% filter. You can also explicitly specify an anti-aliasing low pass filter. This is
% particularly adviced when downsampling using the 'downsample' method, but also when
% strong noise components are present just above the new Nyquist frequency.
% cfg.lpfilter = 'yes' or 'no' (default = 'no')
% cfg.lpfreq = scalar value for low pass frequency (there is no default, so needs to be always specified)
% cfg.lpfilttype = string, filter type (default is set in ft_preproc_lowpassfilter)
% cfg.lpfiltord = scalar, filter order (default is set in ft_preproc_lowpassfilter)
%
% More documentation about anti-alias filtering can be found in this <a href="matlab:
% web('https://www.fieldtriptoolbox.org/faq/resampling_lowpassfilter')">FAQ</a> on the FieldTrip website.
%
% To facilitate data-handling and distributed computing you can use
% cfg.inputfile = ...
% cfg.outputfile = ...
% If you specify one of these (or both) the input data will be read from a *.mat
% file on disk and/or the output data will be written to a *.mat file. These mat
% files should contain only a single variable, corresponding with the
% input/output structure.
%
% See also FT_PREPROCESSING, FT_APPENDDATA, FT_PREPROC_LOWPASSFILTER, RESAMPLE, DOWNSAMPLE, DECIMATE, INTERP1
% Copyright (C) 2003-2006, FC Donders Centre, Markus Siegel
% Copyright (C) 2004-2023, FC Donders Centre, Robert Oostenveld
% Copyright (C) 2022, DCCN, Jan-Mathijs Schoffelen
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% 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$
% these are used by the ft_preamble/ft_postamble function and scripts
ft_revision = '$Id$';
ft_nargin = nargin;
ft_nargout = nargout;
% do the general setup of the function
ft_defaults
ft_preamble init
ft_preamble debug
ft_preamble loadvar data
ft_preamble provenance data
% the ft_abort variable is set to true or false in ft_preamble_init
if ft_abort
return
end
% ft_checkdata is done further down
% check if the input cfg is valid for this function
cfg = ft_checkconfig(cfg, 'forbidden', {'trial'}); % prevent accidental typos, see issue 1729
cfg = ft_checkconfig(cfg, 'renamed', {'blc', 'demean'});
cfg = ft_checkconfig(cfg, 'renamed', {'resamplemethod', 'method'});
cfg = ft_checkconfig(cfg, 'renamed', {'fsample', 'resamplefs'});
% set the defaults
cfg.method = ft_getopt(cfg, 'method', []);
cfg.resamplefs = ft_getopt(cfg, 'resamplefs', []);
cfg.time = ft_getopt(cfg, 'time', {});
cfg.factor = ft_getopt(cfg, 'factor', {});
cfg.detrend = ft_getopt(cfg, 'detrend', 'no');
cfg.demean = ft_getopt(cfg, 'demean', 'no');
cfg.baselinewindow = ft_getopt(cfg, 'baselinewindow', 'all');
cfg.feedback = ft_getopt(cfg, 'feedback', 'text');
cfg.trials = ft_getopt(cfg, 'trials', 'all', 1);
cfg.sampleindex = ft_getopt(cfg, 'sampleindex', 'no');
cfg.extrapval = ft_getopt(cfg, 'extrapval', []);
cfg.lpfilter = ft_getopt(cfg, 'lpfilter');
% store original datatype
convert = ft_datatype(data);
% check if the input data is valid for this function, this will convert it to raw if needed
data = ft_checkdata(data, 'datatype', {'raw+comp', 'raw'}, 'feedback', 'yes');
if isempty(cfg.method)
if ~isempty(cfg.time)
% see INTERP1, shape-preserving piecewise cubic interpolation
cfg.method = 'pchip';
elseif ~isempty(cfg.resamplefs)
% see RESAMPLE
cfg.method = 'resample';
else
ft_error('you must specify cfg.method');
end
end
usefsample = any(strcmp(cfg.method, {'resample', 'downsample', 'decimate', 'mean', 'median'}));
usetime = ~usefsample;
% select trials of interest
tmpcfg = keepfields(cfg, {'trials', 'showcallinfo', 'trackcallinfo', 'trackusage', 'trackdatainfo', 'trackmeminfo', 'tracktimeinfo', 'checksize'});
data = ft_selectdata(tmpcfg, data);
% restore the provenance information
[cfg, data] = rollback_provenance(cfg, data);
if strcmp(cfg.sampleindex, 'yes') && isfield(data, 'sampleinfo')
data.label{end+1} = 'sampleindex';
for i=1:size(data.sampleinfo,1)
% this works for one or more trials
data.trial{i}(end+1,:) = data.sampleinfo(i,1):data.sampleinfo(i,2);
end
elseif strcmp(cfg.sampleindex, 'yes')
ft_warning('no sampleinfo present, cannot add sampleindex as channel');
end
% sampleinfo, if present, becomes invalid because of the resampling
if isfield(data, 'sampleinfo')
data = rmfield(data, 'sampleinfo');
end
if usefsample && usetime
ft_error('you should either specify cfg.resamplefs or cfg.time')
end
% remember the original sampling frequency in the configuration
cfg.origfs = double(data.fsample);
% set this to nan, it will be updated later on
data.fsample = nan;
if isempty(cfg.lpfilter), cfg.lpfilter = 'no'; end
dolpfilt = istrue(cfg.lpfilter);
if dolpfilt
cfg.lpfilttype = ft_getopt(cfg, 'lpfilttype');
cfg.lpfiltord = ft_getopt(cfg, 'lpfiltord');
cfg = ft_checkconfig(cfg, 'required', 'lpfreq');
end
if usefsample
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% resample/downsample based on new sampling frequency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ntr = length(data.trial);
nchan = numel(data.label);
ft_progress('init', cfg.feedback, 'resampling data');
[fsorig, fsres] = rat(cfg.origfs./cfg.resamplefs); %account for non-integer fs
cfg.resamplefs = cfg.origfs.*(fsres./fsorig); %get new fs exact
% make sure that the resampled time axes are aligned (this is to avoid rounding
% errors in the time axes). this procedure relies on the fact that resample assumes
% all data outside the data window to be zero anyway. therefore, padding with zeros
% (to the left and right) before resampling does not hurt
begsample = zeros(ntr, 1);
endsample = zeros(ntr, 1);
for itr = 1:ntr
begsample(itr) = round(cfg.origfs * data.time{itr}(1));
endsample(itr) = round(cfg.origfs * data.time{itr}(end));
end
begpad = begsample-min(begsample);
endpad = max(endsample)-endsample;
if any(begpad~=0) || any(endpad~=0)
ft_warning('not all trials have the same time axis; data will be zero-padded prior to resampling to avoid rounding issues in the resampled time axes');
end
if any(strcmp(cfg.method, {'downsample', 'mean', 'median'}))
ft_warning('using cfg.method = ''%s''; only use this if you have applied an anti-aliasing filter prior to downsampling!', cfg.method);
end
if any(strcmp(cfg.method, {'decimate', 'downsample', 'mean', 'median'}))
if mod(fsorig, fsres) ~= 0
ft_error('the new sampling rate needs to be an integer division of the original sampling rate');
end
end
for itr = 1:ntr
ft_progress(itr/ntr, 'resampling data in trial %d from %d\n', itr, ntr);
olddat = data.trial{itr};
oldtim = data.time{itr};
% detrending is in general not recommended
if istrue(cfg.detrend)
if ~strcmp(cfg.baselinewindow, 'all')
olddat = ft_preproc_detrend(olddat, nearest(oldtim, cfg.baselinewindow(1)), nearest(oldtim, cfg.baselinewindow(2)));
else
olddat = ft_preproc_detrend(olddat);
end
end
% remove the mean to avoid edge effects when there's a strong offset, the cfg.demean option is dealt with below
if ~strcmp(cfg.baselinewindow, 'all')
[olddat, bsl] = ft_preproc_baselinecorrect(olddat, nearest(oldtim, cfg.baselinewindow(1)), nearest(oldtim, cfg.baselinewindow(2)));
else
[olddat, bsl] = ft_preproc_baselinecorrect(olddat);
end
if istrue(cfg.lpfilter)
olddat = ft_preproc_lowpassfilter(olddat, cfg.origfs, cfg.lpfreq, cfg.lpfiltord, cfg.lpfilttype);
end
% pad the data with zeros on both sides
olddat = [zeros(nchan, begpad(itr)) olddat zeros(nchan, endpad(itr))];
oldtim = ((begsample(itr)-begpad(itr)):(endsample(itr)+endpad(itr))) / cfg.origfs;
% perform the resampling
if strcmp(cfg.method, 'downsample')
if isa(olddat, 'single')
% temporary convert this trial to double precision
newdat = transpose(single(downsample(double(transpose(olddat)),fsorig/fsres)));
else
newdat = transpose(downsample(transpose(olddat),fsorig/fsres));
end
elseif strcmp(cfg.method, 'resample')
if isa(olddat, 'single')
% temporary convert this trial to double precision
newdat = transpose(single(resample(double(transpose(olddat)),fsres,fsorig)));
else
newdat = transpose(resample(transpose(olddat),fsres,fsorig));
end
elseif strcmp(cfg.method, 'decimate')
if isa(olddat, 'single')
% temporary convert this trial to double precision
newdat = transpose(single(my_decimate(double(transpose(olddat)),fsorig/fsres)));
else
newdat = transpose(my_decimate(transpose(olddat),fsorig/fsres));
end
elseif strcmp(cfg.method, 'mean')
if isa(olddat, 'single')
% temporary convert this trial to double precision
newdat = transpose(single(my_mean(double(transpose(olddat)), fsorig/fsres)));
else
newdat = transpose(my_mean(transpose(olddat), fsorig/fsres));
end
elseif strcmp(cfg.method, 'median')
if isa(olddat, 'single')
% temporary convert this trial to double precision
newdat = transpose(single(my_median(double(transpose(olddat)), fsorig/fsres)));
else
newdat = transpose(my_median(transpose(olddat), fsorig/fsres));
end
else
ft_error('unknown method ''%s''', cfg.method);
end
% add back the mean
if ~strcmp(cfg.demean, 'yes')
nsmp = size(newdat,2);
newdat = newdat + bsl(:,ones(1,nsmp));
end
% compute the new time axis, assuming that it starts at the same time
nsmp = size(newdat,2);
newtim = (0:(nsmp-1))/cfg.resamplefs;
% the middle of the time bin represented by the first samples are not aligned
% the new time axis can be shifted by a sub-sample amount
shift = mean(oldtim) - mean(newtim);
newtim = newtim + shift;
if begpad(itr)>0 || endpad(itr)>0
% un-pad the data
sel = (1+round(begpad(itr)*cfg.resamplefs/cfg.origfs)):(length(newtim)-round(endpad(itr)*cfg.resamplefs/cfg.origfs));
newtim = newtim( sel);
newdat = newdat(:, sel);
end
data.time{itr} = newtim;
data.trial{itr} = newdat;
end % for itr
ft_progress('close');
% specify the new sampling frequency in the output
data.fsample = cfg.resamplefs;
elseif usetime
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% resample based on the specified new time axes for each trial
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isempty(cfg.extrapval)
if strcmp(cfg.method, 'spline') || strcmp(cfg.method, 'pchip')
cfg.extrapval = 'extrap';
else
cfg.extrapval = nan;
end
end
ntr = length(data.trial);
ft_progress('init', cfg.feedback, 'resampling data');
for itr = 1:ntr
ft_progress(itr/ntr, 'resampling data in trial %d from %d\n', itr, ntr);
olddat = data.trial{itr};
oldtim = data.time{itr};
% detrending is in general not recommended
if istrue(cfg.detrend)
if ~strcmp(cfg.baselinewindow, 'all')
olddat = ft_preproc_detrend(olddat, nearest(oldtim, cfg.baselinewindow(1)), nearest(oldtim, cfg.baselinewindow(2)));
else
olddat = ft_preproc_detrend(olddat);
end
end
% always remove the mean to avoid edge effects when there's a strong offset, the cfg.demean option is dealt with below
if ~strcmp(cfg.baselinewindow, 'all')
[olddat, bsl] = ft_preproc_baselinecorrect(olddat, nearest(oldtim, cfg.baselinewindow(1)), nearest(oldtim, cfg.baselinewindow(2)));
else
[olddat, bsl] = ft_preproc_baselinecorrect(olddat);
end
if istrue(cfg.lpfilter)
olddat = ft_preproc_lowpassfilter(olddat, cfg.origfs, cfg.lpfreq, cfg.lpfiltord, cfg.lpfilttype);
end
% perform the resampling
newtim = cfg.time{itr};
if length(oldtim)>1
newdat = interp1(oldtim', olddat', newtim', cfg.method, cfg.extrapval)';
else
newdat = repmat(olddat, [1 numel(newtim)]);
end
% add back the mean
if ~strcmp(cfg.demean, 'yes')
nsmp = size(newdat, 2);
newdat = newdat + bsl(:,ones(1,nsmp));
end
data.trial{itr} = newdat;
data.time{itr} = newtim;
end % for itr
ft_progress('close');
% specify the new sampling frequency in the output
t1 = cfg.time{1}(1);
t2 = cfg.time{1}(2);
data.fsample = 1/(t2-t1);
end % if usefsample or usetime
ft_info('original sampling rate = %d Hz\nnew sampling rate = %d Hz\n', cfg.origfs, data.fsample);
% convert back to input type if necessary
switch convert
case 'timelock'
data = ft_checkdata(data, 'datatype', 'timelock');
otherwise
% keep the output as it is
end
% do the general cleanup and bookkeeping at the end of the function
ft_postamble debug
ft_postamble previous data
ft_postamble provenance data
ft_postamble history data
ft_postamble savevar data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION that decimates along the columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = my_decimate(x, varargin)
[n, m] = size(x);
% decimate the first column
y = decimate(x(:,1), varargin{:});
if m>1
% increase the size of the output matrix
y(:,m) = 0;
% decimate the subsequent columns
for i=2:m
y(:,i) = decimate(x(:,i), varargin{:});
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION that does a block-wise average along the columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = my_mean(x, r)
[n, m] = size(x);
n = n - mod(n,r);
x = x(1:n,:);
x = reshape(x, [r n/r m]);
y = mean(x, 1);
y = reshape(y, [n/r m]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION that does a block-wise median along the columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y = my_median(x, r)
[n, m] = size(x);
n = n - mod(n,r);
x = x(1:n,:);
x = reshape(x, [r n/r m]);
y = median(x, 1);
y = reshape(y, [n/r m]);