{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Generating pulse trains\n\nThis example shows how to use :py:class:`~pulse2percept.stimuli.PulseTrain`\nand its variants.\n\n## Biphasic pulse trains\n\nA series of biphasic pulses can be created with the\n:py:class:`~pulse2percept.stimuli.BiphasicPulseTrain` class.\n\nYou have the same options as when setting up a single\n:py:class:`~pulse2percept.stimuli.BiphasicPulse`, in addition to specifying\na pulse train frequency (``freq``) and total stimulus duration (``stim_dur``).\n\nFor example, a 20 Hz pulse train lasting 200 ms and made from anodic-first\nbiphasic pulses (30 uA, 2 ms pulse duration, no interphase gap) can be\ncreated as follows:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pulse2percept.stimuli import BiphasicPulseTrain\n\npt = BiphasicPulseTrain(20, 30, 2, stim_dur=200, cathodic_first=False)\npt.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can also limit the number of pulses in the train, but still make the\nstimulus last 200 ms:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pt = BiphasicPulseTrain(20, 30, 2, n_pulses=3, stim_dur=200,\n                        cathodic_first=False)\npt.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Asymmetric biphasic pulse trains\n\nTo create a 20 Hz pulse train lasting 200 ms created from asymmetric biphasic\npulses, use :py:class:`~pulse2percept.stimuli.AsymmetricBiphasicPulseTrain`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pulse2percept.stimuli import AsymmetricBiphasicPulseTrain\n\n# First pulse:\namp1 = 10\nphase_dur1 = 2\n\n# Second pulse\namp2 = 2\nphase_dur2 = 10\n\npt = AsymmetricBiphasicPulseTrain(20, amp1, amp2, phase_dur1, phase_dur2,\n                                  stim_dur=200)\npt.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Biphasic triplet trains\n\nTo create a train of pulse triplets, use\n:py:class:`~pulse2percept.stimuli.BiphasicTripletTrain`:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pulse2percept.stimuli import BiphasicTripletTrain\n\namp = 15\nphase_dur = 2\n\npt = BiphasicTripletTrain(20, amp, phase_dur, stim_dur=200)\npt.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Generic pulse trains\n\nFinally, you can concatenate any :py:class:`~pulse2percept.stimuli.Stimulus`\nobject into a pulse train.\n\nFor example, let's define a single ramp stimulus:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom pulse2percept.stimuli import Stimulus, PulseTrain\n\n# Single ramp:\ndt = 1e-3\nramp = Stimulus([[0, 0, 1, 1, 2, 2, 0, 0]],\n                time=[0, 1, 1 + dt, 2, 2 + dt, 3, 3 + dt, 5 - dt])\nramp.plot()\n\n# Ramp train:\nPulseTrain(20, ramp, stim_dur=200).plot()\n\n# Biphasic ramp:\nbiphasic_ramp = Stimulus(np.concatenate((ramp.data, -ramp.data), axis=1),\n                         time=np.concatenate((ramp.time, ramp.time + 5)))\nbiphasic_ramp.plot()\n\n# Biphasic ramp train:\nPulseTrain(20, biphasic_ramp, stim_dur=200).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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}