www.pudn.com > pitchdetect.rar > pitchdetect.m


waveFile='清华大学资讯系.wav ';  
[y, fs, nbits] = wavread(waveFile);  
N=length(y); 
time=(1:length(y))/fs;  
frameSize=floor(40*fs/1000); % 帧长 
startIndex=round(15000); % 起始序号 
endIndex=startIndex+frameSize-1; % 结束序号  
frame = y(startIndex:endIndex); % 取出该帧  
 
frameSize=length(frame); 
frame2=frame.*hamming(length(frame)); % 加 hamming window  
rwy= rceps(frame2); % 求倒谱  
ylen=length(rwy);  
cepstrum=rwy(1:ylen/2);  
 
%基音检测  
LF=floor(fs/500); 
HF=floor(fs/70); 
cn=cepstrum(LF:HF); 
[mx_cep ind]=max(cn); 
if mx_cep>0.08&ind>LF  
a= fs/(LF+ind); 
else  
a=0;  
end  
pitch=a  
 
% 画图 
figure(1);  
plot(time, y); title(waveFile); axis tight  
ylim=get(gca, 'ylim');  
line([time(startIndex),time(startIndex)],ylim,'color','r'); 
line([time(endIndex), time(endIndex)],ylim,'color','r'); 
figure(2);  
subplot(2,1,1);  
plot(frame);  
title('Frame');  
subplot(2,1,2);  
plot(cepstrum);  
title('cepstrum');