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

.. only:: html

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

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

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

.. _sphx_glr_examples_data_collection_emg_export1.py:


Delsys EMG Export
=================

This example shows how to process and visualize the EMG data.

.. GENERATED FROM PYTHON SOURCE LINES 7-66

.. code-block:: default


    import os

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.backends.backend_pdf import PdfPages
    import math

    import rofunc as rf

    a = 20
    b = 0.05


    def save_multi_image(filename):
        pp = PdfPages(filename)
        fig_nums = plt.get_fignums()
        figs = [plt.figure(n) for n in fig_nums]
        for fig in figs:
            fig.savefig(pp, format='pdf')
        pp.close()


    def impedance_profile(data):
        impedance_1 = np.zeros(len(data))
        impedance_2 = np.zeros(len(data))
        for i in range(len(data)):
            impedance_1[i] = a * (1 - math.exp(- b * (data[i, 0] + data[i, 1] + data[i, 2]))) / (
                        1 + math.exp(- b * (data[i, 0] + data[i, 1] + data[i, 2])))
        for i in range(len(data)):
            impedance_2[i] = 1.5 * a * (1 - math.exp(- b * (data[i, 3] + data[i, 3]))) / (
                    1 + math.exp(- b * (data[i, 3] + data[i, 3])))
        return impedance_1, impedance_2


    emg = np.load('/home/ubuntu/Data/emg_record/20230306/20230306_093814.npy')
    emg = emg[:, 1:]
    # emg = emg[:, :]
    SAMPING_RATE = 2000
    k = 4
    n = 6
    data_filter, data_clean, data_mvc, data_activity, data_abs, data_mvcscale = rf.emg.process_all_channels(emg, n,
                                                                                                            SAMPING_RATE, k)
    impedance_1, impedance_2 = impedance_profile(data_mvcscale)
    # impedance_1 = np.where(impedance_1 > 0.15, 1, 0)
    # impedance_2 = np.where(impedance_2 > 0.15, 1, 0)
    # plt.plot(impedance_1)
    # plt.plot(impedance_2)
    # plt.show()

    for i in range(n):
        rf.emg.plot_raw_and_clean(data_filter[:, i], data_clean[:, i], k)
        rf.emg.plot_abs_and_mvc(data_abs[:, i], data_mvc[:, i], k)
        # rf.emg.plot_mvcscale_and_activity(data_mvcscale[:, i], data_activity[:, i], k)

    # filename = os.path.join('/home/ubuntu/Data/emg_record', '20221221_094445.pdf')
    # save_multi_image(filename)

    plt.show()


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

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


.. _sphx_glr_download_examples_data_collection_emg_export1.py:

.. only:: html

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




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

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

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

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


.. only:: html

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

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