基频(峰值、基音)不准确,stft倒谱在高频

2024-04-29 08:04:34 发布

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

我试着检测声音的基频

我用倒谱来求频率。你知道吗

除了高频外,它工作得很好。你知道吗

我想检测钢琴的最高频率,C8(4186Hz)

然而,要检测4186Hz的采样率44100,我需要quefrency 10.5这是不可能的,因为quefrency是样本数。你知道吗

所以我得到10个频率,44100/10=4410Hz,接近C#8(4435Hz)

我应该如何检测准确的基频。你知道吗

编辑:

samplerate, samples = wav.read(audiopath)
print "samplerate= " + str(samplerate)
samples = (samples[:, 0] + samples[:, 1]) / 2
overlapFac = 0.9
s = stft(samples, binsize, overlapFac)

break_flag = False
fund_freq_list = []
for i in range(len(s)):
    ceps = np.fft.irfft(np.power(np.log(np.abs(s[i])), 2))      
    for nan in np.isnan(ceps):      
        if nan:
            break_flag = True
            break

    if break_flag:
        break

    plt.plot(ceps)


    ignore_begin = int(samplerate/4500 - 2)
    ceps = ceps[ignore_begin:]


    for under_zero in ceps:
        if under_zero < 0:
            under_zero = np.where(ceps == under_zero)[0][0]
            break


    print "under_zero = " + str(under_zero+ignore_begin)


    ceps = ceps[under_zero:int(samplerate/27.5 + 2)]
    hertz = str(samplerate/float(ceps.argmax() + under_zero + ignore_begin))
    print "hertz = " + hertz
    print "sec = %3.3f ~ %3.3f" % (float((binsize*i*(1-overlapFac)))/float((samplerate)), float((binsize*(i+1)*(1-overlapFac)))/float((samplerate)))
    print "val = " + str(ceps.max())
    print "--------------------"

    fund_freq_list.append({'s_num': i, 'hertz': float(hertz)})

Tags: npfloat频率ignoresamplesprintbeginunder