【问题标题】:Numpy convolve: comparing step and impulse responsesNumpy convolve:比较阶跃响应和脉冲响应
【发布时间】: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


    【解决方案1】:

    通常使用卷积,与应用它的数组相比,内核(你的脉冲响应)是一些相当小的东西。可能有 3、5 或 9 个元素宽。当然视情况而定!

    在你的情况下,它似乎是数组的一半,对吧?

    我的猜测是,在您的情况下,结果可能不一样,但非常相似,以至于您没有注意到。巨大的内核“涂抹”(或平均)一切。

    【讨论】:

    • 谢谢。从您的评论中,我了解到我最好将脉冲宽度设置为 2 个时间点,以便提供物理输出。我只是用半阵列大小的窗口进行了说明,但它可能要小得多(我正在模拟不同预测范围内 300 秒的延时响应)。
    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    相关资源
    最近更新 更多