# Correlation using fft and xcorr and plot both answers (Matlab) [closed]

I am trying to calculate autocorrelation using fft and xcorr and plot the grafhic with both cases.

autocorr_a=xcorr(signal_1,signal_1);
N1=length(signal_1);
autocorr_b=ifft( fft(signal_1,N1) .* conj(fft(signal_1,N1)));
x1=[0:(2*length(signal_1)-2)]'/(2*length(signal_1)-1);
plot(x1, autocorr_a, x1, autocorr_b)


What is wrong?

• Welcome to DSP.SE! Why are you asking "What is wrong?" there doesn't appear to be any information in your question as to what you think is wrong. Please edit your question to add this information.
– Peter K.
Nov 17 '15 at 13:15

Matlabs xcorr() command performs linear correlation, whereas the FFT method you employ performs circular correlation, so there is time-aliasing in the same way as with frequency domain convolution.

You can simulate linear convolution in the frequency domain by sufficiently zero-padding your signal with N-1 zeros, where N is the length of your signal vector, before performing the FFT.