Exercise: Filtering EMG data

7.3.1.4. Exercise: Filtering EMG data#

Using this dataset with one surface EMG electrode on the biceps brachii:

import kineticstoolkit.lab as ktk
import numpy as np

filename = ktk.doc.download("emg_biceps_brachii.ktk.zip")
emg = ktk.load(filename)
emg.plot()
_images/f496964b5bc8b99452aa6acd83dc39012fb32d4debb912116aae15350cdd784f.png

Write code that generates new data named “Envelope” in the TimeSeries, which corresponds to the envelope of this signal. Follow these three steps:

  1. Filter the TimeSeries with a band-pass Butterworth filter from 20 Hz to 500 Hz to reject high-frequency noise and body movement artifacts.

Hide code cell content
filtered_emg = ktk.filters.butter(emg, btype="bandpass", fc=[20, 500])
filtered_emg.plot()
_images/2add7196f608eb4811f1640365a908c75bb1cf54df3e6d75acfedb7df4cab32f.png
  1. Rectify the filtered TimeSeries by replacing every data point with its absolute value. You may use np.abs.

Hide code cell content
filtered_emg.data["BicepsBrachiiR"] = np.abs(filtered_emg.data["BicepsBrachiiR"])
filtered_emg.plot()
_images/49b75a60dfd90e6ad3309cacb0f89366a1a4e1b1e5c185fd592b0a6bddd6c5f1.png
  1. Filter the rectified TimeSeries with a low-pass Butterworth filter with a cut-off frequency of 10 Hz.

Hide code cell content
filtered_emg = ktk.filters.butter(filtered_emg, fc=10)

# Add the filtered signal as Envelope in the original TimeSeries
# There are many ways to do this, here is a robust one:
filtered_emg = filtered_emg.rename_data("BicepsBrachiiR", "Envelope")
emg = emg.merge(filtered_emg)

# Plot the result
emg.plot()
_images/aa488c1de3beeb83153ba5f91cdc849d5210aeafebb1844f86e8ca6a626e221b.png