function y = extract(f, a, b, alen, blen)

sze = size(f);
sze = sze(1);

if ((a - (alen-1)/2) > 0)
   numRowsAbv = (alen-1)/2;
   pinit = 1;
else
   numRowsAbv = a-1;
   pinit = ((alen-1)/2 + 1) - numRowsAbv;
end

if ((a + (alen-1)/2) > sze)
   numRowsBel = sze - a;
else
   numRowsBel = (alen-1)/2;
end

if ((b - (blen-1)/2) > 0)
   numColsLeft = (blen-1)/2;
   qinit = 1;
else
   numColsLeft = b-1;
   qinit = ((alen-1)/2 + 1) - numColsLeft;
end

if ((b + (blen-1)/2) > sze)
   numColsRt = sze - b;
else
   numColsRt = (blen-1)/2;
end

temp = zeros(alen, blen);

p = pinit;
q = qinit;
for k = (a - numRowsAbv):(a+numRowsBel)
   for j = (b - numColsLeft):(b+numColsRt)
      temp(p,q) = f(k,j);
      q = q + 1;
   end
   q = qinit;
p = p + 1;
end

y = temp;