Cross-correlation in matlab without using the inbuilt function?

后端 未结 3 2020
暗喜
暗喜 2020-12-06 02:36

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 corr

3条回答
  •  星月不相逢
    2020-12-06 03:40

    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.

提交回复
热议问题