Creating a bandstop filter with mne and applying it to the data?

Hello all,

I am looking for some advice on applying an mne created filter to a data segment.

After applying an mne created filter to a data segment the resulting output does not have the same length as the initial data (please see example from mne documentation below).
Question: What is the best way to match the length of the convolution output to the length of the initial data?

I am following the page Background information on filtering

My goal is to create a bandstop filter that follows the MNE-Python 0.13 default (‘which is a long-duration, steep cutoff FIR that gets applied twice’). To do so I am adapting the mne page example:

transition_band = 0.5  # Hz
f_s = f_p + transition_band
filter_dur = 10.  # sec
freq = [0., f_p, f_s, sfreq / 2.]
gain = [1., 1., 0., 0.]
# This would be equivalent
# n = int(sfreq * filter_dur)
# h = signal.firwin2(n, freq, gain, nyq=sfreq / 2.)
h = mne.filter.create_filter(x, sfreq, l_freq=None, h_freq=f_p,
                             h_trans_bandwidth=transition_band,
                             filter_length='%ss' % filter_dur,
                             fir_design='firwin2', verbose=True)
x_v13 = np.convolve(np.convolve(h, x)[::-1], h)[::-1][len(h) - 1:-len(h) - 1]
# the effective h is one that is applied to the time-reversed version of itself
h_eff = np.convolve(h, h[::-1])
plot_filter(h_eff, sfreq, freq, gain, 'MNE-Python 0.13 default', flim=flim,
            compensate=True)

The original data segment length x is 10001, but the filtered data length x_v13 is 9999.

len(x_v13)
Out[111]: 9999

len(x)
Out[112]: 10001

After filtering the raw data, I want to segment it into epochs using the trigger information from the STIM channel. Therefore, it is necessary that the filtered data matches the original time samples (i.e. length) so that the information in the STIM channel is properly aligned to the filtered data.
What is the best way to deal with this?

Many thanks in advance for your time and input to this question!

have you considered just replicating the old behavior by passing the old values
to raw.filter and then doing the Epochs etc…?

Alex

1 Like

Yes. That works! Thanks!