function y = extn2D(x,f,mode)

% AUTHORS:
%    Jingyu Yang, DA, Tsinghua University, July 2005
%    yangjy03@mails.tsinghua.edu.cn
% y = extn2D(x,n, mode)
% INPUT:
%    x - MxN input signal
%    f - the employed filter for subsequent decomposition
%    mode - the mode of extersion, it could be set as follows
%    'sym' - symmetic extension
%    'zpd' - zero padding  
%    'cpd' - constant padding
%    'rpd' - random padding
%    'sym2' -  symmetric extension producing N/2 coefs for each sub-band
%              This mode is only for symmetric filter 
% OUTPUT:
%     y - 1x(N+2n-2) signal
%     
M = size(x,1);
nf = length(f);
n = nf - 1;
if ischar(mode)
else
    errargt(mfilename,'use !','msg');
end
    
switch mode
case 'symeven'
% even symmetric extension  
    l = flipud(x(1:n, :));
    r = flipud(x((M-n-1:M), :));
case 'symodd'
% odd symmetric extension
    l = flipud(x(2:(n+1), :));
    r = flipud(x((M-1-(n-1)):(M-1), :));
   
case 'zpd'
% zero extension 
%     l = zeros(1,n);
%     r = zeros(1,n);    
case 'cpd'
% constant extention
%    l = ones(1,n)*x(1);
%    r = ones(1,n)*x(M);    
case 'rpd'
% random extension
%    l =100* rand(1,n);
%    r =100* rand(1,n);  
case 'sym2'
%  derive the real length of filter
%    f = abs(f(:)');
%    mask1 = cumsum(f);
%    mask2 = fliplr(cumsum(fliplr(f)));
%    mask = mask1&mask2;
%    n = sum(mask);
%    if ~rem(n, 2) 
%        l = fliplr(x(1:n/2));
%        r = fliplr(x(M-n/2-1:M));
%    else
%        l = fliplr(x(2:n/2));
%        r = fliplr(x(M-n/2:M-1));       
%    end
%         
end

y = [l; x; r];