[FieldTrip] Trial-masked robust detrending

Schoffelen, J.M. (Jan Mathijs) janmathijs.schoffelen at donders.ru.nl
Thu Nov 27 08:02:25 CET 2025


Hi Helin,

As a follow up, it would be nice to hear back, has this now been solved for you?

In the meantime, the latest release version (as well as the master branch on the fieldtrip github repo) now has an updated version of preproc.m with some more elegant handling for the cfg.custom argument.
You can read in the code how it should be optimally used (but it now also includes an option to transpose the input matrix on-the-fly, which may be useful for external functions that have the convention to define the channels in the rows).

Please consider sharing back to the community by providing some feedback about this e-mail thread in general, the code specifically, and ideally in the form of some documentation that we can put online as a FAQ or a piece of example code (see https://www.fieldtriptoolbox.org/development/contribute/<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.fieldtriptoolbox.org%2Fdevelopment%2Fcontribute%2F&data=05%7C02%7Cfieldtrip%40science.ru.nl%7C7163e1848703428b4c9e08de2d82ebbf%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638998237468690251%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=Gm%2F2YDWhMIiobcH2iBBLe3dxHPuAWOF1qFsR3FsZfOs%3D&reserved=0> on ways to contribute). We are all in this together.

Best wishes,
Jan-Mathijs


On 25 Nov 2025, at 10:44, Schoffelen, J.M. (Jan Mathijs) via fieldtrip <fieldtrip at science.ru.nl> wrote:

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%7C7163e1848703428b4c9e08de2d82ebbf%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638998237468709799%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=RLtbu2asaUVZoRalMdkujFxGD7liG0nsKMtJSG7AH%2FU%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

_______________________________________________
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/20251127/a20c690f/attachment.htm>


More information about the fieldtrip mailing list