1
$\begingroup$

In MATLAB if I compute the cross-correlation of 2 arbitrary signals using the xcorr function I get exactly the answer I expect. If I then calculate the cross-correlation using element-wise fft multiplication, the resulting signal is incorrect (offset) by 1 element. Can anyone tell me what I'm missing? Here is my MATLAB script:

a = [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0];
b = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
elems = length(a);

% Calculate the cross-correlation coefficients using built-in function
[ac_coeffs, lags] = xcorr(a, b);
subplot(2, 1, 1);
plot(lags, ac_coeffs);

% Now do it manually by fft multiplication
a = [a, zeros(1, elems - 1)];
b = [b, zeros(1, elems - 1)];
af = fft(a);
bf = fft(b);
mult = af.* conj(bf);
result = ifft(mult);
subplot(2,1,2);
plot(result);
$\endgroup$
2
$\begingroup$

You're missing an fftshift to get the correct alignment:

plot(lags,fftshift(result));
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.