2

如何使用python检查信号是否低于参考信号的限制?每个信号都以二维列表的形式给出,例如下面的代码和图表。

#Signal = [[t0, t1, t2, ...], [y(t0), y(t1), y(t2), ...]]
CapturedSignal = [[1.0, 1.9, 2.0, 3.0, 3.1, 4.0], [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]]
ReferenceSignal = [[0.5, 2.4, 2.5, 2.7, 2.8, 4.5], [1.2, 1.2, 0.4, 0.4, 1.2, 1.2]]

参考和捕获的信号 http://www.img-host.de/bild.php/35899,caprefsigWQJ8Z.png

我的问题是,两个信号的采样点不匹配。我可以在两点之间进行插值以获得可比较的值,但也许你知道准备好在 SciPy、NumPy 或其他东西中使用函数。

4

2 回答 2

2

您必须使用插值。它总是涉及一些不确定性(您永远不知道采样点之间的内容),但只要您的采样率足够高,您就会安全。

import numpy as np
import pylab as plt

from scipy.interpolate import interp1d


CapturedSignal = [[1.0, 1.9, 2.0, 3.0, 3.1, 4.0], [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]]
ReferenceSignal = [[0.5, 2.4, 2.5, 2.7, 2.8, 4.5], [1.2, 1.2, 0.4, 0.4, 1.2, 1.2]]

representation_captured = interp1d(CapturedSignal[0], CapturedSignal[1], kind="linear")
representation_reference = interp1d(ReferenceSignal[0], ReferenceSignal[1], kind="linear")

min_x = max(min(CapturedSignal[0]), min(ReferenceSignal[0]))
max_x = min(max(CapturedSignal[0]), max(ReferenceSignal[0]))

xs = np.linspace(min_x, max_x, 100, False)

captured_interpolated = representation_captured(xs)
reference_interpolated = representation_reference(xs)

captured_signal_in_bounds = np.all(captured_interpolated<reference_interpolated)

plt.plot(xs, captured_interpolated, "r-", label="Captured")
plt.plot(CapturedSignal[0], CapturedSignal[1], "rD")
plt.plot(xs, reference_interpolated, "b-", label="Reference")
plt.plot(ReferenceSignal[0], ReferenceSignal[1], "bD")
plt.title("Signal below reference" if captured_signal_in_bounds else "Signal exceeds bounds")

plt.legend(loc='best')
plt.show()    

结果在这个图中:

结果

于 2013-01-07T13:13:19.243 回答
1

无需为此使用 NumPy。您可以使用零阶保持,这意味着您假设信号在样本之间是恒定的。这是一种足够简单的插值,只需几行代码即可。

CapturedSignal = [[1.0, 1.9, 2.0, 3.0, 3.1, 4.0], [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]]
ReferenceSignal = [[0.5, 2.4, 2.5, 2.7, 2.8, 4.5], [1.2, 1.2, 0.4, 0.4, 1.2, 1.2]]

def refat(x):
    "Simple interpolation (Zero-order hold) of reference signal"
    for i, n in enumerate(ReferenceSignal[0]):
        if n > x:
            if i == 0: return None
            return ReferenceSignal[1][i-1]

def capat(x):
    "Simple interpolation of capture signal"
    for i, n in enumerate(CapturedSignal[0]):
        if n > x:
            if i == 0: return None
            return CapturedSignal[1][i-1]   

def aboveref():
    "Check whether there is a captured value above its interpolated reference value and vice versa"
    print ' X  Cap Ref'
    for i, x in enumerate(CapturedSignal[0]):
        cap = CapturedSignal[1][i]
        ref = refat(x)
        print x, cap, ref
        if cap > ref:
            print "Uh oh! At %.1f, the captured signal is above the reference signal!" % x
            return False
    print '---'
    for i, x in enumerate(ReferenceSignal[0]):
        ref = ReferenceSignal[1][i]
        cap = capat(x)
        print x, cap, ref
        if cap > ref:
            print "Uh oh! At %.1f, the captured signal is above the reference signal!" % x
            return False
    return True

aboveref()

结果:

 X  Cap Ref
1.0 0.0 1.2
1.9 0.0 1.2
2.0 1.0 1.2
3.0 1.0 1.2
3.1 0.0 1.2
4.0 0.0 1.2
---
0.5 None 1.2
2.4 1.0 1.2
2.5 1.0 0.4
Uh oh! At 2.5, the captured signal is above the reference signal!
于 2013-01-07T13:30:36.180 回答