function saMat = SSDB_correlationsleftanddown(X,options);
%
%function saMat = SSDB_correlationsleftanddown(X,options);

 d.RangeParameter = 25;
 d.nLevels = 5;
 d.imSize = [128 128];
 d.FinalPoolSize = 12;
 d.UseLinearVersionOfParallelism = 0;
 if(not(exist('options')))
   options = [];
 end
 options = ResolveMissingOptions(options,d);
 range = generaterange(1:options.RangeParameter);
 nEx = size(X,2);
 for i = 1:nEx
   fprintf('SSDB_correlationsleftanddown image %d of %d\r',i,nEx);
  stim = reshape(X(:,i),options.imSize);
  stim = fliplr(stim);
   stim = histeq(stim/256);
   M = correlationsrightanddown(stim,range,options.nLevels,options);
   if(i == 1);
     saMat.X = zeros(length(M(:)),nEx);
   end
   saMat.X(:,i) = M(:);
 end
fprintf('SSDB_correlationsleftanddown image %d of %d\n',i,nEx);

function Z = correlationsrightanddown(I,range,patchsize,options);
% function M = correlationsrightanddown(I,range,patchsize);
 numranges = size(range,1);
 M = zeros(size(I,1),size(I,2),numranges);
 for i = 1:numranges,
    leftout = size(I)-range(i,:);
    SHIFTI = [zeros(size(I,1),range(i,2)),[zeros(range(i,1),leftout(2)); I(1:leftout(1),1:leftout(2))]];
    M(:,:,i) = conv2(ones(patchsize,1),ones(1,patchsize),(I-SHIFTI).^2,'same');
 end
 if(options.UseLinearVersionOfParallelism)
   M = sum(M,3);
 else
   M = (max(M,[],3)./(min(M,[],3)+.5));
 end
 Mstat = zeros(size(M,1), size(M,2), 4);
 Mstat(:,:,1) = -maxfilter(-M,options.FinalPoolSize);
 Mstat(:,:,2) = sumfilter(M,options.FinalPoolSize)/625;
 Mstat(:,:,3) = modefilter(M,options.FinalPoolSize);
 Mstat(:,:,4) = maxfilter(M,options.FinalPoolSize);
 Z = Mstat(1:8:end, 1:8:end,:);
 
function range = generaterange(range)

mrange = ones(length(range),1)*(range(:)');
mrange2 = mrange';
range = [mrange(:), mrange2(:)];

