我使用iPython查找只有一个音高的WAV文件的音高。我的方法是找到基频和谐波。然后以某种方式使用这些值来计算音高。我找到基本方法的方法是找到峰值频率的所有索引,然后将它们存储在一个数组中。我的方法有误吗?我怎么办,因为即时通讯文件只有一个音调。我设法找到所有组件,但我不确定如何使用它们来找到基本组件。或者我应该使用周期时间并尝试找到该时段的最高频率。
def getPitchInHz(filename):
file3 = wavReadMono(filename)
play(file3)
spec = abs(fft.fft(file3)) #compute the magnitude spectrum
rate = 44100 #44100 Hz
freqs = fft.fftfreq(size(spec))*rate #array of frequencies corresponding to bins
Spec = spec[0:size(spec)/2] #spectrum up to the Nyquist
duration = size(Spec)/rate
#periodic time = 1/frequency
pt = 1/freqs
t = arange(0, duration, 1/rate) #t is an array of equally-spaced time points
#plot(t[0:size(spec)/2], spec[0:size(spec)/2])
harmonics = 5
i = 0
plot(freqs[0:6000], Spec[0:6000]) #
xlabel("Frequency, Hz")
ylabel("Magnitude")
#v = []
#print "Fundamental frequency approx. ", freqs[peakIndex], "Hz"
print "Duration", duration
print "The length of the spectrum is ", size(Spec)
while i < harmonics:
peakIndex = argmax(Spec)#find index for peak frequency
if peakIndex in v: #if index is already in array do
Spec[peakIndex] = 0 #Set this peak to zero so that we can use argmax to find the next highest:
else:
v[i] = peakIndex #then add index to array
i+1
for x in range( len(v)):#loop through array of index starting with the peak frequency that occurs earliet
print "harmonic ", x , " Frequency ", freqs[v[x]], "Hz ", abs(Spec[v[x]]), "magnitude"
return pitch