ValueError: The truth value of an array with more than one element is ambiguous. Use a.any()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any()

这是我的代码:

from scipy.io import wavfile

fName = 'file.wav'

fs, signal = wavfile.read(fName)
signal = signal / max(abs(signal))                 # scale signal
assert min(signal) >= -1 and max(signal) <= 1

错误是:

Traceback (most recent call last):
  File = "vad.py", line 10, in <module>
    signal = signal / max(abs(signal))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any()

谁能帮我解决这个错误..?

提前致谢..

如果您的信号是 1D(即单声道音频文件),则产生错误的行不应给出错误,因此您可能有一个 stereo wav 文件 和您的信号具有 (nsamples, 2) 的形状。这是一个立体声信号的简短示例:

In [109]: x = (np.arange(10, dtype=float)-5).reshape(5,2)

In [110]: x
Out[110]: 
array([[-5., -4.],
       [-3., -2.],
       [-1.,  0.],
       [ 1.,  2.],
       [ 3.,  4.]])

In [111]: x /= abs(x).max(axis=0)  # normalize each channel independently

In [112]: x
Out[112]: 
array([[-1. , -1. ],
       [-0.6, -0.5],
       [-0.2,  0. ],
       [ 0.2,  0.5],
       [ 0.6,  1. ]])

你的下一行也会给你二维数组带来麻烦,所以试试:

 (x>=-1).all() and (x<=1).all()