ValueError:具有多个元素的数组的真值是不明确的。使用a.any()

时间:2021-09-03 16:07:53

Here's my code:

这是我的代码:

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

And the error is:

错误是:

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()

Can anyone please help me solving this error..?

任何人都可以帮我解决这个错误..?

Thanks in advance..

提前致谢..

1 个解决方案

#1


The line that produces the error shouldn't give an error if your signal were 1D (ie, a mono audio file), so you probably have a stereo wav file and your signal has the shape (nsamples, 2). Here's a short example for a stereo signal:

如果您的信号是1D(即单声道音频文件),产生错误的行不应该出错,因此您可能有一个立体声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. ]])

Your next line will also give you trouble with a 2D array, so there try:

您的下一行也会给您带来2D阵列的麻烦,所以尝试:

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

#1


The line that produces the error shouldn't give an error if your signal were 1D (ie, a mono audio file), so you probably have a stereo wav file and your signal has the shape (nsamples, 2). Here's a short example for a stereo signal:

如果您的信号是1D(即单声道音频文件),产生错误的行不应该出错,因此您可能有一个立体声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. ]])

Your next line will also give you trouble with a 2D array, so there try:

您的下一行也会给您带来2D阵列的麻烦,所以尝试:

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