# Cross-correlation peak

How get cross-correlation peak and based on it calculate correlation score for similarity of two audio samples. SO far I've

1. FFT two samples
2. complex conjugate second
3. multiply results
4. IFFT
5. cross-correlate with itself(autocorrelate)

• You've described how to compute the cross-correlation using an FFT. I'm not sure what your question is. If you want a 'similarity score' you could try to use the correlation coefficient. Jul 2, 2013 at 18:59
• In my app I need to know are two words same. I have recorded one and need to detect second real time. And now I don't know what to do next and what I really need to compare mg, amplitude or something else. Jul 3, 2013 at 7:26
• The answers posted here are correct, but cross-correlation is not relevant to what you ultimately want to do. Jul 7, 2013 at 19:40
• so have you any suggestions pichenettes? That will be great Jul 11, 2013 at 8:23

As Matt stated, you should use the correlation coefficient!.

Points 1 to 4 calculate the crosscorrelation. From that you have to find the highest peak (or lowest, if it has a higher absolute value). This value is the value of the nominator.

The denominator consists of the two autocorrelation values. Those are obtained by using the same algorithm where both signals are the equal. Here the peak should be in the middle (t=0) as already stated by welcomedungeon. Taking the square root of both autocorrelation values and multiplying them, gives the denominator.

Edit: Maybe this description is more clear:

$\frac{max(abs(ifft(fft(x_1)*fft(x_2)')))}{sqrt(max(abs(ifft(fft(x_1)*fft(x_1)'))))*sqrt(max(abs(ifft(fft(x_2)*fft(x_2)'))))}$

The apostroph means conjugate complex.

Edit: Two examples with Matlab code:

Using the same signal:

x = rand(1000,1)-0.5;
max(abs(ifft(fft(x).*fft(x)')))/(sqrt(max(abs(ifft(fft(x).*fft(x)')))).*sqrt(max(abs(ifft(fft(x).*fft(x)')))));


gives 1;

Using a sine and a cosine, should also give 1 because they are delayed versions of each other:

x = sin([0:pi/100:10*pi]);
y = cos([0:pi/100:10*pi]);
max(abs(ifft(fft(x).*fft(y)')))/(sqrt(max(abs(ifft(fft(x).*fft(x)')))).*sqrt(max(abs(ifft(fft(y).*fft(y)')))))


gives approximately 1

Using windowing before transformation to frequency domain should improve results.

• Hi thanks for answer, but when I use correlation coefficient I don't get value in 0-1 interval. As I think I do something wrong, I calculate correlation coefficient in ffts, is it right or not? Sorry if my questions are stupid:), I'm just new in audio processing, fft stuff Jul 4, 2013 at 7:28
• Twonky can you please explain which are two autocorrelation values? one is peak and what about second? Thanks a lot!!! Jul 4, 2013 at 14:07
• the second is the peak of the second signals' autocorrelation function, see my edit … Jul 4, 2013 at 20:38
• Great!! thanks now is clear what you mean.If I'll have more questions I'll let you know. Jul 5, 2013 at 14:33
• Added two working examples. But correlation coefficient is not the best solution to find similar words in speech signals (speech recognition). To get reliable results you have to use more sophisticated methods, that are completely out of scope for a single answer. Jul 7, 2013 at 17:16

the autocorrelation of your sample should have a peak at t=0. the value of this peak should give you a good idea of a threshold value. The simplest apporoach to do keyword-searching in realtime data would be to divide incoming data into frames and keep cross correlating the required waveform against the frames and check whether the resultant has a peak higher than the estimated threshold ... However this is a rough method and suseptible to false peaks due to amplitude variations in incoming data

• Hi thanks for answer, but when I use correlation coefficient I don't get value in 0-1 interval. As I think I do something wrong, I calculate correlation coefficient in ffts, is it right or not? Jul 4, 2013 at 7:21