当只有一个音调的声音时寻找音调?

2024-04-19 01:43:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我用iPython找到一个只有一个音高的WAV文件的音高。我的方法是找到基频及其谐波。然后以某种方式使用这些值来计算音高。我找到基频的方法是找到峰值频率的所有索引,然后将它们存储在一个数组中。我的方法错了吗?既然imwav文件只有一个音调,我该怎么办呢。我已经设法找到了所有的组成部分,但我不知道如何使用它们找到基本的。或者我应该使用周期时间,并试图找到该周期的最高频率。你知道吗

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

Tags: ofthetosizeindexratearrayprint