IndexError: too many indices for array when trying to plot a spectrogram of a .wav file

IndexError: too many indices for array when trying to plot a spectrogram of a .wav file

我正在尝试绘制 .wav 文件的频谱图。 以下代码行为方式的奇怪之处在于它对某些 .wav 文件有效而对其他文件无效。我怀疑这是因为某些 .wav 文件与其他文件相比具有不同数量的通道。但是我不知道如何确定 .wav 文件包含多少个通道。在 post 我的问题之前,我已经查看了这个堆栈溢出 post:What is a channel in a .wav file format?Do all channels play simultaneaously when a wav file is played?

我在下面粘贴了我的一种方法,它试图将文件路径 (myAudio) 转换为带有文件路径 (fileNameToSaveTo) 的 jpg。

def individualWavToSpectrogram(myAudio, fileNameToSaveTo):
print(myAudio)
#Read file and get sampling freq [ usually 44100 Hz ]  and sound object
samplingFreq, mySound = wavfile.read(myAudio)

#Check if wave file is 16bit or 32 bit. 24bit is not supported
mySoundDataType = mySound.dtype

#We can convert our sound array to floating point values ranging from -1 to 1 as follows

mySound = mySound / (2.**15)

#Check sample points and sound channel for duel channel(5060, 2) or  (5060, ) for mono channel

mySoundShape = mySound.shape
samplePoints = float(mySound.shape[0])

#Get duration of sound file
signalDuration =  mySound.shape[0] / samplingFreq

#If two channels, then select only one channel
mySoundOneChannel = mySound[:,0]

#Plotting the tone

# We can represent sound by plotting the pressure values against time axis.
#Create an array of sample point in one dimension
timeArray = numpy.arange(0, samplePoints, 1)

#
timeArray = timeArray / samplingFreq

#Scale to milliSeconds
timeArray = timeArray * 1000

#Plot the tone
plt.plot(timeArray, mySoundOneChannel, color='Black')
#plt.xlabel('Time (ms)')
#plt.ylabel('Amplitude')
print("trying to save")
plt.savefig('/Users/billybobjoe/Desktop/SavedSpecs' + fileNameToSaveTo + '.jpg')
print("saved")
plt.show()

这会在我的一些 .wav 文件上产生以下错误 第57行,individualWavToSpectrogram mySoundOneChannel = mySound[:,0] IndexError:数组的索引太多

失败的代码行是

mySoundOneChannel = mySound[:,0]

如何检查 .wav 文件的声道数,以及如何相应地设置 mySoundOneChannel?

据我所知,如果有多个通道,数据数组 mySound 的形状将是 (nSamples, nChannels)。如果只有一个通道,mySound 将具有 (nSamples,).

的形状

此处,您的音频文件必须只有一个声道,因此您不能将其作为二维数组进行索引。

因此,您应该可以替换

mySoundOneChannel = mySound[:,0]

类似

if len(mySound.shape) > 1:
    mySoundOneChannel = mySound[:,0]
else:
    mySoundOneChannel = mySound

要获取通道数,您应该可以这样做:

if len(mySound.shape) > 1:
    nChannels = mySound.shape[1]
else:
    nChannels = 1