倒谱法基频

2024-04-29 12:06:32 发布

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

我试图通过倒谱法找到频率。对于我的测试,我得到了以下文件http://www.mediacollege.com/audio/tone/files/440Hz_44100Hz_16bit_05sec.wav,一个频率为440Hz的音频信号。

我应用了以下公式:

倒谱=IFFT(对数FFT(s))

我得到256块,但我的结果总是错误的。。。

from numpy.fft import fft, ifft
import math
import wave
import numpy as np
from scipy.signal import hamming  

index1=15000;
frameSize=256;
spf = wave.open('440.wav','r');
fs = spf.getframerate();
signal = spf.readframes(-1);
signal = np.fromstring(signal, 'Int16');
index2=index1+frameSize-1;
frames=signal[index1:int(index2)+1]

zeroPaddedFrameSize=16*frameSize;

frames2=frames*hamming(len(frames));   
frameSize=len(frames);

if (zeroPaddedFrameSize>frameSize):
    zrs= np.zeros(zeroPaddedFrameSize-frameSize);
    frames2=np.concatenate((frames2, zrs), axis=0)

fftResult=np.log(abs(fft(frames2)));
ceps=ifft(fftResult);

posmax = ceps.argmax();

result = fs/zeroPaddedFrameSize*(posmax-1)

print result

对于这种情况,如何得到结果=440?

**

UPDATE:

**

我在matlab中重写了我的源代码,现在一切都正常了,我用440Hz和250Hz的频率做了测试。。。

对于440Hz,我得到441Hz不错

对于250Hz,我得到249.1525Hz接近结果

我做了一个简单的方法,让峰值进入倒谱值。

我想我可以找到更好的结果使用四分插值找到最大值!

我正在绘制440Hz的估计结果

enter image description here

共享用于倒谱频率估计的源:

%% ederwander Cepstral Frequency (Matlab)
waveFile='440.wav';
[y, fs, nbits]=wavread(waveFile);

subplot(4,2,1); plot(y); legend('Original signal');

startIndex=15000;
frameSize=4096;
endIndex=startIndex+frameSize-1;
frame = y(startIndex:endIndex);

subplot(4,2,2); plot(frame); legend('4096 CHUNK signal');

%make hamming window
win = hamming(length(frame));


%samples multplied by hamming window
windowedSignal = frame.*win;


fftResult=log(abs(fft(windowedSignal)));
subplot(4,2,3); plot(fftResult); legend('FFT signal');

ceps=ifft(fftResult);

subplot(4,2,4); plot(ceps); legend('ceps signal');

nceps=length(ceps)

%find the peaks in ceps

peaks = zeros(nceps,1);

k=3;

while(k <= nceps - 1)
   y1 = ceps(k - 1);
   y2 = ceps(k);
   y3 = ceps(k + 1);
   if (y2 > y1 && y2 >= y3)
      peaks(k)=ceps(k);
   end
k=k+1;
end

subplot(4,2,5); plot(peaks); legend('PEAKS');

%get the maximum ...
[maxivalue, maxi]=max(peaks)



result = fs/(maxi+1)


subplot(4,2,6); plot(result); %legend('Frequency is' result);

legend(sprintf('Final Result Frequency =====>>> (%8.3f)',result)) 

Tags: importfftsignalplotnpresultfs频率
3条回答

如果采样率是44.1khz,256可能太小而不能做任何有用的事情。在这种情况下,FFT的分辨率为44100/256=172hz。如果你想要10赫兹的分辨率,那么你可以使用4096的FFT大小。

我也遇到了类似的问题,因此我重用了您的代码的一部分,并通过对同一帧执行连续的求值,然后从

我得到了一致的结果。

def fondamentals(frames0, samplerate):
    mid = 16
    sample = mid*2+1
    res = []
    for first in xrange(sample):
        last = first-sample
        frames = frames0[first:last]
        res.append(_fondamentals(frames, samplerate))
    res = sorted(res)
    return res[mid] # We use the medium value

def _fondamentals(frames, samplerate):    
    frames2=frames*hamming(len(frames));
    frameSize=len(frames);
    ceps=ifft(np.log(np.abs(fft(frames2))))
    nceps=ceps.shape[-1]*2/3
    peaks = []
    k=3
    while(k < nceps - 1):
        y1 = (ceps[k - 1])
        y2 = (ceps[k])
        y3 = (ceps[k + 1])
        if (y2 > y1 and y2 >= y3): peaks.append([float(samplerate)/(k+2),abs(y2), k, nceps])
        k=k+1
    maxi=max(peaks, key=lambda x: x[1])
    return maxi[0]

倒谱法对高次谐波的信号效果最好,而对接近纯正弦波的信号效果不好。

最佳测试信号可能更像是时域中的重复、非常接近等间隔的脉冲(每个FFT窗口的脉冲越多越好),它应该在频域中产生接近重复等间隔的峰值,该峰值应该显示为倒谱的激励部分。脉冲响应将在倒谱的低共振峰部分表示。

相关问题 更多 >