1
我很熟悉python,但是對於numpy來說很新,所以如果我錯了,請原諒我。從wav文件中提取頻率python
我正在嘗試閱讀具有多個頻率的.wav文件(由沉默分隔)。到目前爲止,我已經能夠讀取這些值並找到文件中存在聲音的各個部分。於是,我試圖找到離散餘弦變換,並從中計算頻率(REF:how to extract frequency associated with fft values in python)
不過,我得到一個錯誤:
index 46392 is out of bounds for axis 0 with size 25
這裏是我的代碼:
import wave
import struct
import numpy as np
def isSilence(windowPosition):
sumVal = sum([ x*x for x in sound[windowPosition:windowPosition+windowSize+1] ])
avg = sumVal/(windowSize)
if avg <= 0.0001:
return True
else:
return False
#read from wav file
sound_file = wave.open('test.wav', 'r')
file_length = sound_file.getnframes()
data = sound_file.readframes(file_length)
sound_file.close()
#data = struct.unpack("<h", data)
data = struct.unpack('{n}h'.format(n=file_length), data)
sound = np.array(data)
#sound is now a list of values
#detect silence and notes
i=0
windowSize = 2205
windowPosition = 0
listOfLists = []
listOfLists.append([])
maxVal = len(sound) - windowSize
while True:
if windowPosition >= maxVal:
break
if not isSilence(windowPosition):
while not isSilence(windowPosition):
listOfLists[i].append(sound[windowPosition:windowPosition+ windowSize+1])
windowPosition += windowSize
listOfLists.append([]) #empty list
i += 1
windowPosition += windowSize
frequencies = []
#Calculating the frequency of each detected note by using DFT
for signal in listOfLists:
if not signal:
break
w = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(w))
l = len(signal)
#imax = index of first peak in w
imax = np.argmax(np.abs(w))
fs = freqs[imax]
freq = imax*fs/l
frequencies.append(freq)
print frequencies
編輯:這裏是回溯:
Traceback (most recent call last):
File "final.py", line 61, in <module>
fs = freqs[imax]
IndexError: index 46392 is out of bounds for axis 0 with size 21
*「我收到一個錯誤:...」*總是包含* complete *錯誤信息(即,完整的追溯)在你的問題。回溯顯示哪條線觸發了錯誤。 –
@WarrenWeckesser沒關係。 'fs = freqs [imax]'這一行導致了錯誤。在上面提供的鏈接中,傅里葉變換是一維的。但是我的測試文件給出了一個多維列表。 –
可能是多通道(立體聲)?如果是這樣,通過取兩個通道的平均值來減少到單聲道。 –