[FieldTrip] Trial-masked robust detrending

Schoffelen, J.M. (Jan Mathijs) janmathijs.schoffelen at donders.ru.nl
Tue Nov 25 10:44:46 CET 2025


Hi Helin,

If you want, of course you can implement trial-masked robust detrending in your own code and/or using functionality from NoiseTools and the ADAM toolbox :). If you want to do this by means of a high-level FieldTrip call, in the current state of the codebase, something like the following should - in principle - be possible:

cfg = [];
cfg.custom.funhandle =@fancyfunctionthatdoessomething;
cfg.custom.varargin = opts; % with opts being a structure containing fields that specificy additional options for the function -> this should be changed into a cell array but requires minor tweaks to the fieldtrip code FIXME
dataout = ft_preprocessing(cfg, datain);

if you have a matlab m-file, called fancyfunctionthatdoessomething, which takes as input arguments a (single trial) data matrix as a first input argument, with the channels defined in the rows, + an optional additional opts structure, with fields specifying additional options, then the lower level preproc function in fieldtrip/private (which takes care of the specified ‘ft_preprocessing cfg-options’, will execute the function for you in a for-loop across the elements of your data.trial cell-array.


The above recipe is an undocumented option, which may come across as a bit of an over-engineered functionality. Specifically, you would achieve the very same if you were to write a matlab script as follows:

for i = 1:numel(data.trial)
  datapreprocessed.trial{i} = fancyfunctionthatdoessomething(data.trial{i}, opts);
end

What I can see as an advantage for using the custom function handle, and going through ft_preprocessing, is that there will be a trace of the exact processing steps in the output data object (in dataout.cfg.previous). Always good for reproducibility etc.

In your specific use case, you would need to write a wrapper function around nt_detrend that estimates the polynomial coefficients on the masked that, and subsequently applies the modelled polynome to the unmasked data.

Best wishes and happy computing,

Jan-Mathijs




On 22 Nov 2025, at 14:44, Helin Erden via fieldtrip <fieldtrip at science.ru.nl> wrote:

Dear community,

I am doing an EEG-MVPA study, using Fieldtrip for preprocessing. To target slow drifts, instead of using high-pass filtering + baseline correction, I was wondering if I could implement “trial-masked” robust detrending in Fieldtrip as in this paper:

van Driel, J., Olivers, C. N. L., & Fahrenfort, J. J. (2021). High-pass filtering artifacts in multivariate classification of neural time series data. Journal of Neuroscience Methods, 352, 109080. https://doi.org/10.1016/j.jneumeth.2021.109080<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.sciencedirect.com%2Fscience%2Farticle%2Fpii%2FS0165027021000157&data=05%7C02%7Cfieldtrip%40science.ru.nl%7C92621d5644d24bdaeb4708de2c0744ef%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638996606892816562%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=fRO5kQmyTflo7dMrj7wNWMQ8vKawuBrYe2HjAPUbIwM%3D&reserved=0>


I believe nt_detrend from Noisetools can be used with Fieldtrip structures, but I am especially wondering if combining it with trial-masking is possible in Fieldtrip, and which functions would help. Thank you in advance for any help or advice.

Best,
Helin
_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip
https://doi.org/10.1371/journal.pcbi.1002202

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20251125/c5a0c5d3/attachment.htm>


More information about the fieldtrip mailing list