Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python问题(卷积)_Python_Numpy - Fatal编程技术网

Python问题(卷积)

Python问题(卷积),python,numpy,Python,Numpy,如何在不使用预定义方法的情况下为for循环编写三行代码 我试过: h = np.array([1, 1, 1, 0, 0]) x = np.array([0.5, 2, 0, 0, 0]) length = len(h)+len(x)-1 h_conv, h_rev, x_conv, y_conv = np.zeros(length), np.zeros(length), np.zeros(length), np.zeros(length) x_conv[:len(x)] = x h_rev

如何在不使用预定义方法的情况下为for循环编写三行代码

我试过:

h = np.array([1, 1, 1, 0, 0])
x = np.array([0.5, 2, 0, 0, 0])

length = len(h)+len(x)-1

h_conv, h_rev, x_conv, y_conv = np.zeros(length), np.zeros(length), np.zeros(length), np.zeros(length)
x_conv[:len(x)] = x
h_rev[length-len(h):] = h[::-1].copy()

for t in range(length):
    h_conv[:t+1] = h_rev[length-t-1:]
    y_conv[t] = np.sum(x_conv * h_conv)

>>> y
[0.5, 2.5, 2.5, 2., 0., 0., 0., 0., 0.]

代码的问题是
j
可能大于
i
。你是说:

for i in range(length):
    y[i]=0;
    for j in range(length):
        y[i] += x[i-j]*h[j];


Error index 0 is out of bounds for axis with size 0
或者可能是
i+1

for i in range(length):
    y[i]=0;
        for j in range(i):
        y[i] += x[i-j]*h[j];