Possible to vectorize xcorr?

조회 수: 1 (최근 30일)
Sebastian Holmqvist
Sebastian Holmqvist 2013년 3월 21일
Greetings.
Currently my workstation can't keep up when I attempt to calculate the cross correlation for very long signals.
This is a MWE of the code:
x = rand(1,200e3);
for i=500:-1:1
y = i.*x;
C(i,:) = xcorr(x, y, 500);
end
The 500 calls to xcorr seems to kill it. Any way to improve this by vectorizing or otherwise optimize it? Currently it takes >60 sec which isn't practical.
I'm only interested in a small number of lags (around ±500). So I attempted to do a classic implementation of the cross correlation instead of the Fourier implementation in xcorr. My end result wasn't an improvement, but maybe you guys/gals can do one better?
Thanks for any ideas!
  댓글 수: 1
Daniel Shub
Daniel Shub 2013년 4월 3일
I may be missing something, but x and y are scaled versions of each other. You just need to compute the autocorrelation of x once and then set the normalisation factor in the loop.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sebastian Holmqvist
Sebastian Holmqvist 2013년 4월 3일
After some time, I realized that x is the same throughout all calculations, which means that I can pre-calculate both it's FFT and it's conjugate before the loop.
This is what I ended up with:
x = rand(1,200e3);
corr_len = 2^nextpow2(2*length(x)-1);
x_fft = fft(x, corr_len);
x_conj = conj(x_fft);
for i=500:-1:1
y = i.*x;
corr = ifft(x_conj.*y);
C(i,:) = [corr(end-500/2:end) corr(1:500/2)];
end

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by