%function blocks = blocks(blocknumber, total);

%b1 = [0 10 20 30 40 50 60 70; 10 20 30 40 50 60 70 0; 20 30 40 50 60 70 0 10 ; 30 40 50 60 70 0 10 20; 40 50 60 70 0 10 20 30; 50 60 70 0 10 20 30 40; 60 70 0 10 20 30 40 50; 70 0 10 20 30 40 50 60; 0 10 20 30 40 50 60 70; 10 20 30 40 50 60 70 0];
b=[10 10 20 20 30 30 40 40 50 50; 10 10 20 20 30 30 40 40 50 50; 30 30 40 40 50 50 60 60 70 70; 30 30 40 40 50 50 60 60 70 70; 50 50 60 60 70 70 10 10 20 20 ; 50 50 60 60 70 70 10 10 20 20; 70 70 10 10 20 20 30 30 40 40 ; 70 70 10 10 20 20 30 30 40 40];
%b = b1'; 
image(b);

mvx=[0 0 0 0 0 ;1 1 1 1 1;  0 0 0 0 0; 0 0 0 0 0];
blocksize = 2; % a 2x2 block

maxshift=2; %maximum suppported shift
bs=blocksize+maxshift;

zpad = zeros(size(b));
bpad = [zpad zpad zpad; zpad b zpad; zpad zpad zpad];
shifta = mvx;
%shifta = zeros (8*10);

%shiftb1 = [2 2 2 2 2;2 2 2 2 2; 1 1 1 1 1; 1 1 1 1 1];
shiftb1=zeros(4,5);
shiftb = shiftb1';

result=zeros(size(bpad));
resultv = zeros(size(bpad));

bmm = blocksize+ 2*maxshift;

x=0;
g = 1+size(b,1);

while g <= 2*size(b,1),
   
   x=x+1;
   s = 1+size(b,2);
   y = 0;
   while s <= 2*size(b,2),
       y = y+1;
      block=bpad((g-maxshift):(g+bs),(s-maxshift):(s+bs));
      
      shifta1=shifta(x,y);
      for k=1:bmm,
      ffta=fft(block(k,:));
   			for m=1:bmm,
               ffta(m)=ffta(m)*exp(j*2*shifta1*(m-1)*pi/bmm);
               
            end
     
               c(k,:)=ifft(ffta);
end   
      result(g:(g+blocksize),s:(s+blocksize))=c(maxshift+1:bs+1, maxshift+1:bs+1);   
      s = s +blocksize;
      
   end
   g = g+blocksize;

end




theend = zeros(size(b));
for k = 1:size(b,1),
   for j = 1:size(b,2),
   theend(k,j) = result((k+size(b,1)), (j+size(b,2)));
end
end
image(b);
figure;
image(real(theend));



