Padding Short Epochs with mne.filter.resample Not Effective

I have time series data that I processed in Matlab/Brainstorm and source reconstructed and projected into ROIs. I am now importing this data into a simple structure in MNE v1.5 on MacOS. Since the trials are very short, I want to pad each single trial before computing PSDs and averaging them to get 1 PSD estimate with better resolution per channel.

Below a code example:

import numpy as np
import mne

sfreq = 500 # Sampling frequency
tmax = 0.5
times = np.arange(0, 0.5, 0.002) # 
 
sin = np.sin(times * 10) # Multiplied by 10 for shorter cycles
cos = np.cos(times * 10)
sinX2 = sin * 2 + 0.01 * np.random.normal(size=times.size)
cosX2 = cos * 2 + 0.01 * np.random.normal(size=times.size)

data = np.array([sin, cos, sinX2, cosX2])

# Definition of channel types and names.
ch_types = ["misc", "misc", "misc", "misc"]
ch_names = ["sin", "cos", "sinX2", "cosX2"]

# create raw object

info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(data, info)
 
# create epochs
raw_trials = [raw, raw]
datas = [r.get_data() for r in raw_trials]
info = raw_trials[0].info
epochs = mne.EpochsArray(datas, info)
spectrum_before = epochs.compute_psd(picks="misc", method="welch", n_overlap=125)
spectrum_before.plot(picks="misc");

So far, this works as expected. However, when I try to create padded epochs using mne.filter.resample, it doesn’t seem to have an effect:

before_padding = epochs.get_data()
display(before_padding.shape)

after = mne.filter.resample(before_padding, npad=1 * sfreq, pad="wrap", verbose=True)
display(after.shape)

# compute psd
after_epochs = mne.EpochsArray(after, info)
spectrum = after_epochs.compute_psd(picks="misc", method="welch", n_overlap=125)

The shape of the epochs doesn’t change after padding with mne.filter.resample.

Is mne.filter.resample not suited for achieving my goal of padding short epochs before averaging? Or am I using it incorrectly? How can I properly pad short epochs before averaging to get better resolution of PSD estimates?

No. The padding that happens in mne.filter.resample is temporary and goes away after the resampling operation.

Use np.pad(np.array(datas), pad_width=(before, after), mode="wrap") before you create your EpochsArray. If I’m interpreting your intent correctly, you want before = after = sfreq/2

3 Likes