{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Repeating pattern sequence\n\n\nRun the cochlea on a sequence composed of 1 repeating pattern\nThis pattern of 50ms appears 10 times and each repetition is separated by a noise segment (i.e. a non-repeating pattern)\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n# import matplotlib\n# matplotlib.use('TkAgg')\nimport numpy as np\nfrom scipy.io import wavfile\nimport seaborn as sns\nfrom simplecochlea import Cochlea\nimport simplecochlea\nsns.set_context('paper')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load the file\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "root_dirpath = os.path.dirname(simplecochlea.__file__)\nsample_data_dir = os.path.join(root_dirpath, 'sample_data')\nfs, sequence = wavfile.read(os.path.join(sample_data_dir, 'sample_sequence_10_50ms_1.wav'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create the cochlea\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "fmin, fmax, freq_scale, n_channels = 200, 8000, 'erbscale', 100\ncomp_factor, comp_gain = 0.3, 1.5\ntau, v_thresh, v_spike = np.linspace(0.001, 0.0004, n_channels), np.linspace(0.3, 0.17, n_channels), 0.5\n\ncochlea = Cochlea(n_channels, fs, fmin, fmax, freq_scale, comp_factor=comp_factor, comp_gain=comp_gain,\n                       lif_tau=tau, lif_v_thresh=v_thresh, lif_v_spike=v_spike)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Run the sequence through the cochlea\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spikelist_seq, _ = cochlea.process_input(sequence)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot the spikelist\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spikelist_seq.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We know the repeating pattern is repeating every 50ms, the sequence starts with a noise segment and in total, there\nare 20 segments (10 time the pattern and 10 interleaved noise segments).\nThus we can set the pattern_id of the spikes in the output spikelist, with the set_pattern_id_from_time_limits method.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "chunk_duration, n_chunks = 0.050, 20\nt_start = np.arange(0, chunk_duration*n_chunks, chunk_duration)\nt_end = t_start + chunk_duration\npattern_id = [1, 2] * 10\npattern_names = {1: 'Noise', 2: 'Pattern'}\n\nspikelist_seq.set_pattern_id_from_time_limits(t_start, t_end, pattern_id, pattern_names)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Replot the spikelist to see the results :\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "spikelist_seq.plot()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}