## Definition of Matlab Autocorrelation

In Matlab, Autocorrelation function means a correlation between numbers in a set or series with other numbers in the same set or series separated by provided time interval. Autocorrelation is also called a serial correlation because correlated numbers with a delayed copy of itself set or series. Autocorrelation is used in signal processing for analyzing a series of values like time-domain signals. Autocorrelation means the correlation between the observation at the current time spot and the observation at previous time spots. Autocorrelation used to determine the terms used in the MA model. Autocorrelation used to measure the relation between the elements’ current value and past values of the same element.

**Syntax:**

`autocorr(x)`

[acf,lags,] = autocorr(___)

### How Autocorrelation Function works in Matlab?

Autocorrelation used to measure the relation between elements’ current value and past values of the same element. There are the following steps of autocorrelation function to works in Matlab: –

**Step 1: **Load and read all the data from the file.

**Step 2: **Assign all data to a variable.

**Step 3: **Then use the appropriate syntax of the ‘Matlab Autocorrelation’ function.

**Step 4:** then execute the code.

### Examples of Matlab Autocorrelation

Lets us discuss the examples of Matlab Autocorrelation.

#### Example #1

In this example, we calculate the autocorrelation of random Gaussian noise in Matlab. We know that autocorrelation means matching of signals with the delayed version itself. Now for random Gaussian noise, only when shift= 0 there is some value of autocorrelation and for all other cases, the autocorrelation result will be zero. Now first we will generate random Gaussian noise in Matlab. For generating random Gaussian noise, we will use randn function in Matlab. “x= randn(1, length(t))” generate length t Gaussian sequence with mean 0 and variance 1. After that we use subplot and plot function to plot the random Gaussian noise signal. Here we will use the Matlab autocorrelation function to calculate the autocorrelation of random Gaussian noise in Matlab.“autocorr(x)” this syntax is used for calculating the autocorrelation of random Gaussian noise. Then subplot and plot function is used for plotting autocorrelation of random Gaussian noise. To calculate the autocorrelation of a random Gaussian signal execute the Matlab code.

**Code : **

`clc;`

clear all;

close all;

t= 0:1000;

x= randn(1,length(t));

subplot(2,1,1)

plot(t,x)

subplot(2,1,2)

autocorr(x)

4.5 (7,731 ratings)

View Course

**Output: **

** **

#### Example #2

In this example, we can see that how we can find the periodicity of the signal using the Matlab autocorrelation function. So let first we load the data. Here we use office temperature for data. This is by default available in Matlab. Ones we load the data we plot the data. We use a plot function to plot the data. After the plotting data, we will found the temperature is in oscillating behavior. So we take the normal temperature by using mean temperature. “normal_temp= temp -mean(temp)” ones we subtract mean temperature from temperature we get the normal temperature. After that, we will plot the normal temperature using the plot function. so we get normal temperature is varying around the zero. Now we will set sampling ‘fs’ as 24. Then we are going to create time vector t. the t will start from 0 and it will go up to the length of normal temperature. Then we use Matlab autocorrelation to find the periodicity of the signal. Then we use above syntax “[autocor, lags]= xcorr (normal_temp,3*7*fs,’coeff’)”. Here ‘autocor’ variable store autocorrelation matrix and ‘lags’ this variable stores the lags between the data. ‘xcorr’ correlate between normal temperature and sampling frequency. Then we plot the data that is lags/fs and autocor it plot the autocorrelation of the signal.

**Code: **

`clc;`

clear all;

close all;

load officetemp;

plot(temp)

normal_temp= temp -mean(temp);

mean(normal_temp)

subplot(2,1,1);

plot(normal_temp)

shg

fs= 24;

t = (0:length(normal_temp)-1)/fs;

plot(t,normal_temp);

xlabel='Time in days';

ylabel='Temprature';

axis tight;

shg

[autocor, lags]= xcorr(normal_temp,3*7*fs,'coeff');

subplot(2,1,2);

plot(lags/fs,autocor);

**Output:**

After executing the code we get autocorrelation of the input signal. If we see in figure 2 we get that the input signal is periodic.

#### Example #3

In this example, we calculate the autocorrelation of the input sine signal. Now we load the signal in variable ‘x’. For getting sine signal in Matlab“x= sin(2*t)” is used. After that, we use the subplot and plot function to plot the sine signal. Here we will use the Matlab autocorrelation function to calculating autocorrelation of random Gaussian noise in Matlab.“autocorr(x)” this syntax is used for calculating the autocorrelation of sine signal. Then subplot and plot function is used for plotting autocorrelation of sine signal.

**Code:**

`clc;`

clear all;

close all;

t= 0:100;

x= sin(2*t);

subplot(2,1,1)

plot(t,x)

subplot(2,1,2)

autocorr(x)

**Output: **

### Conclusion

In this article, we saw the concept of Matlab autocorrelation. Basically, this function is used to measure the relation between elements’ current value and past values of the same element. Then we saw how we can find the periodicity of the signal using this function and efficient work of the Matlab autocorrelation function.

### Recommended Articles

This is a guide to Matlab Autocorrelation. Here we discuss the definition, How Autocorrelation Function works in Matlab along with code implementation. You may also have a look at the following articles to learn more –