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/98e02e983fd530b2777be1909802bd9b5cfd086c5c1483129362fca9c7307aea.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/4da27e1a622f8d491b33a4e1a8825ee0e371ea09188a7620f52aa6eb801759fe.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/4c1fecdec10132388205bc27281350fdcefff1fc763f0b26e38ec1155915e71c.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/e584757768bf29e16a3eeb4109b9653e9bcf69ee7277c287805a3680e4a7dbee.png