我是一名新手,对信号处理(以及numpy、scipy和matlab)一无所知。我正在尝试使用Python中的LPC来估算元音共振峰,通过改编这个matlab代码:http://www.mathworks.com/help/signal/ug/formant-estimation-with-lpc-coefficients.html。以下是我的代码:
#!/usr/bin/env python
import sys
import numpy
import wave
import math
from scipy.signal import lfilter, hamming
from scikits.talkbox import lpc
"""
Estimate formants using LPC.
"""
def get_formants(file_path):
# Read from file.
spf = wave.open(file_path, 'r') # http://www.linguistics.ucla.edu/people/hayes/103/Charts/VChart/ae.wav
# Get file as numpy array.
x = spf.readframes(-1)
x = numpy.fromstring(x, 'Int16')
# Get Hamming window.
N = len(x)
w = numpy.hamming(N)
# Apply window and high pass filter.
x1 = x * w
x1 = lfilter([1., -0.63], 1, x1)
# Get LPC.
A, e, k = lpc(x1, 8)
# Get roots.
rts = numpy.roots(A)
rts = [r for r in rts if numpy.imag(r) >= 0]
# Get angles.
angz = numpy.arctan2(numpy.imag(rts), numpy.real(rts))
# Get frequencies.
Fs = spf.getframerate()
frqs = sorted(angz * (Fs / (2 * math.pi)))
return frqs
print get_formants(sys.argv[1])
使用这个文件作为输入,我的脚本返回以下列表:
[682.18960189917243, 1886.3054773107765, 3518.8326108511073, 6524.8112723782951]
我甚至没有进行最后的步骤,即通过带宽过滤频率,因为列表中的频率不正确。 根据Praat的说法,我的结果应该像这样(这是元音的中间形式列表):
Time_s F1_Hz F2_Hz F3_Hz F4_Hz
0.164969 731.914588 1737.980346 2115.510104 3191.775838
我错在哪里了?
非常感谢
更新:
我将这个
x1 = lfilter([1., -0.63], 1, x1)
改成了
x1 = lfilter([1], [1., 0.63], x1)
根据Warren Weckesser的建议,现在得到的结果是
[631.44354635609318, 1815.8629524985781, 3421.8288991389031, 6667.5030877036006]
我觉得我可能漏掉了什么,因为F3偏差很大。
更新2:
我意识到传递给scikits.talkbox.lpc
的order
不正确,因为采样频率有所不同。我将其更改为:
Fs = spf.getframerate()
ncoeff = 2 + Fs / 1000
A, e, k = lpc(x1, ncoeff)
现在我得到的是:
[257.86573127888488, 774.59006835496086, 1769.4624576002402, 2386.7093679399809, 3282.387975973973, 4413.0428174593926, 6060.8150432549655, 6503.3090645887842, 7266.5069407315023]
非常接近 Praat 的估计值!