您可以使用以下命令生成信号:numpy
Python 模块。您可以将脉冲添加到该信号中(当然,如果尺寸正确)。我为你写了一个例子,我用它生成了一个正弦信号numpy
我已经添加了脉冲signal.unit_impulse()
就像你的问题一样。为了更好地理解,我在代码中添加了一些注释。
Code:
import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal
positive_impulses = signal.unit_impulse(200, [10, 50, 60]) # Generate positive impulses
negative_impulses = signal.unit_impulse(200, [80, 100, 150])*-1 # Generate negative impulses
# Generate the Sinus signal.
t = np.linspace(1, 200, 200)
x_sin_sig = np.sin(t / (2 * np.pi))
plt.subplot(4, 1, 1)
plt.plot(t, x_sin_sig)
plt.title("Signal")
plt.ylabel("Sin")
plt.subplot(4, 1, 2)
plt.plot(t, x_sin_sig + positive_impulses) # Add the positive impulses to the original signal
plt.title("Signal with positive impulses")
plt.subplot(4, 1, 3)
plt.plot(t, x_sin_sig + negative_impulses) # Add the negative impulses to the original signal
plt.title("Signal with negative impulses")
plt.subplot(4, 1, 4)
plt.plot(t, x_sin_sig + positive_impulses + negative_impulses) # Add the both impulses to the original signal
plt.title("Signal with different impulses")
plt.tight_layout()
plt.show()
Output:
Note:
为信号生成随机脉冲可能更有效。你可以用random
module.
import random
positive_impulses = signal.unit_impulse(200, random.sample(range(200), 5)) # Generate positive impulses
negative_impulses = signal.unit_impulse(200, random.sample(range(200), 5))*-1 # Generate negative impulses
The random.sample(range(200), 5)
将返回从 0 到 200 范围内选择的 5 个数字的列表,不重复。
随机脉冲的示例: