% 2-d convolution gaussian filter
clear
x=[1 -1 3 2;
    2 1 2 4;
    1 -1 2 -2;
    3 1 2 2]; % input image
h=fspecial('gaussian',[3 3],0.5);
[s,hc,hr]=isfilterseparable(h);hr=-hr;
% choose one of the border extensions, lines 10,18 or 11,19
%xe = padarray(x,[1 1],'circular') % periodic
xe = padarray(x,[1 1]) % zero-padded
[R C]=size(x);
for r=1:R
  for c=1:C 
    x(r,c)=hr(1)*(xe(r+1,c)+xe(r+1,c+2)) +hr(2)*xe(r+1,c+1);
  end
end
%xe = padarray(x,[1 1],'circular') % periodic
xe = padarray(x,[1 1]) % zero-padded
%
for c=1:C
  for r=1:R 
    x(r,c)=hr(1)*(xe(r,c+1)+xe(r+2,c+1)) +hr(2)*xe(r+1,c+1);
  end
end
x % output


