-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocess.py
More file actions
80 lines (65 loc) · 2.3 KB
/
Copy pathPreprocess.py
File metadata and controls
80 lines (65 loc) · 2.3 KB
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
# TODO:心脏带数据预处理
# - 使用环境变量/相对路径配置数据文件
# BCG_ROW_PATH/ECG_ROW_PATH/ECG_RPEAKS_PATH,默认读取 data/ 下文件
import os
import heartpy as hp
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import wfdb
from sklearn import preprocessing
from biosppy.signals import ecg
BCG_Rowfpath = os.getenv('BCG_ROW_PATH', 'data/BCG_ROW.csv')
ECG_Rowfpath = os.getenv('ECG_ROW_PATH', 'data/ECG_RECEIVE.csv')
ECG_Rpeaks = os.getenv('ECG_RPEAKS_PATH', 'data/Rpeaks.csv')
# savepath
# BCG_Beats = "/Users/gyw/Desktop/Projec/基于睡眠检测的闭环自适应分类/BCG-ECG/BCG处理/BCG_Beats.csv"
sample_rate = 1000
R_before = 0.36
R_after = 0.5
Sam_before = R_before * sample_rate
Sam_after = R_after * sample_rate
def filter(data, sample_rate):
filtered = hp.remove_baseline_wander(data, sample_rate)
return filtered
def arctan_normalization(X):
# 归一化
min_max_scaler = preprocessing.MinMaxScaler(feature_range=[-1, 1])
x = np.asarray(X).reshape(-1, 1)
x_minmax = min_max_scaler.fit_transform(x)
return np.array(x_minmax)
def moving_average(interval, windowsize):
window = np.ones(int(windowsize)) / float(windowsize)
re = np.convolve(interval, window, 'valid')
return re
#
BCGBeats = []
ECGBeats = []
if __name__ == '__main__':
# load data
start = 30000
stop = start + 20000
BCG = pd.read_csv(BCG_Rowfpath).dropna(axis=1)
ECG = pd.read_csv(ECG_Rowfpath).dropna(axis=1)
BCG = arctan_normalization(BCG)
ECG = arctan_normalization(ECG)
Rpeaks = pd.read_csv(ECG_Rpeaks).dropna(axis=1)
plt.figure(figsize=(30, 15))
plt.plot(BCG, color="blue", label="BCG", alpha=1)
plt.plot(ECG, color="red", label="ECG", alpha=1)
plt.title("Synchronous acquisition signal:WXK-1013", fontsize=50)
plt.yticks(fontproperties='Times New Roman',
size=20, weight='bold') # 设置大小及加粗
plt.xticks(fontproperties='Times New Roman', size=20, weight='bold')
plt.legend(fontsize=20)
plt.show()
print(Rpeaks)
for i in Rpeaks:
i = int(i)
bcg_beat = BCG[i - 360:i + 500]
ecg_beat = ECG[i - 360:i + 500]
BCGBeats.append(bcg_beat)
ECGBeats.append(ecg_beat)
plt.plot(ecg_beat)
plt.plot(bcg_beat)
plt.show()