Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于matlab或python的真实心电信号模拟器_Python_Matlab_Numpy_Signal Processing_Scipy - Fatal编程技术网

基于matlab或python的真实心电信号模拟器

基于matlab或python的真实心电信号模拟器,python,matlab,numpy,signal-processing,scipy,Python,Matlab,Numpy,Signal Processing,Scipy,我有一系列rr数据(PQM信号中r-r峰值之间的距离) 我想用matlab或python生成真实的心电信号。我已经找到了一些matlab的资料(ecgmatlab中的内置函数),但我不知道如何从rr数据生成它,我也没有找到python的资料。有什么建议吗?这适合你的需要吗?如果没有,请告诉我。祝你好运 import scipy import scipy.signal as sig rr = [1.0, 1.0, 0.5, 1.5, 1.0, 1.0] # rr time in seconds f

我有一系列rr数据(PQM信号中r-r峰值之间的距离)
我想用matlab或python生成真实的心电信号。我已经找到了一些matlab的资料(
ecg
matlab中的内置函数),但我不知道如何从rr数据生成它,我也没有找到python的资料。有什么建议吗?

这适合你的需要吗?如果没有,请告诉我。祝你好运

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()

Steve Tjoa的回答为我编写以下脚本提供了很好的基础。 它非常相似,只是我将一些代码行分隔开,以使像我这样的N00B更容易理解。我还为心脏增加了一个更长的“休息”期,以提供稍微更精确的复制。该脚本允许您设置以下内容:心率bpm、捕获时间长度、添加的噪声、adc分辨率和adc采样率。我建议安装以运行该脚本。它将安装必要的库,并为您提供优秀的Spyder IDE来运行它


嗨,谢谢你的回答,顺便说一句,通过这篇文章我获得了风滚草徽章。好的,我用了一些类似于你的解决方案的东西,但我的问题是(对于像这样的通用pourpose编程论坛来说,可能是太领域特定了)。我的问题是如何生成真正的ecg信号,不是任何信号。我在网络中发现的更真实的ecg波是这样的:但它根据一些参数计算rr间期,我没有设法只得到一个picewise波的代码(该代码用计算的rr至少做了18个波,我认为由于概率原因,没有简单的方法只得到1个波。)最后,我在$matlab/toolbox/signal/sigdemo/中使用了R.Losada(版权所有1988-2002 the MathWorks,Inc.)的ecg.m仿真,它使用了ecg的非参数化线性插值,并带有一点噪声,以获得更真实的效果。顺便说一下,谢谢你明确的回答。请告诉我,如果您为所有pqrst波找到了更好、更现实的解决方案,请注意,这不是一个现实的ECG,不应用于训练算法等。特别是,即使您对
pqrst
向量使用了现实的节拍,也不会产生好的结果。这是因为心脏周期的各种计时成分不会随R-R间期成比例变化,它们之间的比率可能会发生显著变化。对于更灵活的脚本和环境细节,这应该是正确的答案
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')