查找波形中的离散逻辑电平
Finding discrete logic levels in a waveform
我尝试使用 Python 处理一些波形。我想找到这些信号显示的离散逻辑电平。我有每个波形的 x 和 y 值的一维数组。
数据类似于此示例楼梯:
如何找到不同的级别?在这里它们可能在 (1.8, 3.3)、(2.1, 3.0)、(2.7, 2.6) 等附近。
我尝试遵循 this similar question 中讨论的与 R 相关的方法。我实现了一个 LOWESS 平滑器(这是你看到的曲线),它具有紧密的拟合以消除噪声,因为真实波形具有非- 微不足道的噪音成分,然后尝试对数据进行 window 的滚动最大值,但我无法得到任何可靠的结果。
对于您的简单示例,您可以只定义一个跳跃大小并绘制一个阶跃函数,
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()
这给出了
对于 link 中更复杂的解决方案,我认为这将涉及通过在给定范围内寻找局部 min/max 来构建 ystep,例如:
ystep =[]
for rec in range(0,x.shape[0],skip):
ystep.append(y[argrelmax(y[rec:rec+skip])[0]])
编辑:完整示例给出范围
的本地 min/max
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()
我尝试使用 Python 处理一些波形。我想找到这些信号显示的离散逻辑电平。我有每个波形的 x 和 y 值的一维数组。
数据类似于此示例楼梯:
如何找到不同的级别?在这里它们可能在 (1.8, 3.3)、(2.1, 3.0)、(2.7, 2.6) 等附近。
我尝试遵循 this similar question 中讨论的与 R 相关的方法。我实现了一个 LOWESS 平滑器(这是你看到的曲线),它具有紧密的拟合以消除噪声,因为真实波形具有非- 微不足道的噪音成分,然后尝试对数据进行 window 的滚动最大值,但我无法得到任何可靠的结果。
对于您的简单示例,您可以只定义一个跳跃大小并绘制一个阶跃函数,
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()
这给出了
对于 link 中更复杂的解决方案,我认为这将涉及通过在给定范围内寻找局部 min/max 来构建 ystep,例如:
ystep =[]
for rec in range(0,x.shape[0],skip):
ystep.append(y[argrelmax(y[rec:rec+skip])[0]])
编辑:完整示例给出范围
的本地 min/maximport 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()