加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
feature_gen.py 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
import numpy as np
import scipy.io as scio
import heartpy as hp
import libs.SkinVector.SkinVector as SkinVector
import CsvAccess
#20
import os #清除残余文件
#https://scikit-learn.org/stable/modules/model_persistence.html
if os.path.exists('feature.csv'):
os.remove('feature.csv')
# 文件载入
filename_1 = 'n08'
data1 = scio.loadmat('data\\心电\\10_12211013\\' + filename_1 + '.mat')
filename_2 = 'n08'
data2 = scio.loadmat('data\\皮电\\101221\\' + filename_2 + '.mat')
# 统计数据长度
data_ecg = np.array(data1[filename_1])
data_skin = np.array(data2[filename_2])
ecg_length = data_ecg.shape[1]
skin_length = data_skin.shape[0]
#强制转换
data_skin = data_skin.reshape(-1,1)
fs_ecg = 514 # 采样频率
fs_skin = 50
#####################测试环节(不要删除,因为要写入表头)##################
#初始载入一些数据
data_ecg = np.array(data1[filename_1][0][0:1000])
data_skin_temp = data_skin[0:1000]
# # 心电特征提取测试
data = hp.scale_data(data_ecg)
working_data, measures = hp.process(data, fs_ecg)
#皮电提取测试
vector = SkinVector.get_vector(data_skin_temp)
# # 将两个字典嵌套为一个字典写入文件
#
# dict = {'ecg': measures, 'skin': vector}
#
# file = open('cache.pkl', 'w')
# for k,v in dict.items():
# file.write(k + ' ')
# for k1, v1 in v.items():
# file.write(k1 + ' ' + str(v1) + ' ')
# file.write(' \n')
# file.close()
# 将表头写入文件
heads = CsvAccess.dict_key_to_list('number', 'label', measures, vector) # 写入表头
CsvAccess.write('feature.csv', heads)
############### 特征提取部分###########################3
# 参数设置
time = ecg_length / fs_ecg # 计算采集时间
sect_time = 180 # 切片时间单位秒
n_sect = time / sect_time # 计算时间片数量
offset = 30000 # 切片偏移量
for section in range(int(n_sect) - 1):
# 计算切片起始点与终点
end_ecg = offset + sect_time * fs_ecg * (section + 1)
start_ecg = end_ecg - sect_time * fs_ecg
end_skin = sect_time * fs_skin * (section + 1)
start_skin = end_skin - sect_time * fs_skin
# 通过上述计算进行切片
data_ecg = np.array(data1[filename_1][0][start_ecg:end_ecg])
data_skin_split = np.array(data_skin[start_skin:end_skin])
# 计算心电的特征
data = hp.scale_data(data_ecg)
working_data, measures = hp.process(data, fs_ecg)
# 计算皮电的特征
vector = SkinVector.get_vector(data_skin_split)
# 将特征值和标注转成list类型
vaule = CsvAccess.dict_value_to_list(section, 'sad', measures, vector)
# 写入文件
CsvAccess.write('feature.csv', vaule)
print('\n特征已经提取并写入成功')
#从文件读取结果 (测试)
#print(type(get_dict()))
# fs=514 # 采样率
# lowcut=0.5
# highcut=85
# fs=fs*0.5
# b, a = filter.butter(6,[lowcut/fs,highcut/fs],btype='bandpass')
# data_ecg = filter.filtfilt(b, a, data_ecg)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化