.. _artifacts: Dealing with Artifacts ====================== Here is the plot of EEG at the end of :ref:`getting_started`. .. image:: _static/eegplot2.png :width: 6in :align: center Looks like we have an eye-blink artifact near sample 100. Bandpass Filtering ------------------ One way to decrease the effect of eye blinks is to filter the data. Here is a link to the file :download:`bandpass.py `, adapted from `ObsPy `_, that defines the function ``bandpass``. We can use it to pass frequencies from 1 to 30 Hz as follows: .. ipython:: :suppress: In [0]: import os In [3]: if not os.path.isfile('s20-gammasys-gifford-unimpaired.json'): ...: !wget http://www.cs.colostate.edu/eeg/data/json/zips/s20-gammasys-gifford-unimpaired.json.zip ...: !unzip s20-gamma*zip ...: .. ipython:: In [1]: import json In [3]: import numpy as np In [4]: import matplotlib.pyplot as plt In [207]: import bandpass as bp In [2]: data = json.load(open('s20-gammasys-gifford-unimpaired.json','r')) In [5]: first = data[0] In [5]: eeg = np.array(first['eeg']['trial 1']) In [212]: filtered = bp.bandpass(eeg[:8,:], 2,30, 256, corners=5, zerophase=True, axis=1) In [213]: plt.figure(3); In [214]: plt.plot(filtered[:8,4000:4512].T + 80*np.arange(7,-1,-1)); In [91]: plt.plot(np.zeros((512,8)) + 80*np.arange(7,-1,-1),'--',color='gray'); In [91]: plt.yticks([]); In [215]: plt.axis('tight'); @savefig artifactplot1.png width=6in align=center In [216]: plt.legend(first['channels']); There still seems to be a little eye blink present. Unfortunately, bandpass filtering filters each channel independently, so we cannot use information about the relative amplitudes among the channels. Eye blinks tend to affect channels near the front the scalp more. Methods that extract this pattern would do a better job of removing eye blinks, such as `Signal Fraction Analysis (SFA) `_.