Auto and Cross Correlation in MATLAB: Implementation and Comparison

matlab
correlation
signal processing
auto correlation
cross correlation

This document explains auto and cross correlation in MATLAB, demonstrating how to implement them with and without using the built-in xcorr function. Correlation helps determine the similarity between two signals or vectors in terms of phase and magnitude.

There are two main types: auto correlation and cross correlation. Correlation reaches its maximum when two signals are highly similar.

Correlation is mathematically equivalent to multiplying the complex conjugate of the frequency spectrum of one signal by the frequency spectrum of the other. In auto correlation, a signal is correlated with itself or a shifted version of itself. Cross correlation, on the other hand, involves correlating two different time series signals.

The example provided below focuses on cross correlation. To perform auto correlation, you can use the same vector for both in1 and in2 (or append zeros to one of them initially).

Mathematically, the correlation output z[n]z[n] is given by:

z[n]=X[k]Y[n+k]z[n] = \sum X[k] * Y[n+k]

Setting Up Input Parameters

First, let’s define some sample input vectors:

in1=[0 1 2 3 4 5 6 7 8 9];
in2=[9 11 12 13 4 5 16 17 18 19];

Cross Correlation MATLAB Code Without Using the xcorr Function

The following MATLAB script demonstrates how to compute cross correlation without using the built-in xcorr function:

if length(in1)>length(in2)
    pad = length(in1)-length(in2);
    in2 = [in2 zeros(1,pad)];
elseif length(in1)<length(in2)
    pad = length(in2)-length(in1);
    in1 = [in1 zeros(1,pad)];
end

out_len = length(in1);
out = zeros(1,out_len);
tmp = in2;

for k = 1:out_len
    out(k) = in1*tmp';
    tmp = [0 tmp(1:end-1)];
end

figure;
plot(out);
title('Our code CORRELATION OUTPUT');

[m,n]=max(out);  % max value in the correlation and its index

This code first ensures that the input vectors have the same length by padding the shorter vector with zeros. Then, it iterates through the vectors, calculating the correlation at each shift and storing the result in the out vector. Finally, it plots the resulting correlation and finds the maximum value and its index.

Cross Correlation MATLAB Code Using xcorr

Here’s how to perform cross correlation using MATLAB’s built-in xcorr function:

y=xcorr(in1,in2); % matlab built in function
len=length(y);
index=len/2;
z=y(index:1:end); %extacting one side of the result

figure;
plot(z);
title('MATLAB xcorr function OUTPUT');

[m1,n1]=max(z); % max value in the correlation and its index

This code is much simpler, as it directly uses the xcorr function. It then extracts one side of the result (due to the symmetry of the output of xcorr) for plotting and finds the maximum value and its index.

OUTPUT Plots of Cross Correlation in MATLAB

Correlation matlab output

xcorr matlab function output

AWGN Impairment Simulation in MATLAB

AWGN Impairment Simulation in MATLAB

MATLAB code demonstrating Additive White Gaussian Noise (AWGN) impairment and its effect on constellation diagrams with BPSK, QPSK, 16QAM, and 64QAM modulation.

awgn
matlab
impairment