{
  "nbformat": 4,
  "nbformat_minor": 0,
  "cells": [
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n#  Phase basic functions\n\n\nThis example shows how to use the Phase functions\n\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "# import matplotlib\n# matplotlib.use('TkAgg')\nimport matplotlib.pyplot as plt\nfrom os.path import join, isdir\nimport seaborn as sns\nfrom phase_utils import *\nsns.set()\nsns.set_context('paper')"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Parameters\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "fs = 256\ntmax = 0.7\ndata_dirpath = join('pySAB', 'sample_data') if isdir('pySAB') else join('..', '..', 'pySAB', 'sample_data')"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Load the example EEG signal and visualize it\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "x = np.fromfile(join(data_dirpath, 'rec_042_chan_EEG_Cp1-Cp2_trial_1_2048Hz.dat'))\nf = plt.figure()\nax = f.add_subplot(111)\nplt.plot(np.linspace(0, tmax, len(x)), x)\nplt.autoscale(axis='x', tight=True)\nax.set(title='Raw signal', xlabel='Time (s)', ylabel='Amplitude')"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Filter the signal in the alpha band\n``bp_filter_1d()`` allows to visualize the bode diagram of the filter\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "x_alpha, _ = bp_filter_1d(x, fs, ftype='elliptic', wn=2 / fs * np.array([8, 12]), order=3, do_plot=1)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Plot the analytical signal\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "plot_analytical_signal(x_alpha, fs)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Compute the robust phase estimation\n\n"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "outputs": [],
      "source": [
        "compute_robust_estimation(x, fs, fmin=8, fmax=12, f_tolerance=0.1, noise_tolerance=1, n_monte_carlo=20, do_fplot=0,\n                          do_plot=1)"
      ],
      "execution_count": null,
      "metadata": {
        "collapsed": false
      }
    }
  ],
  "metadata": {
    "language_info": {
      "version": "3.5.2",
      "name": "python",
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      },
      "file_extension": ".py",
      "pygments_lexer": "ipython3"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3",
      "language": "python"
    }
  }
}