检查信号是否在另一个标志给出的限值内

2024-09-28 05:18:28 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用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]]

reference and captured signal http://www.img-host.de/bild.php/35899,caprefsigWQJ8Z.png

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


Tags: and代码列表signal信号图表形式reference
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()    

此图中的结果:

Resuls

不需要使用NumPy。您可以使用zero-order hold,这意味着您假设样本之间的信号是恒定的。这是一种非常简单的插值,只需几行代码。你知道吗

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!

相关问题 更多 >

    热门问题