查找波形中的离散逻辑电平

时间:2015-07-09 22:29:50

标签: python numpy scipy signal-processing

我有一些波形,我正在尝试用Python处理。我想找到这些信号显示的离散逻辑电平。我有一个每个波形的x和y值的一维数组。

数据类似于此示例楼梯:

example

如何找到不同的关卡?在这里它们可能在(1.8,3.3),(2.1,3.0),(2.7,2.6)等等。

我尝试过遵循this similar question中与R相关的方法。我实现了一个LOWESS平滑器(这是你看到的曲线),紧密贴合以消除噪音,因为真正的波形非常重要噪声组件,然后尝试使用窗口覆盖数据进行滚动最大化,但我无法获得任何实体。

1 个答案:

答案 0 :(得分:0)

对于您的简单示例,您只需定义跳过大小并绘制步进函数,

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import argrelmin, argrelmax

#Generate some example signal
res = 1000
x = np.linspace(0,2*np.pi,res)
y = np.sin(x)
plt.plot(x,y)

#Take a number of discrete steps
Nsteps = 10
skip = x.shape[0]/Nsteps
xstep = x[::skip]; ystep = y[::skip]
plt.plot(xstep,ystep,'o')

#Plot the step graph
plt.step(xstep, ystep)

#Get max and min (only 2 in this simple case)
plt.plot(x[argrelmax(y)[0]],y[argrelmax(y)[0]],'ro')
plt.plot(x[argrelmin(y)[0]],y[argrelmin(y)[0]],'ro')
plt.show()

给出了

enter image description here

对于链接中更复杂的解决方案,我认为这将涉及通过在给定范围内查找本地最小值/最大值来构建ystep,例如:

ystep =[]
for rec in range(0,x.shape[0],skip):
    ystep.append(y[argrelmax(y[rec:rec+skip])[0]])

编辑:给出范围的本地最小/最大值的完整示例

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import argrelmin, argrelmax

#Generate some example signal
res = 1000
x = np.linspace(0,2*np.pi,res)
y = np.sin(x)
plt.plot(x,y)

#Take a number of discrete steps
Nsteps = 10
skip = x.shape[0]/Nsteps

#Get local minimum and maximum in blocks for whole set of data
xstep = []; ystep =[]
for rec in range(0,x.shape[0],skip):
    srt = rec
    end = rec+skip-1
    if (y[srt] > y[end]):
        indx = argrelmax(y[srt:end],mode='wrap')[0]
    elif (y[srt] < y[end]):
        indx = argrelmin(y[srt:end],mode='wrap')[0]

    xstep.append(x[srt+indx])
    ystep.append(y[srt+indx])

#Plot location of min/max
plt.plot(xstep,ystep,'o')

#Plot the step graph
plt.step(xstep, ystep)

plt.show()