当前位置:   article > 正文

肌电信号时域特征和频域特征提取,特征带有标签_时域特征ssc公式

时域特征ssc公式
  1. import numpy as np
  2. from feature_utils import *
  3. import math
  4. from keras.utils import to_categorical
  5. arr = normalized_filtered_emg
  6. new_shape = (-1, 12)
  7. emg = np.reshape(arr, new_shape)
  8. featureData = []
  9. featureLabel = []
  10. classes = 49
  11. timeWindow = 200
  12. strideWindow = 100
  13. for i in range(classes):
  14. index = []
  15. for j in range(label.shape[0]):
  16. if np.all(label[j,:]==i):
  17. index.append(j)
  18. iemg = emg[index,:]
  19. length = math.floor((iemg.shape[0] - timeWindow) / strideWindow)
  20. print("class ", i, ", number of sample: ", iemg.shape[0], length)
  21. for j in range(length):
  22. start_index = strideWindow * j
  23. end_index = start_index + timeWindow
  24. if end_index <= iemg.shape[0]:
  25. rms = featureRMS(iemg[start_index:end_index,:])
  26. mav = featureMAV(iemg[start_index:end_index,:])
  27. wl = featureWL(iemg[start_index:end_index,:])
  28. zc = featureZC(iemg[start_index:end_index,:])
  29. ssc = featureSSC(iemg[start_index:end_index,:])
  30. featureStack = np.hstack((rms, mav, wl, zc, ssc))
  31. featureData.append(featureStack)
  32. featureLabel.append(i)
  33. featureData = np.array(featureData)
  34. featureLabel = np.array(featureLabel)
  35. print(featureData.shape)
  36. print(featureLabel.shape)

对特征的定义可以在GITHUB中找到

  1. def spectrum(signal, fs):
  2. m = len(signal)
  3. n = next_power_of_2(m)
  4. y = np.fft.fft(signal, n)
  5. yh = y[0:int(n / 2 )]
  6. fh = (fs / n) * np.arange(0, n / 2, 1)#
  7. power = np.real(yh * np.conj(yh) / n)#
  8. #print(fh.shape)
  9. #print(power.shape)
  10. return fh, power
  11. def next_power_of_2(x):
  12. return 1 if x == 0 else 2 ** (x - 1).bit_length()
  13. def frequency_ratio(frequency, power):
  14. power_low = power[(frequency >= 30) & (frequency <= 250)]
  15. power_high = power[(frequency > 250) & (frequency <= 500)]
  16. ULC = np.sum(power_low)
  17. UHC = np.sum(power_high)
  18. return ULC / UHC
  19. def mean_freq(frequency, power):#频率和功率
  20. num = 0
  21. den = 0
  22. for i in range(int(len(power) / 2)):
  23. num += frequency[i] * power[i]
  24. den += power[i]
  25. return num / den
  26. def median_freq(frequency, power):
  27. power_total = np.sum(power) / 2
  28. temp = 0
  29. tol = 0.01
  30. errel = 1
  31. i = 0
  32. while abs(errel.all()) > tol:
  33. temp += power[i]
  34. errel = (power_total - temp) / power_total
  35. i += 1
  36. if errel.all() < 0:
  37. errel = 0
  38. i -= 1
  39. return frequency[i]
  40. for i in range(classes):
  41. index = []
  42. for j in range(label.shape[0]):
  43. if np.all(label[j, :] == i):
  44. index.append(j)
  45. iemg = emg[index, :]
  46. length = math.floor((iemg.shape[0] - timeWindow) / strideWindow)
  47. print("class ", i, ", number of sample: ", iemg.shape[0], length)
  48. sampling_frequency = 2e3
  49. classes = 49
  50. timeWindow = 200
  51. strideWindow = 200
  52. emg=normalized_filtered_emg
  53. featureData = []
  54. featureLabel = []
  55. for j in range(length):
  56. start_index = strideWindow * j
  57. end_index = start_index + timeWindow
  58. if end_index <= iemg.shape[0]:
  59. # 提取频率特征
  60. fh, power = spectrum(iemg[start_index:end_index, :], fs) # 使用您的频率特征提取函数
  61. fr = (frequency_ratio(fh, power)) # 使用您的频率特征提取函数
  62. mnp = (np.sum(power) / len(power)) # 使用您的频率特征提取函数
  63. tot = (np.sum(power)) # 使用您的频率特征提取函数
  64. mnf = (mean_freq(fh, power)) # 使用您的频率特征提取函数
  65. pkf = (fh[np.argmax(power)]) # 使用您的频率特征提取函数
  66. featureStack = np.hstack((fr, mnp, tot, mnf, pkf))
  67. featureData.append(featureStack)
  68. featureLabel.append(i)
  69. featureData = np.array(featureData)
  70. featureLabel = np.array(featureLabel)
  71. print(featureData.shape)
  72. print(featureLabel.shape)

频域特征提取,采用与时域相同代码可以保证特征提取时,有对应标签。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/326740?site
推荐阅读
相关标签
  

闽ICP备14008679号