簡體   English   中英

如何使用python為簡單的正弦波輸入生成反向間隙信號?

[英]How do you generate a backlash signal for simple sine wave input using python?

我正在使用下面的 python 代碼,以便為簡單的正弦波輸入生成反沖信號。生成的輸出不符合要求。輸出應該類似於 Simulink 中使用的反沖塊的輸出。

#Importing libraries 
import matplotlib.pyplot as plt
import numpy as np

#Setting upper limit and lower limit
LL = -0.5
UL = 0.5

#Generating the sine wave
x=np.linspace(0,10,1000)
y=(np.sin(x))

#phase shift of y1 by -pi/2
y1=(np.sin(x-1.571))

# plot original sine
plt.plot(x,y)

#setting the thresholds 
y1[(y1>UL)] = UL
y1[(y1<LL)] = LL

#Initializing at the input
y1[(y==0)]  = 0

y1[(y1>UL)] -= UL
y1[(y1<LL)] -= LL

#Plotting both the waves
plt.plot(x,y)
plt.plot(x,y1)

plt.grid()
plt.show()

在此處輸入圖片說明

在此處輸入圖片說明

我認為沒有針對反彈過程的簡單矢量化實現。 第 k 個輸出以一種非平凡的方式取決於先前的值。 編寫該過程的簡潔方法(假設x是輸入數組, y是輸出數組)是

y[k] = min(max(y[k-1], x[k] - h), x[k] + h)

其中h是死區的一半。

以下腳本包含一個使用 Python for 循環的backlash函數。 (該函數使用if語句而不是minmax函數。)它很簡單,但速度不會很快。 如果高性能很重要,您可以考慮在Cythonnumba 中重新實現該功能。

import numpy as np


def backlash(x, deadband=1.0, initial=0.0):
    """
    Backlash process.

    This function emulates the Backlash block of Simulink
    (https://www.mathworks.com/help/simulink/slref/backlash.html).

    x must be a one-dimensional numpy array (or array-like).
    deadband must be a nonnegative scalar.
    initial must be a scalar.
    """
    halfband = 0.5*deadband

    y = np.empty_like(x, dtype=np.float64)
    current_y = initial

    for k in range(len(x)):
        current_x = x[k]
        xminus = current_x - halfband
        if xminus > current_y:
            current_y = xminus
        else:
            xplus = current_x + halfband
            if xplus < current_y:
                current_y = xplus
        y[k] = current_y

    return y


if __name__ == "__main__":
    import matplotlib.pyplot as plt

    t = np.linspace(0, 10, 500)
    x = np.sin(t)
    deadband = 1
    y = backlash(x, deadband=deadband)

    plt.plot(t, x, label='x(t)')
    plt.plot(t, y, '--', label='backlash(x(t))')
    plt.xlabel('t')

    plt.legend(framealpha=1, shadow=True)
    plt.grid(alpha=0.5)
    plt.show()

陰謀

更新:我在我的ufunclab 存儲庫中將backlash函數實現為 NumPy gufunc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM