Using xcorr in pitch detections

I'm currently switching from fft to this function and I found some questions. What I have known is that we should calculate the frequency through comparing the positons of adjacent local maxima, but I don't know how I should use my maxima. I'm testing things with a 120Hz sine wave:
t=(0:999)*(1/1000); x = sin(2*pi*120*t); R = xcorr(x); [pks,locs]=findpeaks( R );
the locs have differences of 8 or 9, which is still correct ( 1/(9/1000) < 120 < 1/(8/1000) )
But I don't know how to get 120 as my answer using effective codes.
How much maxima should I count into the average value, etc?
And I don't think my code works with mixed frequencies... Can someone give me a direction?

 Accepted Answer

Pitch detection is a huge field with many different algorithms each with advantages and disadvantages. How close to 120 do you need to get. What i your answer is 121, 120.1, 120.01, ...
One easy way to get around the problem is to increase your sampling rate.
Fs = 1e6;
t=(0:(Fs-1))*(1/Fs);
x = sin(2*pi*120*t);
R = xcorr(x);
[pks,locs]=findpeaks(R);
[min(Fs./diff(locs)), mean(Fs./diff(locs)), max(Fs./diff(locs))]
ans =
119.9904 120.0000 120.0048

6 Comments

Have you ever worked the ACF?
I have a speech signal,I wrote the following codes,
ND=100
Nframes = fix(LDx/ND);
for M=1 : Nframes ;
xframe=x(1+(M-1)*ND*10 : M*ND*10);
figure(1); plot (abs(xframe)); grid on;
xlabel('sample number'); ylabel('Voltage'); axis([0,ND*2,-1,1]);
title(sprintf('Frame number: %d',M));
ms=fix(fs/500); % maximum speech Fx at 500Hz
ms20=fs/50 ; % minimum speech Fx at 50Hz
r=abs(xcorr(xframe,ms20,'biased'));
[pks,locs]=findpeaks(r);
dlocs=(-ms20:ms20)/fs; % delay value
%figure(2); plot (dlocs,r); grid on;?
figure(2); plot (dlocs,r); grid on;
xlabel('Delay(s)'); ylabel('autocorrelation');
title(sprintf('Autocorrelation against Lag: %d',M));
I dont get the last part of your program,can you explain more?
pause
end
What is the lag in your autocorrelation formula?
I don't use the "lag" in my autocorrelation formula.
I estimate the pitch by comparing the "time" between peaks in the autocorrelation function. the second to last line finds the peaks and the last line takes the difference in "time" between the peaks.
Does this formula usful for the each frame in the speech?How should I plot ?which of the above numbers are the pitch?
That is a lot of questions. Why don't you try and implement something with this code and see where you get stuck. Also if the answer is helpful, consider voting for it.

Sign in to comment.

More Answers (0)

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!