{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Resampling data\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 sampling\nrate than is necessary needlessly consumes memory and slows down computations\noperating on the data.\n\nThis example downsamples from 600 Hz to 100 Hz. This achieves a 6-fold\nreduction in data size, at the cost of an equal loss of temporal resolution.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Authors: Marijn van Vliet \n#\n# License: BSD (3-clause)\n#\nfrom __future__ import print_function\n\nfrom matplotlib import pyplot as plt\n\nimport mne\nfrom mne.datasets import sample" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Setting up data paths and loading raw data (skip some data for speed)\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "data_path = sample.data_path()\nraw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'\nraw = mne.io.read_raw_fif(raw_fname).crop(120, 240).load_data()" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Since downsampling reduces the timing precision of events, we recommend\nfirst extracting epochs and downsampling the Epochs object:\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "events = mne.find_events(raw)\nepochs = mne.Epochs(raw, events, event_id=2, tmin=-0.1, tmax=0.8, preload=True)\n\n# Downsample to 100 Hz\nprint('Original sampling rate:', epochs.info['sfreq'], 'Hz')\nepochs_resampled = epochs.copy().resample(100, npad='auto')\nprint('New sampling rate:', epochs_resampled.info['sfreq'], 'Hz')\n\n# Plot a piece of data to see the effects of downsampling\nplt.figure(figsize=(7, 3))\n\nn_samples_to_plot = int(0.5 * epochs.info['sfreq']) # plot 0.5 seconds of data\nplt.plot(epochs.times[:n_samples_to_plot],\n epochs.get_data()[0, 0, :n_samples_to_plot], color='black')\n\nn_samples_to_plot = int(0.5 * epochs_resampled.info['sfreq'])\nplt.plot(epochs_resampled.times[:n_samples_to_plot],\n epochs_resampled.get_data()[0, 0, :n_samples_to_plot],\n '-o', color='red')\n\nplt.xlabel('time (s)')\nplt.legend(['original', 'downsampled'], loc='best')\nplt.title('Effect of downsampling')\nmne.viz.tight_layout()" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "When resampling epochs is unwanted or impossible, for example when the data\ndoesn't fit into memory or your analysis pipeline doesn't involve epochs at\nall, the alternative approach is to resample the continuous data. This\ncan also be done on non-preloaded data.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Resample to 300 Hz\nraw_resampled = raw.copy().resample(300, npad='auto')" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Because resampling also affects the stim channels, some trigger onsets might\nbe lost in this case. While MNE attempts to downsample the stim channels in\nan intelligent manner to avoid this, the recommended approach is to find\nevents on the original data before downsampling.\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "print('Number of events before resampling:', len(mne.find_events(raw)))\n\n# Resample to 100 Hz (generates warning)\nraw_resampled = raw.copy().resample(100, npad='auto')\nprint('Number of events after resampling:',\n len(mne.find_events(raw_resampled)))\n\n# To avoid losing events, jointly resample the data and event matrix\nevents = mne.find_events(raw)\nraw_resampled, events_resampled = raw.copy().resample(\n 100, npad='auto', events=events)\nprint('Number of events after resampling:', len(events_resampled))" ], "outputs": [], "metadata": { "collapsed": false } } ], "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" } } } }