-
Notifications
You must be signed in to change notification settings - Fork 4
/
ninaweb_sEMG_envelop.py
121 lines (91 loc) · 3.41 KB
/
ninaweb_sEMG_envelop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# coding: utf-8
from __future__ import division, print_function
import numpy as np
import os
import pdb
import matplotlib.pyplot as plt
import get_max_min
import utilities
import get_envelop
PLOT_ENVELOP = True
if __name__=='__main__':
nb_channels = 16
cut_len = 64
fs = 200 # 采样频率
ma_len = 10 # 求envelop里面mean average的长度,越长数据越平滑
data_dir = './raw_data'
save_dir = './processed_data/envelop'
mat_path = utilities.walk_through_dir(data_dir)
EMG = []
LABEL = []
for path in mat_path:
emg, label = utilities.read_mat(path) # emg, label均是二维矩阵
emg_raw = emg.copy()
for i in range(nb_channels):
emg[:, i] = get_envelop.envelop(emg[:, i], fs, ma_len)
if PLOT_ENVELOP:
plt.figure()
plt.plot(emg_raw[0:4000, 0])
plt.plot(emg[0:4000, 0])
plt.show()
emg_cut, label_cut = utilities.process_emg_according_to_label(emg, label, cut_len)
if 'E1' in path:
pass
elif 'E2' in path:
for i, _label in enumerate(label_cut):
if _label != 0:
label_cut[i] = label_cut[i] + 12
elif 'E3' in path:
for i, _label in enumerate(label_cut):
if _label != 0:
label_cut[i] = label_cut[i] + 12 + 17
EMG.append(emg_cut)
LABEL.append(label_cut)
EMG = np.concatenate(EMG)
LABEL = np.concatenate(LABEL)
# 减少rest即label=0的动作
def reduce_rest_movement(emg, label):
emg_rest = emg[label == 0]
label_rest = label[label == 0]
pick_random_amount = int(np.sum(label == 1))
pick_random_index = np.random.choice(len(label_rest), pick_random_amount)
emg_rest = emg_rest[pick_random_index]
label_rest = label_rest[pick_random_index]
emg = emg[label > 0]
label = label[label > 0]
emg = np.concatenate((emg, emg_rest))
label = np.concatenate((label, label_rest))
return emg, label
EMG, LABEL = reduce_rest_movement(EMG, LABEL)
# 将EMG Envelop变换到 [0, 1]之间
EMG_normalize = EMG / np.max(EMG)
EMG_normalize[EMG_normalize > 1] = 1
EMG_normalize[EMG_normalize < 0] = 0
# 保存在一个文件
np.save(os.path.join(save_dir, 'EMG.npy'), EMG_normalize)
np.save(os.path.join(save_dir, 'label.npy'), LABEL)
print('EMG shape: ', EMG.shape)
print('LABEL shape: ', LABEL.shape)
# 分为测试集和验证集
nb_samples = len(LABEL)
train_len = int(0.7 * nb_samples)
id_list = np.arange(nb_samples)
np.random.shuffle(id_list)
train_id_list = id_list[:train_len]
test_id_list = id_list[train_len:]
EMG_train = EMG[train_id_list]
LABEL_train = LABEL[train_id_list]
EMG_test = EMG[test_id_list]
LABEL_test = LABEL[test_id_list]
#对测试集和验证机归一化
max_value = np.max(EMG_train)
EMG_train = EMG_train / max_value
EMG_test = EMG_test / max_value
EMG_train[EMG_train > 1] = 1
EMG_train[EMG_train < 0] = 0
EMG_test[EMG_test > 1] = 1
EMG_test[EMG_test < 0] = 0
np.save(os.path.join(save_dir, 'EMG_train.npy'), EMG_train)
np.save(os.path.join(save_dir, 'label_train.npy'), LABEL_train)
np.save(os.path.join(save_dir, 'EMG_test.npy'), EMG_test)
np.save(os.path.join(save_dir, 'label_test.npy'), LABEL_test)