8

can someone tell how to do the cross-correlation of two speech signals (each of 40,000 samples) in MATLAB without using the built-in function xcorr and the correlation coefficient?

Thanks in advance.

3

3 Answers 3

31

You can do cross-correlations using fft. The cross-correlation of two vectors is simply the product of their respective Fourier transforms, with one of the transforms conjugated.

Example:

a=rand(5,1);
b=rand(5,1);
corrLength=length(a)+length(b)-1;

c=fftshift(ifft(fft(a,corrLength).*conj(fft(b,corrLength))));

Compare results:

c =

    0.3311
    0.5992
    1.1320
    1.5853
    1.5848
    1.1745
    0.8500
    0.4727
    0.0915

>> xcorr(a,b)

ans =

    0.3311
    0.5992
    1.1320
    1.5853
    1.5848
    1.1745
    0.8500
    0.4727
    0.0915
Sign up to request clarification or add additional context in comments.

6 Comments

I cant use any matlab inbuilt functions. Can you suggest me something else?
You should make that clear in your question. You merely disallowed xcorr initially. At what point do you consider it ok to use inbuilt functions? Is 2+2 allowed? How about plus(2,2)? You should explain why you have these restrictions
I apologize for being vague. Since I am doing the cross-correlation between two signals each 40,000 samples this creates some delay in the real-time application for processing the entire 40,000 samples So, I am looking for something else like processing block of samples rather than processing all the samples at a time. I trying to do convolution but could't do it.
Well, all you need to do is break up your data into smaller blocks of data and use the above. You don't need a different technique. For large sample sizes, FFT approach will be much faster than xcorr. If I do the above with 40,000 samples for both a and b, on my machine, it takes about 0.7 seconds. And if I simply halve it (i.e., 20,000) samples, it takes merely 0.04 seconds and is near instant at 0.006 seconds for 10,000 samples.
@yoda: instead of padding the signals, use the second argument of the FFT function
|
4

If there some good reason why you can't use the inbuilt, you can use a convolution instead. Cross-correlation is simply a convolution without the reversing, so to 'undo' the reversing of the correlation integral you can first apply an additional reverse to one of your signals (which will cancel out in the convolution).

Comments

4

Well yoda gave a good answer but I thought I mention this anyway just in case. Coming back to the definition of the discrete cross correlation you can compute it without using (too much) builtin Matlab functions (which should be what Matlab do with xcorr). Of course there is still room for improvment as I did not try to vectorize this:

n=1000;
x1=rand(n,1);
x2=rand(n,1);
xc=zeros(2*n-1,1);
for i=1:2*n-1
    if(i>n)
        j1=1;
        k1=2*n-i;
        j2=i-n+1;
        k2=n;
    else
        j1=n-i+1;
        k1=n;
        j2=1;
        k2=i;
    end
    xc(i)=sum(conj(x1(j1:k1)).*x2(j2:k2));
end
xc=flipud(xc);

Which match the result of the xcorr function.

UPDATE: forgot to mention that in my opinion Matlab is not the appropriate tool for doing real time cross correlation of large data sets, I would rather try it in C or other compiled languages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.