function [decorr, dist]=DecorrelationLength(x,y,data, dist); % % EXAMPLE: % load DATA % x=lon_d; y=lat_d; % dist=0:0.2:10; % the x-axis of the autocorrelation function % [decorr, dist]=DecorrelationLength(x,y,data,dist); % plot(dist, decorr, 'linewidth',2); grid on d=data; x1=x;y1=y; i=length(x(:)); j=length(x1(:)); X=M2d(x,j); Y=M2d(y,j); X1=M2d(x1,i)'; Y1=M2d(y1,i)'; DIST = sqrt( (X-X1).*(X-X1) + (Y-Y1).*(Y-Y1)) ; CDD=d*d'; L=size(DIST,1); % Now interpolate the covariances onto regularly spaced distances for i=1:L; % Sort the values based on distance tmp=DIST(:,i); tmp(:,2)=CDD(:,i); tmp=sortrows(tmp,1); % Remove duplicate values when distances are the same in = find (tmp(2:end,1)-tmp(1:end-1,1) ~=0); tmp=tmp(in,:); % interpolate onto linearly spaced distances % DECORR will hold all the realizations of DECORR DECORR(:,i)=interp1(tmp(:,1), tmp(:,2), dist, 'linear'); end % Compute spatial autocorrelation function by averaging over % all realizations and divide by the variance of d to get correlation. decorr=mean(DECORR,2)/var(d); return % Utility function. function X=M2d(x,J) I=length(x(:)); X=zeros(I,1); X(:)=x(:); X=repmat(X,[1 J]);