{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n\nFiltering and resampling data\n=============================\n\nSome artifacts are restricted to certain frequencies and can therefore\nbe fixed by filtering. An artifact that typically affects only some\nfrequencies is due to the power line.\n\nPower-line noise is a noise created by the electrical network.\nIt is composed of sharp peaks at 50Hz (or 60Hz depending on your\ngeographical location). Some peaks may also be present at the harmonic\nfrequencies, i.e. the integer multiples of\nthe power-line frequency, e.g. 100Hz, 150Hz, ... (or 120Hz, 180Hz, ...).\n\nThis tutorial covers some basics of how to filter data in MNE-Python.\nFor more in-depth information about filter design in general and in\nMNE-Python in particular, check out `tut_background_filtering`.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "import numpy as np\nimport mne\nfrom mne.datasets import sample\n\ndata_path = sample.data_path()\nraw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'\nproj_fname = data_path + '/MEG/sample/sample_audvis_eog_proj.fif'\n\ntmin, tmax = 0, 20 # use the first 20s of data\n\n# Setup for reading the raw data (save memory by cropping the raw data\n# before loading it)\nraw = mne.io.read_raw_fif(raw_fname)\nraw.crop(tmin, tmax).load_data()\nraw.info['bads'] = ['MEG 2443', 'EEG 053'] # bads + 2 more\n\nfmin, fmax = 2, 300 # look at frequencies between 2 and 300Hz\nn_fft = 2048 # the FFT size (n_fft). Ideally a power of 2\n\n# Pick a subset of channels (here for speed reason)\nselection = mne.read_selection('Left-temporal')\npicks = mne.pick_types(raw.info, meg='mag', eeg=False, eog=False,\n stim=False, exclude='bads', selection=selection)\n\n# Let's first check out all channel types\nraw.plot_psd(area_mode='range', tmax=10.0, picks=picks, average=False)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Removing power-line noise with notch filtering\n----------------------------------------------\n\nRemoving power-line noise can be done with a Notch filter, directly on the\nRaw object, specifying an array of frequency to be cut off:\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw.notch_filter(np.arange(60, 241, 60), picks=picks, filter_length='auto',\n phase='zero')\nraw.plot_psd(area_mode='range', tmax=10.0, picks=picks, average=False)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Removing power-line noise with low-pass filtering\n-------------------------------------------------\n\nIf you're only interested in low frequencies, below the peaks of power-line\nnoise you can simply low pass filter the data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# low pass filtering below 50 Hz\nraw.filter(None, 50., h_trans_bandwidth='auto', filter_length='auto',\n phase='zero')\nraw.plot_psd(area_mode='range', tmax=10.0, picks=picks, average=False)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "High-pass filtering to remove slow drifts\n-----------------------------------------\n\nTo remove slow drifts, you can high pass.\n\n

Warning

There can be issues using high-passes greater than 0.1 Hz\n (see examples in `tut_filtering_hp_problems`),\n so apply high-pass filters with caution.

\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw.filter(1., None, l_trans_bandwidth='auto', filter_length='auto',\n phase='zero')\nraw.plot_psd(area_mode='range', tmax=10.0, picks=picks, average=False)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "To do the low-pass and high-pass filtering in one step you can do\na so-called *band-pass* filter by running the following:\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# band-pass filtering in the range 1 Hz - 50 Hz\nraw.filter(1, 50., l_trans_bandwidth='auto', h_trans_bandwidth='auto',\n filter_length='auto', phase='zero')" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Downsampling and decimation\n---------------------------\n\nWhen performing experiments where timing is critical, a signal with a high\nsampling rate is desired. However, having a signal with a much higher\nsampling rate than necessary needlessly consumes memory and slows down\ncomputations operating on the data. To avoid that, you can downsample\nyour time series. Since downsampling raw data reduces the timing precision\nof events, it is recommended only for use in procedures that do not require\noptimal precision, e.g. computing EOG or ECG projectors on long recordings.\n\n

Note

A *downsampling* operation performs a low-pass (to prevent\n aliasing) followed by *decimation*, which selects every\n $N^{th}$ sample from the signal. See\n :func:`scipy.signal.resample` and\n :func:`scipy.signal.resample_poly` for examples.

\n\nData resampling can be done with *resample* methods.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "raw.resample(100, npad=\"auto\") # set sampling frequency to 100Hz\nraw.plot_psd(area_mode='range', tmax=10.0, picks=picks)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "To avoid this reduction in precision, the suggested pipeline for\nprocessing final data to be analyzed is:\n\n 1. low-pass the data with :meth:`mne.io.Raw.filter`.\n 2. Extract epochs with :class:`mne.Epochs`.\n 3. Decimate the Epochs object using :meth:`mne.Epochs.decimate` or the\n ``decim`` argument to the :class:`mne.Epochs` object.\n\nWe also provide the convenience methods :meth:`mne.Epochs.resample` and\n:meth:`mne.Evoked.resample` to downsample or upsample data, but these are\nless optimal because they will introduce edge artifacts into every epoch,\nwhereas filtering the raw data will only introduce edge artifacts only at\nthe start and end of the recording.\n\n" ], "cell_type": "markdown", "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.13", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }