scipy.signal.square¶
- scipy.signal.square(t, duty=0.5)[源代码]¶
返回周期方波波形。
方波有一个周期
2*pi
,值为+1,范围从0到2*pi*duty
和-1来自2*pi*duty
至2*pi
。 duty 必须在间隔内 [0,1] 。请注意,这不受频带限制。它产生无限数量的谐波,这些谐波在频谱中来回混叠。
- 参数
- tarray_like
输入时间数组。
- dutyARRAY_LIKE,可选
工作周期。默认值为0.5(50%占空比)。如果是数组,则会导致波形随时间变化,并且长度必须与t相同。
- 退货
- yndarray
包含方波的输出数组。
示例
以500 Hz采样1秒的5 Hz波形:
>>> from scipy import signal >>> import matplotlib.pyplot as plt >>> t = np.linspace(0, 1, 500, endpoint=False) >>> plt.plot(t, signal.square(2 * np.pi * 5 * t)) >>> plt.ylim(-2, 2)
脉宽调制正弦波:
>>> plt.figure() >>> sig = np.sin(2 * np.pi * t) >>> pwm = signal.square(2 * np.pi * 30 * t, duty=(sig + 1)/2) >>> plt.subplot(2, 1, 1) >>> plt.plot(t, sig) >>> plt.subplot(2, 1, 2) >>> plt.plot(t, pwm) >>> plt.ylim(-1.5, 1.5)