function y=prederr2(x,x_1,N1,N2,Symmetric,N_w,Smoother)
%PREDERR2	y=prederr2(x,x_1,N1,N2,Symmetric,N_w,Smoother)
%	Perform texture attribute extraction with an AR predictor and
%	a local energy function on the image x, with the texture regions 
%	(which the filter is optimized with respect to) defined by
%	m1, n1, m2 and n2.
%	Smoother may be 'Gauss', 'Rectangular', 'Maximum filter', 'Minimum filter'
%	NaN are replaced by 0 (zero) prior to the filtering operation.
%
%	Example:
%	v=prederr2(z,z(351:450,331:430),z(501:600,1051:1150));

WaitbarHndl=waitbar(5,'Linear prediction error computation',100);
% Get the size of the input
[xM,xN]=size(x);

% Replace all NaNs by zeros, and retain a matrix with zeros where x have
% values, and NaNs where x have NaNs
NaNs=x-x; 
x=NaN2Val(x,0);
waitbar(10);

% Optimal filter design
neighborhood=neigh(N1,N2);
if Symmetric,
  theta=unpack_param(symmetric_ext(LS_symmest(x_1,neighborhood)));
else
  theta=unpack_param(LS_est(x_1,neighborhood));
end
waitbar(70);

% Symmetric extension
top=N1+N_w; bot=N1+N_w; lft=N2+N_w; rgt=N2+N_w;
x=symmext(x,top,bot,lft,rgt);

% Prediction error calculation
y=x-arpred(x,theta);
waitbar(80);

% 
y=y.^2;
if strcmp(Smoother,'Gauss'),
  y=gaussmth(y);
elseif strcmp(Smoother,'Rectangular'),
  y=avgsmth(y,N_w);
elseif strcmp(Smoother,'Maximum filter'),
  y=maxfilt2(y,[N_w N_w]);
elseif strcmp(Smoother,'Minimum filter'),
  y=minfilt2(y,[N_w N_w]);
else
  error('Unsupported smmothing operator requested');
end
waitbar(100);

% Remove the symmetric extension
y=y(top+1:top+xM,lft+1:lft+xN);

% Put NaN's where x had NaN's
y=NaNs + y;
close(WaitbarHndl);
