Auto-correlation, also called series correlation, is the correlation of a given sequence with itself as a function of time lag. Cross-correlation is a more generic term, which gives the correlation between two different sequences as a function of time lag.
Given two sequences and , the cross-correlation at times separated by lag i is given by ( denotes complex conjugate operation)
Auto-correlation is a special case of cross-correlation, where x=y. One can use a brute force method (using for loops implementing the above equation) to compute the auto-correlation sequence. However, other alternatives are also at your disposal.
Method 1: Auto-correlation using xcorr function
Matlab
For a N-dimensional given vector x, the Matlab command xcorr(x,x) or simply xcorr(x) gives the auto-correlation sequence. For the input sequence x=[1,2,3,4], the command xcorr(x) gives the following result.
>> x=[1 2 3 4] >> acf=xcorr(x) acf= 4 11 20 30 20 11 4
Python
In Python, autocorrelation of 1-D sequence can be obtained using numpy.correlate function. Set the parameter mode=’full’ which is useful for calculating the autocorrelation as a function of lag.
import numpy as np x = np.asarray([1,2,3,4]) np.correlate(x, x,mode='full') # output: array([ 4, 11, 20, 30, 20, 11, 4])
Method 2: Auto-correlation using Convolution
Auto-correlation sequence can be computed as the convolution between the given sequence and the reversed (flipped) version of the conjugate of the sequence.The conjugate operation is not needed if the input sequence is real.
Matlab
>> x=[1 2 3 4] >> acf=conv(x,fliplr(conj(x))) acf= 4 11 20 30 20 11 4
Python
import numpy as np x = np.asarray([1,2,3,4]) np.convolve(x,np.conj(x)[::-1]) # output: array([ 4, 11, 20, 30, 20, 11, 4])
Method 3: Autocorrelation using Toeplitz matrix
Autocorrelation sequence can be found using Toeplitz matrices. An example for using Toeplitz matrix structure for computing convolution is given here. The same technique is extended here, where one signal is set as input sequence and the other is just the flipped version of its conjugate. The conjugate operation is not needed if the input sequence is real.
Matlab
>> x=[1 2 3 4] >> acf=toeplitz([x zeros(1,length(x)-1)],[x(1) zeros(1,length(conj(x))-1)])*fliplr(x).' acf= 4 11 20 30 20 11 4
Python
import numpy as np from scipy.linalg import toeplitz x = np.asarray([1,2,3,4]) toeplitz(np.pad(x, (0,len(x)-1),mode='constant'),np.pad([x[0]], (0,len(x)-1),mode='constant'))@x[::-1] # output: array([ 4, 11, 20, 30, 20, 11, 4])
Method 4: Auto-correlation using FFT/IFFT
Auto-correlation sequence can be found using FFT/IFFT pairs. An example for using FFT/IFFT for computing convolution is given here. The same technique is extended here, where one signal is set as input sequence and the other is just the flipped version of its conjugate.The conjugate operation is not needed if the input sequence is real.
Matlab
>> x=[1 2 3 4] >> L = 2*length(x)-1 >> acf=ifft(fft(x,L).*fft(fliplr(conj(x)),L)) acf= 4 11 20 30 20 11 4
Python
import numpy as np from scipy.fftpack import fft,ifft x = np.asarray([1,2,3,4]) L = 2*len(x) - 1 ifft(fft(x,L)*fft(np.conj(x)[::-1],L)) #output: array([ 4+0j, 11+0j, 20+0j, 30+0j, 20+0j, 11+0j, 4+0j])
Note that in all the above cases, due to the symmetry property of auto-correlation function, the center element represents .
Construction the Auto-correlation Matrix
Auto-correlation matrix is a special form of matrix constructed from auto-correlation sequence. It takes the following form.
The auto-correlation matrix is easily constructed, once the auto-correlation sequence is known. The auto-correlation matrix is a Hermitian matrix as well as a Toeplitz matrix. This property is exploited in the following code for constructing the Auto-Correlation matrix.
Matlab
>> x=[1+j 2+j 3-j] %x is complex >> acf=conv(x,fliplr(conj(x)))% %using Method 2 to compute Auto-correlation sequence >>Rxx=acf(3:end); % R_xx(0) is the center element >>Rx=toeplitz(Rxx,[Rxx(1) conj(Rxx(2:end))])
Python
import numpy as np x = np.asarray([1+1j,2+1j,3-1j]) #x is complex acf = np.convolve(x,np.conj(x)[::-1]) # using Method 2 to compute Auto-correlation sequence Rxx=acf[2:]; # R_xx(0) is the center element Rx = toeplitz(Rxx,np.hstack((Rxx[0], np.conj(Rxx[1:]))))
Result:
Rate this article:
References:
[1] Reddi.S.S,”Eigen Vector properties of Toeplitz matrices and their application to spectral analysis of time series, Signal Processing, Vol 7,North-Holland, 1984,pp 46-56.↗
[2] Robert M. Gray,”Toeplitz and circulant matrices – an overview”,Department of Electrical Engineering,Stanford University,Stanford 94305,USA.↗
[3] Matlab documentation help on Toeplitz command.↗
Why does you definition is different from Wikipedia?
https://en.wikipedia.org/wiki/Autocorrelation#Auto-correlation_of_discrete-time_signal
https://en.wikipedia.org/wiki/Cross-correlation#Cross-correlation_of_deterministic_signals
Wikipedia says:
R_{{xy}_i}=Corr(x,y)_i = sum_{j=-infty}^{infty} x^*_j y_{j+i}
where we take the conjugate of x instead of y
The definition of correlation above is not unique and sometimes correlation may be defined differently
For example, note these two alternative definitions:
https://uploads.disquscdn.com/images/2a1ea840390a73025c75f28182d206425eff8e60a5c0a9193bbb1029cab3d5bb.png
They are related to each other by
https://uploads.disquscdn.com/images/b6a3b10804a277e931d18f1b90f918a4bd5e5d314129f8bdcade45e2da142d72.png
Hello,
Is there any Example you considered for constructing AR(1) Correlation Matrix?