# autocorr2 documentation

The autocorr2 function calculates the radially averaged autocorrelation (also known as pair correlation or two-point correlation of the pixels of an image as a function of distance. The function is really fast because it uses the Fourier transform for the convolution of the original image. The code also corrects for the finite image size: images close to the edge have fewer neighbors than particles near the center.

## Syntax

[S2,d] = autocorr2(I)

S2 = autocorr2(I,d)

## Description

[S2,d] = autocorr2(I) Computes the radially averaged autocorrelation S2 also known as pair correlation or two-point correlation) of the pixels of an image I as a function of distance d.

S2 = autocorr2(I,d) Explicitly determine the distance vector d. The maximum element of d should not be larger than 0.3*max(height,width).

## Example

The main application of this algorithm is to characterize microstructures. To demonstrate this, let's create a simple synthetic microstructure based on some circles of different diameter, and then calculate its radially averaged autocorrelation.

```% Create a logical image with circles embedded in it
% First create the image
imageSizeX = 640;
imageSizeY = 480;
I = zeros(imageSizeY,imageSizeX);
[columnsInImage, rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Define the number of circles
N = 200;
% Define the circle parameters in the image.
centersX = imageSizeX.*rand(N,1);
centersY = imageSizeY.*rand(N,1);
% Place the circles
for ii = 1:N
aux = (rowsInImage - centersY(ii)).^2 ...
+ (columnsInImage - centersX(ii)).^2 <= radius(ii).^2;
I = I + aux;
end
% Convert back to logical
I = logical(I);
```

Now compute the autocorrelation and display it along with the original image.

```[S2,d] = autocorr2(I);
% Display the images and the two-point correlation function
h = figure;
set(h, 'Position',[159.4000 203.4000 1.1824e+03 370.4000]);
subplot(1,2,1)
imshow(I) ;
title('Binary image');
subplot(1,2,2)
plot(d,S2,'r','LineWidth',2)
title('Two-point correlation function');
xlabel('Pixel number'), ylabel('Correlation function S_{2}')
ylim([0,1])
``` 