代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。