从wav文件python中提取频率 [英] Extracting frequencies from a wav file python

查看:189
本文介绍了从wav文件python中提取频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉python,但对numpy还是陌生的,所以如果我错了,请原谅我.

I am familiar with python but new to numpy, so please pardon me if I am wrong.

我正在尝试读取具有多个频率(以静默分隔)的.wav文件.到目前为止,我已经能够读取值并找到声音的文件中的各个部分.然后,我试图找到离散余弦变换并从中计算出频率(参考:

I am trying to read a .wav file having multiple frequencies (separated by silence). So far I've been able to read the values and find the various parts in the file where there is a sound. Then, I am trying to find the Discrete Cosine Transform and calculate the frequencies from it (ref: how to extract frequency associated with fft values in python)

但是,我遇到一个错误:

However, I'm getting an error:

索引46392超出了尺寸为25的轴0的边界

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

推荐答案

问题是我假设listOfLists实际上是列表列表,但实际上是列表列表.该行:

The problem was that I assumed listOfLists was actually a list of lists, but actually it was a list of list of lists. The line:

        listOfLists[i].append(sound[windowPosition:windowPosition+ windowSize+1])

每次都添加一个列表,但是我认为它是将元素添加到现有列表中.

was appending a list everytime, but I assumed it was appending the elements to existing list.

例如,如果listOfLists为:

For instance, if listOfLists was:

[ [1,2,3] ]

然后,listOfLists [0] .append([4,5,6])将给出:

Then, listOfLists[0].append([4,5,6]) would give:

[ [ [1,2,3],[4,5,6] ] ]

但是我期待着:

[ [1,2,3,4,5,6] ]

用下面的代码替换有问题的行对我有用:

Replacing the problematic line with the code below worked for me:

for v in sound[windowPosition:windowPosition+windowSize+1]:
            listOfLists[i].append(v)

这篇关于从wav文件python中提取频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆