
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/image_processing/plot_waveforms.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

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

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

.. _sphx_glr_auto_examples_image_processing_plot_waveforms.py:


=============================
Decode and plot Waveform Data
=============================

This example illustrates how to plot waveforms from a Waveform Sequence using
matplotlib.

.. GENERATED FROM PYTHON SOURCE LINES 10-53



.. image-sg:: /auto_examples/image_processing/images/sphx_glr_plot_waveforms_001.png
   :alt: RHYTHM: Lead I (Einthoven), MEDIAN BEAT: Lead I (Einthoven)
   :srcset: /auto_examples/image_processing/images/sphx_glr_plot_waveforms_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import numpy as np
    import matplotlib.pyplot as plt

    from pydicom import dcmread
    from pydicom.data import get_testdata_file
    from pydicom.waveforms import generate_multiplex

    fpath = get_testdata_file("waveform_ecg.dcm")
    ds = dcmread(fpath)

    # Plot the first channel of each multiplex
    ch_idx = 0
    # We could also use ds.waveform_array()
    fig, axes = plt.subplots(len(ds.WaveformSequence))
    generator = generate_multiplex(ds, as_raw=False)
    for ax, mplx, arr in zip(axes, ds.WaveformSequence, generator):
        nr_channels = mplx.NumberOfWaveformChannels
        nr_samples = mplx.NumberOfWaveformSamples
        sampling_fq = mplx.SamplingFrequency  # in Hz
        mplx_label = mplx.MultiplexGroupLabel

        ch_item = mplx.ChannelDefinitionSequence[ch_idx]

        x = np.arange(0, nr_samples / sampling_fq, 1 / sampling_fq)
        x_units = "seconds"

        # ChannelSensitivityUnitsSequence is type 1C, so check it's there
        if "ChannelSensitivityUnitsSequence" in ch_item:
            y_units = ch_item.ChannelSensitivityUnitsSequence[0].CodeMeaning
        else:
            y_units = "unitless"

        # Description of the channel source
        ch_source = ch_item.ChannelSourceSequence[0].CodeMeaning

        ax.plot(x, arr[..., ch_idx])
        ax.set_title(f"{mplx_label}: {ch_source}")
        ax.set_xlabel(f"({x_units})")
        ax.set_ylabel(f"({y_units})")

    fig.tight_layout(pad=1)
    plt.show()


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

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


.. _sphx_glr_download_auto_examples_image_processing_plot_waveforms.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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