python – 如何获得信号的高低信封?
我有相当嘈杂的数据,我正试图找出信号的高低信封.它有点像MATLAB中的这个例子:
http://uk.mathworks.com/help/signal/examples/signal-smoothing.html 在“提取峰值信封”中. Python中是否有类似的功能可以做到这一点?我的整个项目都是用Python编写的,最糟糕的情况我可以提取我的numpy数组并将其抛入MATLAB并使用该示例.但是我更喜欢matplotlib的外观……而且真正的cba在MATLAB和Python之间完成所有这些I / O …… 谢谢, 解决方法
据我所知,Numpy / Scipy / Python中没有这样的功能.但是,创建一个并不困难.总体思路如下: 给定值的向量: >找到(s)峰的位置.我们叫他们(你) 如您所见,它是三个步骤(查找位置,拟合模型,评估模型)的序列,但应用了两次,一次用于信封的上半部分,一次用于下部. 要收集(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的连续值之间的差异,可以很容易地添加它. 希望这可以帮助. (如果提供了有关原始申请的更多信息,请尽快提出答复.也许数据可以更合适的方式进行预处理(?)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |