.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_simulate_surface_muaps.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_03_simulate_surface_muaps.py: Surface Motor Unit Action Potentials ==================================== After having created the **muscle model**, we can simulate the **surface EMG** by creating a **surface EMG model**. First step is to create **MUAPs** from the **muscle model**. .. note:: The **MUAPs** are the **action potentials** of the **motor units** at the surface of the skin. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python from pathlib import Path import joblib import matplotlib.pyplot as plt import numpy as np from myogen import simulator from myogen.utils.plotting import plot_muap_grid .. GENERATED FROM PYTHON SOURCE LINES 23-36 Define Parameters ----------------- The **surface EMG** is created using the **SurfaceEMG** object. The **SurfaceEMG** object takes the following parameters: - ``muscle_model``: Muscle model - ``sampling_frequency``: Sampling frequency - ``electrode_grid_dimensions``: Electrode grid dimensions - ``inter_electrode_distance``: Inter-electrode distance - ``fat_thickness``: Fat thickness - ``skin_thickness``: Skin thickness .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: Python # Define simulation parameters sampling_frequency = 2048.0 # Hz - standard for surface EMG electrode_grid_size = (5, 5) inter_electrode_distance = 4.0 # mm - standard spacing # Define volume conductor parameters fat_thickness = 1.0 # mm skin_thickness = 1.0 # mm .. GENERATED FROM PYTHON SOURCE LINES 47-51 Load Muscle Model ---------------------------- Load muscle model from previous example .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: Python save_path = Path("./results") muscle = joblib.load(save_path / "muscle_model.pkl") .. GENERATED FROM PYTHON SOURCE LINES 56-67 Create Surface EMG Model ------------------------- The **SurfaceEMG** object is initialized with the **muscle model** and the **simulation parameters**. .. note:: For simplicity, we only simulate the first motor unit. This can be changed by modifying the ``MUs_to_simulate`` parameter. This is to simulate the **surface EMG** from two different directions. .. GENERATED FROM PYTHON SOURCE LINES 67-79 .. code-block:: Python surface_emg = simulator.SurfaceEMG( muscle_model=muscle, sampling_frequency__Hz=sampling_frequency, electrode_grid_dimensions__rows_cols=electrode_grid_size, inter_electrode_distance__mm=inter_electrode_distance, MUs_to_simulate=[0], electrode_grid_center_positions=[(0, 0)], fat_thickness__mm=fat_thickness, skin_thickness__mm=skin_thickness, ) .. GENERATED FROM PYTHON SOURCE LINES 80-84 Simulate MUAPs -------------- To generate the **MUAPs**, we need to run the ``simulate_muaps`` method of the **SurfaceEMG** object. .. GENERATED FROM PYTHON SOURCE LINES 84-99 .. code-block:: Python # Run simulation with progress output muaps = surface_emg.simulate_muaps(show_plots=False, verbose=False) print(f"\nMUAP simulation completed!") print(f"Generated MUAPs shape: {muaps.shape}") print(f" - {muaps.shape[0]} electrode position(s)") print(f" - {muaps.shape[1]} motor units") print(f" - {muaps.shape[2]}×{muaps.shape[3]} electrode grid") print(f" - {muaps.shape[4]} time samples") # Save results joblib.dump(surface_emg, save_path / "surface_emg.pkl") .. rst-class:: sphx-glr-script-out .. code-block:: none MU 1 Position 1: 0%| | 0/5 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 03_simulate_surface_muaps.py <03_simulate_surface_muaps.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 03_simulate_surface_muaps.zip <03_simulate_surface_muaps.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_