gpt4 book ai didi

python - IndexError : too many indices for array when trying to plot a spectrogram of a . wav 文件

转载 作者:行者123 更新时间:2023-12-03 01:44:20 25 4
gpt4 key购买 nike

我正在尝试绘制 .wav 文件的频谱图。
下面代码行为方式的奇怪之处在于它适用于某些 .wav 文件而在其他文件上失败。我怀疑这是因为某些 .wav 文件与其他文件相比具有不同数量的 channel 。但是我不知道如何确定 .wav 文件包含多少个 channel 。在发布我的问题之前,我已经查看了这个堆栈溢出帖子: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 行,in individualWavToSpectrogram
mySoundOneChannel = mySound[:,0]
IndexError:数组的索引过多

失败的代码行是
mySoundOneChannel = mySound[:,0]

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

最佳答案

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

在这里,您的音频文件必须只有一个 channel ,因此您不能将其索引为 2D 数组。

因此,您应该能够更换

mySoundOneChannel = mySound[:,0]

有类似的东西
if len(mySound.shape) > 1:
mySoundOneChannel = mySound[:,0]
else:
mySoundOneChannel = mySound

要获取 channel 数,您应该能够:
if len(mySound.shape) > 1:
nChannels = mySound.shape[1]
else:
nChannels = 1

关于python - IndexError : too many indices for array when trying to plot a spectrogram of a . wav 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44813763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com