www.pudn.com > audioProcessingtoolbox.rar > acf2pitch.m
function [pitch, clarity, pitchIndex]=acf2pitch(acf, PP, plotOpt)
% acf2pitch: ACF to pitch conversion
% Usage: [pitch, clarity, pitchIndex]=acf2pitch(acf, PP, plotOpt)
% Roger Jang, 20070211
if nargin<1, selfdemo; return; end
if nargin<3, plotOpt=0; end
% ===== Find all local max
lmaxIndex1=find(localmax(acf));
lmaxValue1=acf(lmaxIndex1);
% ====== Within the right range
minIndex=PP.fs/pitch2freq(PP.maxPitch);
maxIndex=PP.fs/pitch2freq(PP.minPitch);
index=find(minIndex<=lmaxIndex1 & lmaxIndex1<=maxIndex);
if isempty(index), pitch=0; clarity=0; pitchIndex=[]; return; end
lmaxValue2=lmaxValue1(index);
lmaxIndex2=lmaxIndex1(index);
% ====== Find the max value
[maxValue, localIndex]=max(lmaxValue2);
pitchIndex=lmaxIndex2(localIndex);
pitch=freq2pitch(PP.fs/(pitchIndex-1));
clarity=maxValue/acf(1);
if plotOpt
plot(1:length(acf), acf);
set(gca, 'xlim', [-inf inf]);
axisLimit=axis;
line(minIndex*[1 1], axisLimit(3:4), 'color', 'm');
line(maxIndex*[1 1], axisLimit(3:4), 'color', 'k');
line(lmaxIndex1, lmaxValue1, 'color', 'g', 'linestyle', 'none', 'marker', 'o');
line(lmaxIndex2, lmaxValue2, 'color', 'b', 'linestyle', 'none', 'marker', '^');
line(pitchIndex, maxValue, 'color', 'r', 'linestyle', 'none', 'marker', 'square');
legend('ACF', 'Lower bound', 'Upper bound', 'All local max', 'Within-bound local max', 'Selected');
end
% ====== Self demo
function selfdemo
waveFile='greenOil.wav';
[y, fs, nbits]=wavReadInt(waveFile);
framedY=buffer2(y, 256, 0);
plotOpt=1;
PP=ptParamSet(fs, nbits);
frame=framedY(:, 34);
acf=frame2acf(frame);
subplot(2,1,1); [pitch, clarity, ppcIndex]=feval(mfilename, acf, PP, plotOpt);
frame=framedY(:, 250);
acf=frame2acf(frame);
subplot(2,1,2); [pitch, clarity, ppcIndex]=feval(mfilename, acf, PP, plotOpt);