raw.resample induces noise

MNE version: 1.11.0
Operating system: e.g. Windows 11

This is my code:

for rawfile in MEG:
    try:
    r = rawfile.split(“\”)[-1]  
    r = r.split(“.”)[0]
    run = r.split(““)[1]
    subj = r.split(””)[0]
    cond = r.split(“_”)[2]
    
    
    # Load the raw data
    raw = mne.io.read_raw_fif(rawfile, preload=True, verbose=False)
    # Initial PSD plot (using new method)
    psd = raw.compute_psd(fmax=60)
    psd.plot(show=False)
    plt.title(f'Original PSD - {subj}_{run}_{cond}')
   
    
    
    # Apply band-pass filter (high-pass only)
    
    raw.filter(l_freq= None , h_freq=raw.info['sfreq']/2.5,  verbose=False)
    psd = raw.compute_psd(fmax=60)
    psd.plot(show=False)
    plt.title(f'filtered PSD - {subj}_{run}_{cond}')
    
    # Downsample to 500 Hz
    
    raw.resample(sfreq=500)
     
    psd = raw.compute_psd(fmax=60)
    psd.plot(show=False)
    plt.title(f'downsampl PSD - {subj}_{run}_{cond}')
    
    
except Exception as e:
    print(f"✗ Error processing {rawfile}: {str(e)}")

No matter if I filter first or not, the resample function increases noise a lot. I tried different parameters (mainly for method). Is there anything I’m missing?

Original raw:

Resampled raw:

This is expected, because resampling reduces the number of available data points, which leads to a smaller number of segments that are averaged if you do not adapt the parameters (which is the case when using the defaults). Therefore, the variance/noise increases.

If you want to get PSDs with similar variances, you need to specify the parameters in terms of time (e.g., n_per_seg=4 * raw.info["sfreq"]) and not samples (e.g., n_per_seg=1024).

Here’s a reproducible example using method="multitaper":

import matplotlib.pyplot as plt

from mne.datasets import sample
from mne.io import read_raw

raw = read_raw(sample.data_path() / "MEG" / "sample" / "sample_audvis_raw.fif")
raw.pick(picks="meg", exclude="bads").crop(0, 60).load_data()
psd = raw.compute_psd(fmax=60, method="multitaper", bandwidth=2)
psd.plot(exclude="bads", show=False)

raw_rs = raw.copy().resample(200)
psd_rs = raw_rs.compute_psd(fmax=60, method="multitaper", bandwidth=2)
psd_rs.plot(exclude="bads", show=False)

plt.show()

If you do not set bandwidth=2, the resampled PSD will be noisier than the original one. However, if you set bandwidth=2, they look pretty much identical.

1 Like