心电信号特征提取代码
时间: 2024-06-11 19:01:49 浏览: 242
心电信号读取代码
心电信号特征提取是对心电信号进行分析和处理的重要步骤,它可以帮助我们提取出不同的特征,如心率、ST段、QRS波等等。下面是一个简单的心电信号特征提取的代码示例,其中主要使用了Python中的心电信号处理库wfdb和信号处理库scipy:
```
import wfdb
import numpy as np
from scipy import signal
# 读取心电信号数据
record = wfdb.rdrecord('sample_data/100', channels=)
ecg = record.p_signal.flatten()
# 带通滤波,去除低频和高频噪声
fs = record.fs
b, a = signal.butter(4, [0.5 / fs, 30 / fs], 'bandpass')
ecg_filtered = signal.filtfilt(b, a, ecg)
# 心拍检测,提取RR间期
qrs_inds = signal.find_peaks(ecg_filtered, distance=fs * 0.5)
rr_intervals = np.diff(qrs_inds) / fs
# 计算心率
hr = 60 / np.mean(rr_intervals)
# 计算ST段和QRS波特征
st_seg_start = int(0.2 * fs)
st_seg_end = int(0.3 * fs)
st_segment = ecg_filtered[st_seg_start:st_seg_end]
st_amplitude = np.max(st_segment) - np.min(st_segment)
qrs_start = qrs_inds - int(0.05 * fs)
qrs_end = qrs_inds + int(0.1 * fs)
qrs_intervals = [ecg_filtered[qrs_start[i]:qrs_end[i]] for i in range(len(qrs_inds))]
qrs_amplitude = [np.max(qrs_intervals[i]) - np.min(qrs_intervals[i]) for i in range(len(qrs_inds))]
# 输出结果
print('Heart rate: {:.2f} bpm'.format(hr))
print('ST amplitude: {:.2f} mV'.format(st_amplitude))
print('QRS amplitude: {:.2f} mV'.format(np.mean(qrs_amplitude)))
```
阅读全文