加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何获得信号的高低信封?

发布时间:2020-12-16 23:37:38 所属栏目:Python 来源:网络整理
导读:我有相当嘈杂的数据,我正试图找出信号的高低信封.它有点像MATLAB中的这个例子: http://uk.mathworks.com/help/signal/examples/signal-smoothing.html 在“提取峰值信封”中. Python中是否有类似的功能可以做到这一点?我的整个项目都是用Python编写的,最糟
我有相当嘈杂的数据,我正试图找出信号的高低信封.它有点像MATLAB中的这个例子:

http://uk.mathworks.com/help/signal/examples/signal-smoothing.html

在“提取峰值信封”中. Python中是否有类似的功能可以做到这一点?我的整个项目都是用Python编写的,最糟糕的情况我可以提取我的numpy数组并将其抛入MATLAB并使用该示例.但是我更喜欢matplotlib的外观……而且真正的cba在MATLAB和Python之间完成所有这些I / O ……

谢谢,

解决方法

Is there a similar function in Python that can do that?

据我所知,Numpy / Scipy / Python中没有这样的功能.但是,创建一个并不困难.总体思路如下:

给定值的向量:

>找到(s)峰的位置.我们叫他们(你)
>找到s的低谷的位置.我们称之为(l).
>使模型适合(u)值对.我们称之为(u_p)
>使模型适合(l)值对.我们称之为(l_p)
>在(s)的域上评估(u_p)以获得上包络的内插值. (我们称之为(q_u))
>评估(s)域上的(l_p)以获得下包络的内插值. (我们称之为(q_l)).

如您所见,它是三个步骤(查找位置,拟合模型,评估模型)的序列,但应用了两次,一次用于信封的上半部分,一次用于下部.

要收集(s)的“峰值”,您需要找到(s)的斜率从正变为负的点,并收集(??s)的“波谷”,您需要找到斜率为(s)的点. )从负面变为正面.

峰值示例:s = [4,5,4] 5-4为正4-5为负

一个低谷的例子:s = [5,4,5] 4-5是负5-4是正的

这是一个示例脚本,可以帮助您开始使用大量内联注释:

from numpy import array,sign,zeros
from scipy.interpolate import interp1d
from matplotlib.pyplot import plot,show,hold,grid

s = array([1,3,2,6,7,8,8]) #This is your noisy vector of values.

q_u = zeros(s.shape)
q_l = zeros(s.shape)

#Prepend the first value of (s) to the interpolating values. This forces the model to use the same starting point for both the upper and lower envelope models.

u_x = [0,]
u_y = [s[0],]

l_x = [0,]
l_y = [s[0],]

#Detect peaks and troughs and mark their location in u_x,u_y,l_x,l_y respectively.

for k in xrange(1,len(s)-1):
    if (sign(s[k]-s[k-1])==1) and (sign(s[k]-s[k+1])==1):
        u_x.append(k)
        u_y.append(s[k])

    if (sign(s[k]-s[k-1])==-1) and ((sign(s[k]-s[k+1]))==-1):
        l_x.append(k)
        l_y.append(s[k])

#Append the last value of (s) to the interpolating values. This forces the model to use the same ending point for both the upper and lower envelope models.

u_x.append(len(s)-1)
u_y.append(s[-1])

l_x.append(len(s)-1)
l_y.append(s[-1])

#Fit suitable models to the data. Here I am using cubic splines,similarly to the MATLAB example given in the question.

u_p = interp1d(u_x,kind = 'cubic',bounds_error = False,fill_value=0.0)
l_p = interp1d(l_x,l_y,fill_value=0.0)

#Evaluate each model over the domain of (s)
for k in xrange(0,len(s)):
    q_u[k] = u_p(k)
    q_l[k] = l_p(k)

#Plot everything
plot(s);hold(True);plot(q_u,'r');plot(q_l,'g');grid(True);show()

这会产生以下输出:

要进一步改进的要点:

>上述代码不过滤可能比某个阈值“距离”(T1)(例如时间)更近的峰值或波谷.这类似于包络的第二个参数.通过检查u_x,u_y的连续值之间的差异,可以很容易地添加它.
>然而,在前面提到的这一点上的快速改进是在插入上下包络函数之前使用移动平均滤波器对数据进行低通滤波.您可以通过将您的(s)与合适的移动平均滤波器进行卷积来轻松完成此操作.如果没有详细说明(如果需要可以做的话),要产生一个在N个连续样本上运行的移动平均滤波器,你可以这样做:s_filtered = numpy.convolve(s,numpy.ones((1,N) ))/ float(N).(N)越高,您的数据就越平滑.请注意,这会将您的(s)值(N / 2)样本向右移动(在s_filtered中),因为被称为平滑滤波器的group delay.有关移动平均线的更多信息,请参见this link.

希望这可以帮助.

(如果提供了有关原始申请的更多信息,请尽快提出答复.也许数据可以更合适的方式进行预处理(?))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读