Python: Analysing EMG signals – Part 2
The code to generate Figure 1 and the explanations are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import numpy as np import matplotlib.pyplot as plt %matplotlib inline # simulate EMG signal burst1 = np.random.uniform(-1, 1, size=1000) + 0.08 burst2 = np.random.uniform(-1, 1, size=1000) + 0.08 quiet = np.random.uniform(-0.05, 0.05, size=500) + 0.08 emg = np.concatenate([quiet, burst1, quiet, burst2, quiet]) time = np.array([i/1000 for i in range(0, len(emg), 1)]) # sampling rate 1000 Hz # plot EMG signal fig = plt.figure() plt.plot(time, emg) plt.xlabel('Time (sec)') plt.ylabel('EMG (a.u.)') fig_name = 'fig2.png' fig.set_size_inches(w=11,h=7) fig.savefig(fig_name)
Lines 1-3. Import libraries. Use
%matplotlib inline if working in an IPython or Jupyter notebook, or in a qtconsole (QuickTime toolkit console).
Lines 6-7. Get 1000 random samples from a uniform distribution of values between -1 and 1. (In a uniform probability distribution, any value within the interval specified has equal probability of being drawn.) To each of these values, add an offset value of 0.08. Perform these procedures to generate EMG data of muscle contractions
Line 8. To generate EMG data for resting muscle in
quiet, get 500 random samples from a uniform distribution of values between -0.05 and 0.05. To each value, add an offset of 0.08.
Line 9. Join (concatenate) the simulated EMG data for
burst periods, and assign these values to the variable
Line 10. Create a
time variable to indicate that EMG data were sampled at 1000 Hz (ie. 1000 times per second): using a for loop within a list comprehension, generate a list of integer values ranging from 0 to the length of the variable
emg, increasing by steps of 1. Take each value in the list and divide it by 1000. Then convert the list of values into a numpy array.
Lines 13-19. Create a figure object called
fig so we can refer to the whole figure later. Plot
emg data, label the x and y axes. Name the figure, set its size and save it.
List comprehensions. Python has a neat syntax functionality called list comprehensions to create a list in one line of code. To write a list comprehension, write an expression and a for loop within square brackets
[ ]. In the example above, the expression is
i/1000. The list comprehension here is equivalent to:
1 2 3 4 5 time =  for i in range(0, len(emg), 1): i = i/1000 time.append(i) np.array(time)
What are some of the problems with our simulated EMG signal?
- Baseline EMG values have an offset from zero. This could be due to experimental limitations. An offset in the baseline means that EMG values during contraction also have an offset.
- Baseline EMG values are noisy. That is, even when muscles were relaxed, we could not experimentally prevent nonsense signals from being recorded.
- The EMG potential difference swings from negative to positive values. This means if we wanted to calculate an average or mean EMG at present, the negative and positive values will cancel out!
In the next lesson we will discuss how some of these problems can be solved using techniques to process EMG data.
Konrad P (2006) The ABC of EMG – A practical introduction to kinesiological electromyography. Noraxon USA Inc.