
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples\stimuli\plot_pulse_trains.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_stimuli_plot_pulse_trains.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_stimuli_plot_pulse_trains.py:


===============================================================================
Generating pulse trains
===============================================================================

This example shows how to use :py:class:`~pulse2percept.stimuli.PulseTrain`
and its variants.

Biphasic pulse trains
---------------------

A series of biphasic pulses can be created with the
:py:class:`~pulse2percept.stimuli.BiphasicPulseTrain` class.

You have the same options as when setting up a single
:py:class:`~pulse2percept.stimuli.BiphasicPulse`, in addition to specifying
a pulse train frequency (``freq``) and total stimulus duration (``stim_dur``).

For example, a 20 Hz pulse train lasting 200 ms and made from anodic-first
biphasic pulses (30 uA, 2 ms pulse duration, no interphase gap) can be
created as follows:

.. GENERATED FROM PYTHON SOURCE LINES 24-30

.. code-block:: Python


    from pulse2percept.stimuli import BiphasicPulseTrain

    pt = BiphasicPulseTrain(20, 30, 2, stim_dur=200, cathodic_first=False)
    pt.plot()




.. image-sg:: /examples/stimuli/images/sphx_glr_plot_pulse_trains_001.png
   :alt: plot pulse trains
   :srcset: /examples/stimuli/images/sphx_glr_plot_pulse_trains_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <Axes: ylabel='0'>



.. GENERATED FROM PYTHON SOURCE LINES 32-34

You can also limit the number of pulses in the train, but still make the
stimulus last 200 ms:

.. GENERATED FROM PYTHON SOURCE LINES 34-39

.. code-block:: Python


    pt = BiphasicPulseTrain(20, 30, 2, n_pulses=3, stim_dur=200,
                            cathodic_first=False)
    pt.plot()




.. image-sg:: /examples/stimuli/images/sphx_glr_plot_pulse_trains_002.png
   :alt: plot pulse trains
   :srcset: /examples/stimuli/images/sphx_glr_plot_pulse_trains_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <Axes: ylabel='0'>



.. GENERATED FROM PYTHON SOURCE LINES 40-45

Asymmetric biphasic pulse trains
--------------------------------

To create a 20 Hz pulse train lasting 200 ms created from asymmetric biphasic
pulses, use :py:class:`~pulse2percept.stimuli.AsymmetricBiphasicPulseTrain`:

.. GENERATED FROM PYTHON SOURCE LINES 45-60

.. code-block:: Python


    from pulse2percept.stimuli import AsymmetricBiphasicPulseTrain

    # First pulse:
    amp1 = 10
    phase_dur1 = 2

    # Second pulse
    amp2 = 2
    phase_dur2 = 10

    pt = AsymmetricBiphasicPulseTrain(20, amp1, amp2, phase_dur1, phase_dur2,
                                      stim_dur=200)
    pt.plot()




.. image-sg:: /examples/stimuli/images/sphx_glr_plot_pulse_trains_003.png
   :alt: plot pulse trains
   :srcset: /examples/stimuli/images/sphx_glr_plot_pulse_trains_003.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <Axes: ylabel='0'>



.. GENERATED FROM PYTHON SOURCE LINES 61-66

Biphasic triplet trains
-----------------------

To create a train of pulse triplets, use
:py:class:`~pulse2percept.stimuli.BiphasicTripletTrain`:

.. GENERATED FROM PYTHON SOURCE LINES 66-75

.. code-block:: Python


    from pulse2percept.stimuli import BiphasicTripletTrain

    amp = 15
    phase_dur = 2

    pt = BiphasicTripletTrain(20, amp, phase_dur, stim_dur=200)
    pt.plot()




.. image-sg:: /examples/stimuli/images/sphx_glr_plot_pulse_trains_004.png
   :alt: plot pulse trains
   :srcset: /examples/stimuli/images/sphx_glr_plot_pulse_trains_004.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <Axes: ylabel='0'>



.. GENERATED FROM PYTHON SOURCE LINES 76-83

Generic pulse trains
--------------------

Finally, you can concatenate any :py:class:`~pulse2percept.stimuli.Stimulus`
object into a pulse train.

For example, let's define a single ramp stimulus:

.. GENERATED FROM PYTHON SOURCE LINES 83-103

.. code-block:: Python


    import numpy as np
    from pulse2percept.stimuli import Stimulus, PulseTrain

    # Single ramp:
    dt = 1e-3
    ramp = Stimulus([[0, 0, 1, 1, 2, 2, 0, 0]],
                    time=[0, 1, 1 + dt, 2, 2 + dt, 3, 3 + dt, 5 - dt])
    ramp.plot()

    # Ramp train:
    PulseTrain(20, ramp, stim_dur=200).plot()

    # Biphasic ramp:
    biphasic_ramp = Stimulus(np.concatenate((ramp.data, -ramp.data), axis=1),
                             time=np.concatenate((ramp.time, ramp.time + 5)))
    biphasic_ramp.plot()

    # Biphasic ramp train:
    PulseTrain(20, biphasic_ramp, stim_dur=200).plot()



.. image-sg:: /examples/stimuli/images/sphx_glr_plot_pulse_trains_005.png
   :alt: plot pulse trains
   :srcset: /examples/stimuli/images/sphx_glr_plot_pulse_trains_005.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <Axes: ylabel='0'>




.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.394 seconds)


.. _sphx_glr_download_examples_stimuli_plot_pulse_trains.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_pulse_trains.ipynb <plot_pulse_trains.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_pulse_trains.py <plot_pulse_trains.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_pulse_trains.zip <plot_pulse_trains.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
