当前位置: 动力学知识库 > 问答 > 编程问答 >

numpy - I am getting an error in python code

问题描述:

Traceback (most recent call last):

File "C:\Users\Ian\Downloads\main_stereo.py", line 56, in <module>

leftCh = myconv(leftimp, waveData) # convolution of left ear impulse response and waveData

File "C:\Users\Ian\Downloads\my_conv.py", line 34, in myconv

z=[0]*len_h+x

ValueError: operands could not be broadcast together with shapes (40001) (128)

My convolution is what is giving the trouble, it is giveing me the above error and I cannot for the life of me figure out what I am doing wrong

mycon.py

import numpy as np

def myconv(x,h):

# INPUT PARAMETERS---------------------------------------------------------

# x: input signal

# h: impulse response

here are the inputs to the function:

which I believe is an array

[ 5.94000000e+02 -7.70000000e+02 9.75000000e+02 -1.17400000e+03

1.32740000e+04 1.02210000e+04 -2.80010000e+04 -1.13600000e+03

1.43450000e+04 -9.92000000e+02 3.42200000e+03 2.17110000e+04

1.11780000e+04 -1.48470000e+04 2.62200000e+03 1.03150000e+04

1.48400000e+03 -3.60800000e+03 -3.51100000e+03 -5.28700000e+03

-1.33000000e+03 4.20000000e+02 -4.47000000e+03 -3.88100000e+03

-3.54200000e+03 -8.05000000e+02 -1.83300000e+03 -1.08700000e+03

9.00000000e+02 -2.83000000e+02 -1.31000000e+02 -9.43000000e+02

-1.33000000e+03 -2.49000000e+03 -1.04400000e+03 -1.73100000e+03

-1.56700000e+03 -1.36500000e+03 -2.01500000e+03 -1.20100000e+03

-1.08500000e+03 -6.01000000e+02 -7.44000000e+02 -2.92000000e+02

-9.61000000e+02 -9.57000000e+02 -8.16000000e+02 -7.85000000e+02

-2.51000000e+02 -4.10000000e+01 -4.42000000e+02 -4.09000000e+02

-1.54000000e+02 6.70000000e+01 1.70000000e+01 -3.55000000e+02

-6.34000000e+02 -6.22000000e+02 -6.10000000e+02 -5.62000000e+02

-2.04000000e+02 -3.24000000e+02 -2.85000000e+02 -4.50000000e+02

-2.13000000e+02 -3.50000000e+01 -4.50000000e+01 7.50000000e+01

7.10000000e+01 -1.02000000e+02 -1.84000000e+02 -2.09000000e+02

-2.30000000e+02 -4.60000000e+01 9.80000000e+01 -7.30000000e+01

-2.02000000e+02 -7.90000000e+01 4.00000000e+00 8.00000000e+00

9.00000000e+00 6.60000000e+01 -6.40000000e+01 -5.40000000e+01

-8.60000000e+01 -7.80000000e+01 5.30000000e+01 1.08000000e+02

4.10000000e+01 -6.20000000e+01 -2.00000000e+02 -1.26000000e+02

-2.00000000e+00 1.30000000e+02 1.93000000e+02 8.40000000e+01

-1.20000000e+02 -2.74000000e+02 -9.30000000e+01 7.70000000e+01

1.26000000e+02 -1.50000000e+01 -5.60000000e+01 -1.26000000e+02

-1.17000000e+02 1.20000000e+01 7.20000000e+01 7.20000000e+01

-4.20000000e+01 -1.14000000e+02 -1.22000000e+02 3.00000000e+00

1.61000000e+02 2.00000000e+02 1.43000000e+02 5.30000000e+01

5.00000000e+01 1.75000000e+02 2.10000000e+02 1.92000000e+02

1.31000000e+02 9.20000000e+01 7.50000000e+01 1.20000000e+02

1.87000000e+02 1.69000000e+02 1.15000000e+02 9.10000000e+01]

and this which is another array:

[-0.00985718 -0.00982666 -0.00912476 ..., 0.00064087 0.00015259

-0.00061035]

网友答案:

You cannot mix np.arrays() and lists. Instead of [0]*len_h+x you should write np.zeros(len_h) + np.asarray(x). To clarify:

import numpy as np

a = [1,2,3]
np_a = np.array(a)

print(a+a)  # gives [1,2,3,1,2,3]
print(np_a + np_a) # gives[2,4,6] of type np.array()
分享给朋友:
您可能感兴趣的文章:
随机阅读: