% crevconv2.m
% usage: Y = crevconv2(X,h) 
% Performs 2D circular convolution by the time reversal (in two dimnesions) 
% of h on X, the result has the same size as X.
% It is assumed that both the row dimension and column dimension 
% of h do not exceed those of X. 
% The result is the same as zero-padding h out to the size of X, 
% and then computing the convolution X*h.


function Y=crevconv2(X,h)
   [m,n] = size(X);
   Y = conv2(X,fliplr(flipud(h)));
   [r,s] = size(Y);
   Y(m+1:r,:) = Y(m+1:r,:)+Y(1:r-m,:);
   Y(:,n+1:s) = Y(:,n+1:s)+Y(:,1:s-n);
   Y=Y((r-m+1):r,(s-n+1):s);
   
