Certain artifacts are restricted to certain frequencies and can therefore be fixed by filtering. An artifact that typically affects only some frequencies is due to the power line.
Power-line noise is a noise created by the electrical network. It is composed of sharp peaks at 50Hz (or 60Hz depending on your geographical location). Some peaks may also be present at the harmonic frequencies, i.e. the integer multiples of the power-line frequency, e.g. 100Hz, 150Hz, ... (or 120Hz, 180Hz, ...).
import numpy as np
import mne
from mne.datasets import sample
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'
proj_fname = data_path + '/MEG/sample/sample_audvis_eog_proj.fif'
tmin, tmax = 0, 20 # use the first 20s of data
# Setup for reading the raw data (save memory by cropping the raw data
# before loading it)
raw = mne.io.read_raw_fif(raw_fname).crop(tmin, tmax).load_data()
raw.info['bads'] = ['MEG 2443', 'EEG 053'] # bads + 2 more
fmin, fmax = 2, 300 # look at frequencies between 2 and 300Hz
n_fft = 2048 # the FFT size (n_fft). Ideally a power of 2
# Pick a subset of channels (here for speed reason)
selection = mne.read_selection('Left-temporal')
picks = mne.pick_types(raw.info, meg='mag', eeg=False, eog=False,
stim=False, exclude='bads', selection=selection)
# Let's first check out all channel types
raw.plot_psd(area_mode='range', tmax=10.0, picks=picks)
Out:
Opening raw data file /home/ubuntu/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
Read a total of 3 projection items:
PCA-v1 (1 x 102) idle
PCA-v2 (1 x 102) idle
PCA-v3 (1 x 102) idle
Current compensation grade : 0
Range : 25800 ... 192599 = 42.956 ... 320.670 secs
Ready.
Adding average EEG reference projection.
1 projection items deactivated
The copy parameter is deprecated and will be removed in 0.14. In 0.13 the behavior will be to operate in-place (like copy=False). In 0.12 the default is copy=True. Use inst.copy() if necessary.
Reading 0 ... 12012 = 0.000 ... 20.000 secs...
Effective window size : 3.410 (s)
Removing power-line noise can be done with a Notch filter, directly on the Raw object, specifying an array of frequency to be cut off:
raw.notch_filter(np.arange(60, 241, 60), picks=picks)
raw.plot_psd(area_mode='range', tmax=10.0, picks=picks)
Out:
Effective window size : 3.410 (s)
If you’re only interested in low frequencies, below the peaks of power-line noise you can simply low pass filter the data.
raw.filter(None, 50.) # low pass filtering below 50 Hz
raw.plot_psd(area_mode='range', tmax=10.0, picks=picks)
Out:
Low-pass filtering at 50 Hz
Effective window size : 3.410 (s)
If you’re only interested in low frequencies, below the peaks of power-line noise you can simply high pass filter the data.
raw.filter(1., None) # low pass filtering above 1 Hz
raw.plot_psd(area_mode='range', tmax=10.0, picks=picks)
Out:
High-pass filtering at 1 Hz
Effective window size : 3.410 (s)
To do the low-pass and high-pass filtering in one step you can do a so-called band-pass filter by running
raw.filter(1., 50.) # band-pass filtering in the range 1 Hz - 50 Hz
Out:
Band-pass filtering from 1 - 50 Hz
When performing experiments where timing is critical, a signal with a high sampling rate is desired. However, having a signal with a much higher sampling rate than necessary needlessly consumes memory and slows down computations operating on the data. To avoid that, you can down-sample your time series.
Data resampling can be done with resample methods.
raw.resample(100, npad="auto") # set sampling frequency to 100Hz
raw.plot_psd(area_mode='range', tmax=10.0, picks=picks)
Out:
25 events found
Events id: [ 1 2 3 4 5 32]
25 events found
Events id: [ 1 2 3 4 5 32]
Effective window size : 10.010 (s)
Since down-sampling reduces the timing precision of events, you might want to
first extract epochs and down-sampling the Epochs object. You can do this
using the mne.Epochs.resample()
method.
Total running time of the script: (0 minutes 5.208 seconds)
plot_artifacts_correction_filtering.py
plot_artifacts_correction_filtering.ipynb