function out = correl_fsp(d, l, mu, sigma)
% Function to evaluate the spatial correlation of two points in space 
% Tim Brown 5/3/02. Tidied up 26/9/02. Modified 6/6/08 and January 2012

% Read in angular data from file.
fdir = fopen('ang.dat','rt');
data = fscanf(fdir,'%f',[2 6498])';
fclose(fdir);

delta_theta = atan(l/d);
dh = sqrt((d^2)+(l^2));

% Calculate angles and weighting in the case of measurements
ang = data(:,1);
angp = data(:,2);
wp = complex(cos(dh.*2.*pi.*sign(angp+0.00001).*sqrt(((sin(ang.*pi/180).^2).*(sin(angp.*pi./180).^2).*(cos(delta_theta).^2)) ... 
           + ((cos(ang.*pi./180).^2).*(sin(delta_theta).^2)))), ...
           sin(dh.*2.*pi.*sign(angp+0.00001).*sqrt(((sin(ang.*pi/180).^2).*(sin(angp.*pi./180).^2).*(cos(delta_theta).^2)) ... 
           + ((cos(ang.*pi./180).^2).*(sin(delta_theta).^2)))));

% Calculate the weighting factors and XPR
A = 1 ./ sum(ptheta(abs(ang),mu,sigma) .* sin(abs(ang) .* pi ./ 180));

% Calculate correlation components for integration.
out = sum(wp .* A .* ptheta(abs(ang),mu,sigma) .* sin(abs(ang) .* pi ./ 180));

