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

时间:2015-05-10 15:36:13

标签: python-2.7 numpy machine-learning speech

这是我的代码:

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

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

提前致谢..

1 个答案:

答案 0 :(得分:1)

如果您的信号是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. ]])

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

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