查找框图的脉冲响应

Finding impulse response of the block diagram

我有以下 block diagram and each subsystem,我需要找到 0<=n<=99 的总脉冲响应。

找到每个子系统的单独脉冲响应,如图所示。

import numpy as np
import matplotlib.pyplot as plt

n1 = np.arange(0, 6, 1) # set upper limit = 5 and lower limit = 0 in steps of 1.
h1 = [1, 1/2, 1/4, 1/8, 1/16, 1/32] #impulse response h1
plt.stem(n1, h1)
n2 = np.arange(0, 6, 1) # set upper limit = 5 and lower limit = 0 in steps of 1.
h2 = [1, 1, 1, 1, 1, 0] #impulse response h2
plt.stem(n2, h2)
n3 = np.arange(0, 6, 1) # set upper limit = 5 and lower limit = 0 in steps of 1.
h3 = [1/4, 1/2, 1/3, 0, 0, 0] #impulse response h3
plt.stem(n3, h3)

然后我执行了以下卷积和加法以找到整体脉冲响应:

h_t = np.convolve(h1,h2, 'full') + h3

但是我得到了以下错误。

ValueError: operands could not be broadcast together with shapes (11,) (6,) 

不确定我应该如何合并所考虑的 n 个值的范围。

您需要填充各种脉冲响应,使它们的形状匹配。实际上,您显示的脉冲响应只是非零值。之后,你可以用零填充到无穷大时间。

def pad(y, t):
    return np.pad(y, (0, len(t) - len(y)), constant_values=0)

t = np.arange(20)  # or change to 100 for 0<=n<=99, but that's lots of zeros...
h1 = [1, 1/2, 1/4, 1/8, 1/16, 1/32]  # impulse response h1
h2 = [1, 1, 1, 1, 1]  # impulse response h2
h3 = [1/4, 1/2, 1/3]  # impulse response h3
h_t = pad(np.convolve(h1, h2, 'full'), t) + pad(h3, t)


# or, equivalently:

t = np.arange(20)
h1 = pad([1, 1/2, 1/4, 1/8, 1/16, 1/32], t)  # impulse response h1
h2 = pad([1, 1, 1, 1, 1], t)  # impulse response h2
h3 = pad([1/4, 1/2, 1/3], t)  # impulse response h3
h_t = np.convolve(h1, h2, 'full')[:len(t)] + h3


plt.stem(t, pad(h1, t), markerfmt='C0o')
plt.stem(t, pad(h2, t), markerfmt='C1o')
plt.stem(t, pad(h3, t), markerfmt='C2o')
plt.stem(t, h_t, markerfmt='C3-')

附录

为了解决 y 比我们需要的长的情况,修改函数可以根据需要裁剪或填充:

def take(y, n):
    y = y[:n]
    return np.pad(y, (0, n - len(y)), constant_values=0)

示例:

n = 20
t = np.arange(n)
h1 = take([1, 1/2, 1/4, 1/8, 1/16, 1/32], n)  # impulse response h1
h2 = take([1, 1, 1, 1, 1], n)  # impulse response h2
h3 = take([1/4, 1/2, 1/3], n)  # impulse response h3
h_t = take(np.convolve(h1, h2, 'full'), n) + h3

还有:

X = take(np.exp(-t/10), n)
h_t = take(np.convolve(h1, X, 'full'), n) + h3