基于RR数据的真实心电图信号模拟器(Matlab或Python)
我有一系列的rr数据(就是心电图信号中r-r峰之间的距离),我想在matlab或python中生成真实的心电图信号。我找到了一些关于matlab的资料(比如matlab里的ecg
内置函数),但是我不知道怎么从rr数据生成心电图。而对于python,我什么资料都没找到。有没有什么建议?
2 个回答
8
Steve Tjoa 的回答给了我很好的基础,让我写出了下面这个脚本。这个脚本和他的很相似,不过我把一些代码行分开了,这样像我这样的编程小白更容易理解。我还增加了心脏的“休息”时间,这样可以更准确地模拟心跳。这个脚本让你可以设置以下内容:心率(bpm)、捕捉的时间长度、添加的噪声、ADC(模数转换器)分辨率和采样率。我建议你安装Anaconda来运行这个脚本。它会安装所需的库,并提供一个很棒的Spyder IDE来运行它。
import pylab
import scipy.signal as signal
import numpy
print('Simulating heart ecg')
# The "Daubechies" wavelet is a rough approximation to a real,
# single, heart beat ("pqrst") signal
pqrst = signal.wavelets.daub(10)
# Add the gap after the pqrst when the heart is resting.
samples_rest = 10
zero_array = numpy.zeros(samples_rest, dtype=float)
pqrst_full = numpy.concatenate([pqrst,zero_array])
# Plot the heart signal template
pylab.plot(pqrst_full)
pylab.xlabel('Sample number')
pylab.ylabel('Amplitude (normalised)')
pylab.title('Heart beat signal Template')
pylab.show()
# Simulated Beats per minute rate
# For a health, athletic, person, 60 is resting, 180 is intensive exercising
bpm = 60
bps = bpm / 60
# Simumated period of time in seconds that the ecg is captured in
capture_length = 10
# Caculate the number of beats in capture time period
# Round the number to simplify things
num_heart_beats = int(capture_length * bps)
# Concatonate together the number of heart beats needed
ecg_template = numpy.tile(pqrst_full , num_heart_beats)
# Plot the heart ECG template
pylab.plot(ecg_template)
pylab.xlabel('Sample number')
pylab.ylabel('Amplitude (normalised)')
pylab.title('Heart ECG Template')
pylab.show()
# Add random (gaussian distributed) noise
noise = numpy.random.normal(0, 0.01, len(ecg_template))
ecg_template_noisy = noise + ecg_template
# Plot the noisy heart ECG template
pylab.plot(ecg_template_noisy)
pylab.xlabel('Sample number')
pylab.ylabel('Amplitude (normalised)')
pylab.title('Heart ECG Template with Gaussian noise')
pylab.show()
# Simulate an ADC by sampling the noisy ecg template to produce the values
# Might be worth checking nyquist here
# e.g. sampling rate >= (2 * template sampling rate)
sampling_rate = 50.0
num_samples = sampling_rate * capture_length
ecg_sampled = signal.resample(ecg_template_noisy, num_samples)
# Scale the normalised amplitude of the sampled ecg to whatever the ADC
# bit resolution is
# note: check if this is correct: not sure if there should be negative bit values.
adc_bit_resolution = 1024
ecg = adc_bit_resolution * ecg_sampled
# Plot the sampled ecg signal
pylab.plot(ecg)
pylab.xlabel('Sample number')
pylab.ylabel('bit value')
pylab.title('%d bpm ECG signal with gaussian noise sampled at %d Hz' %(bpm, sampling_rate) )
pylab.show()
print('saving ecg values to file')
numpy.savetxt("ecg_values.csv", ecg, delimiter=",")
print('Done')
13
这个符合你的需求吗?如果不符合,请告诉我。祝你好运。
import scipy
import scipy.signal as sig
rr = [1.0, 1.0, 0.5, 1.5, 1.0, 1.0] # rr time in seconds
fs = 8000.0 # sampling rate
pqrst = sig.wavelets.daub(10) # just to simulate a signal, whatever
ecg = scipy.concatenate([sig.resample(pqrst, int(r*fs)) for r in rr])
t = scipy.arange(len(ecg))/fs
pylab.plot(t, ecg)
pylab.show()