在 Python 中绘制斜坡脉冲函数

Plot ramp pulse function in Python

我必须解决此练习,并且真的不知道要更改代码中的哪些内容才能在区间 [3,4] 上获得斜坡,因为我对 Python 还很陌生.这是实际问题:

x(A, t1, t2, t) = A/(t2 - t1) * (t - t1) 如果 t1 <= t < t2 AND 0 否则

通过绘制幅度为 1 的信号的 myRampPulse 的值和包络进行测试, 从第 3 秒开始到第 4 秒结束,对于频率为 20 Hz 的采样,从第 2 秒开始 0 并在第 5 秒结束。

我已经在下面粘贴了我的代码:

import numpy as np
import matplotlib.pyplot as plt


A = 1       # amplitude
t1 = 3      # start time
t2 = 4      # stop time
fs = 20     # sampling frequency

t = np.arange(0, 5, 1/fs)

def myRampPulse(A, t1, t2, t):
    while(len(t)):
        return (A*((t>=t1)&(t<t2)))

result = myRampPulse(A, t1, t2, t)

plt.close('all')
plt.plot(t, result, '.')
plt.axis([0, 5, -2, 2])       
plt.title('Ramp pulse signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

非常感谢任何帮助!提前致谢!

用这个替换你的 myRampPulse() 函数

def myRampPulse(A, t1, t2, t):
    while(len(t)):
        return (A*((t>=t1)&(t<t2))) * (t-t1) / (t2-t1)