replacing noisy segments with intact segments in EEG


MNE version: 1.0.3
operating system: macOS 11.1

I have a question which is related to a question I posted last time.

I’d like to replace noisy segments due to TMS artifacts in my EEG data with intact segments which are just before the start of TMS artifacts.

Assuming the timing of TMS onset is defined in an array, “event_onset_samples”:

sample_dir = mne.datasets.sample.data_path()
sample_fname = sample_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
raw =
event_onset_samples = events[:, 0] - raw.first_samp

and the time window of the noisy data segment which should be replaced is defined as following:

noise_start = -0.005  # start of the noise before TMS pulse (sec)
noise_end = 0.015   # end of the noise after TMS pulse (sec)

These all noisy segments (in total 0.02 sec) at each TMS pulse should be replaced by the intact data segments (also 0.02 sec in total) immediately before the noisy segments.

I’m sorry if the example became specific, but I’d appreciate any advice how to do this with mne.
Also I’m happy to give more backgrounds or details if needed.
Thanks a lot!