user12397996
user12397996

Reputation:

Trimming and filtering signal using MNE - Python

I am working on an EEG Signal analysis problem with python. I need to remove the recordings below 1st minute and above 6th minute of the signal in edf format which is loaded using MNE, and pass it through a bandpass filter. I am not familiar with MNE so used scipy for trimming and filtering after converting it into raw NumPy array format. The code is given below. Since the sampling rate is 100 Hz, I assumed first minute will contain 6000 samples and next five minutes will contain 30000 more samples which is why I am only taking raw_data[i][6000:36000].

filtered_data[i] = butter_bandpass_filter(raw_data[i][6000:36000], lowcut, highcut, fs, order=5)

butter_bandpass_filter is defined as follows

def butter_bandpass(lowcut, highcut, fs, order=5):
   nyq = 0.5 * fs
   low = lowcut / nyq
   high = highcut / nyq
   b, a = butter(order, [low, high], btype='band',analog=True)
   return b, a

def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
   b, a = butter_bandpass(lowcut, highcut, fs, order=order)
   y = lfilter(b, a, data)
   return y

But I don't feel like this is the correct method. Is there a way to do the above mentioned task using MNE-Python instead of converting it to ndarray or using scipy?

Upvotes: 0

Views: 1626

Answers (1)

mmagnuski
mmagnuski

Reputation: 1275

To select only a portion of your signal you can do the following in mne-python:

raw.crop(tmin=60, tmax=360)

(the tmin and tmax arguments are in seconds)

Bear in mind that filtering will result in edge artifacts so it is a good idea to first crop your data so that you leave a wider signal segment that what you are interested in, so after filtering you can crop again, dropping the edge artifacts. Filtering in mne-python is also easy:

raw.filter(1, None)

this performs 1 Hz high-pass FIR filtering. To learn more about filtering in mne-python take a look at this tutorial and this detailed detailed tutorial/discussion:

Upvotes: 1

Related Questions