【发布时间】:2020-06-17 14:52:48
【问题描述】:
我是数字信号处理的新手,在开发我的第一个基于模型的控制方法时,我第一次面临在任何学校环境之外计算卷积的需求(我的背景是数值模拟的另一个领域)。我将this question 中的代码改编为我的问题,我凭经验知道步进时间响应并且一切都很好。我理解需要将我系统中的 impulse_response 乘以时间步长,没有任何困难。
tend = 600.0 # Total time [s]
dt = 15.0 # Time step [s]
k = 0.01 # Time constant [s]
sp = 0.15 # Step height [a.u.]
# Time interval
t = np.arange(0, tend + 1, dt)
# Define step function.
H = np.ones_like(t)
H[0] = 0.5
def impulse_response(t):
""" Unit response. """
return k * np.exp(-k * t) * H
# Response to step. Multiply by dt in discrete case.
response = np.convolve(H, impulse_response(t) * dt, 'full')
response = response[:len(response)//2]
# Define new, longer, time array for plotting response - must
# be same length as response, with step dt
tp = np.arange(len(response)) * dt
t = np.arange(-tend, tend, dt)
Hp = sp * np.ones_like(t)
Hp[t < 0] = 0.0
plt.style.use('bmh')
plt.step(t, Hp, label='Unit step function')
plt.plot(tp, sp * response, label='Response')
plt.tight_layout()
plt.xlabel('Time (s)')
plt.ylabel('Response [a.u.]')
plt.xlim(-100, tend + 1)
plt.legend()
我在上面的代码中唯一不明白的一点是,如果我在下面的函数中不乘以H,为什么会得到不同的结果(如参考答案和我的理解中所述,因为我的步高t
def impulse_response(t):
""" Unit response. """
return k * np.exp(-k * t) * H
我的问题:在动态系统的实际应用中,我将需要计算卷积的任意信号。为了确保我很好地理解了问题陈述并验证了开发的后续步骤,我试图检查这个函数对于单个单位增量脉冲的作用。这种增量的时间长度为零,我相信这就是为什么我在单脉冲情况下不需要乘以dt 的原因。这是正确的吗?通过这样做,我得到了预期的分析解决方案,但我对自己的结论没有信心。下面是修改后的代码。
# Define impulse function.
d = np.zeros_like(t)
d[0] = 1.0
def impulse_response(t):
""" Unit response. """
return k * np.exp(-k * t)
# Response to impulse. Multiply by dt in discrete case.
response = np.convolve(d, impulse_response(t), 'full')
response = response[:len(response)//2]
# Define new, longer, time array for plotting response - must
# be same length as response, with step dt
tp = np.arange(len(response)) * dt
plt.style.use('bmh')
plt.plot(tp, response, label='Response')
plt.plot(tp, k * np.exp(-k * tp), label='Analytical')
plt.tight_layout()
plt.xlabel('Time (s)')
plt.ylabel('Response [a.u.]')
plt.legend()
【问题讨论】:
标签: python numpy signal-processing convolution