{ "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
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.
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.