function [his,ofar]=dyhghn(p,step,prehis);
his=prehis/4;
count=1;
offs=0;
[r,c]=size(p);
for io=(1:step:c);
	tot=sum(p(:,io:min(io+step,c))')';
	if (offs >0)
	sum1=sum(his(2:r-offs)'.*tot(offs+1:r-1));
	sum2=sum(his(2:r-offs)'.*tot(offs+1:r-1));
	sum3=sum(his(1:r-1-offs)'.*tot(offs+2:r));
	else
	sum1=sum(his(2-offs:r)'.*tot(1:r-1+offs));
    	sum2=sum(his(2-offs:r)'.*tot(1:r-1+offs));
        sum3=sum(his(1-offs:r-1)'.*tot(2:r+offs));
	end;

	if (sum1>=sum2) & (sum1>=sum3) offs=offs-1; end;
        if (sum3>=sum2) & (sum3>=sum1) offs=offs+1; end;


 	if (offs >0)
        his(1:r-offs)=his(1:r-offs)+tot(offs+1:r)';
        else
        his(1-offs:r)=his(1-offs:r)+tot(1:r+offs)';
        end;
	ofar(count)=offs;
	count=count+1;
end;
his=his-prehis/4;
end;

