function [ AvLength ] = BlockAvKLength( imBlock)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

  TotalDist=0;
  TotalLine=0;
    
  
  BlockSize=size(imBlock,1);
  
 for i=1:BlockSize
    
%MaskedSignal=zeros(1,4*64);
     
X=double(imBlock(i,:));

ti=1/4:1/4:BlockSize; % interpolation by 4

Xi = interp1(1:BlockSize,X(1:BlockSize),ti,'spline'); %'linear'); --slow?


%========== projection =============

Y=diff(Xi);
Y(size(Xi,2))=0;


Y=abs(Y)/norm(Y);


 cEnergy=0;
 devCount=0;
 devLeft=0;
 devRight=0;
 
 [EdgeMax MaxIndex]=max(Y);
    
while (and(cEnergy < 0.9,devCount<BlockSize-3))
    
 devCount=devCount+1;
  if mod(devCount,2)==0
     devLeft=devLeft+1;    
  else
     devRight=devRight+1; 
   end
 
 
% MaskedSignal=ChopByMax(Y,devDist);
 if ((MaxIndex>BlockSize/2) &&(MaxIndex<BlockSize*4-(BlockSize/2)))
    
 cEnergy=norm(Y(MaxIndex-devLeft:MaxIndex+devRight));  
 end
 
end
 
 
if devCount<BlockSize
 dist=devLeft+devRight+1;
 TotalDist= TotalDist+ dist;
 TotalLine= TotalLine+1;
end
 
 
 
 end % end of each block .
 
 AvLength= TotalDist/TotalLine;

end

