function expimg = expand(img)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CS223b HOMEWORK 1, "DOUBLE IMAGE SIZE"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Emilio Antunez (1/27/2003)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% this is the 5x5 blurring kernel used in lecture
kernel = [1;4;6;4;1]*[1 4 6 4 1]/16^2;

% for cleaner notation, we want the dimensions of img in an array
dim = size(img);

% add padding rows/columns for filtering, as suggested in B&A, '83
img = [2*img(:,1)-img(:,2)  img  2*img(:,dim(2))-img(:,dim(2)-1)];
img = [2*img(1,:)-img(2,:); img; 2*img(dim(1),:)-img(dim(1)-1,:)];

% insert zeros between rows and columns
img = upsample(upsample(img,2)',2)';

% interpolate the sampled image using the above kernel
% (padding is implicitly removed by 'valid')
expimg = 4*filter2(kernel,img,'valid');
  