function [N_features,theta]=cp_linpred(x,FeatureFN,ImageId,NonlinId,SmoothId,NormId,FeatDec,N_tex,r,sigma,theta)
% CP_LINPRED  Comparative texture measure study: Optimal linear predictor

% Initialization
if strcmp(lower(SmoothId),'gauss')
  if isempty(sigma), sigma=8; end
  f_0=1./(2*sqrt(2)*sigma);
elseif strcmp(lower(SmoothId),'average')
  M_w=31; N_w=31;
  f_0=[M_w N_w];
else
  error(fprintf('Smoothing option "%s" not supported',SmoothId));
end
N_features=N_tex;

% Design filter
if isempty(theta)
  N=neigh(r);
  for TexNo=1:N_tex
    fprintf(1,'.');
    x_i=cp_image(ImageId,TexNo);
    theta_i=unpack_param(symmetric_ext(LS_symmest(x_i,N)));
    theta=[theta;theta_i];
  end
  fprintf(1,':');
end

% Feature extraction and classification
[M_theta,N_theta]=size(theta);
M_theta=M_theta/N_features;
% zz=[]; % DEBUG
for n_theta=1:N_features
  fprintf(1,'.');
  theta_i=theta((1:M_theta)+(n_theta-1)*M_theta,1:N_theta);
  y=x-arpred(x,theta_i);
  z=cp_energy(y,NonlinId,SmoothId,NormId,f_0);  % Local energy function
  % zz=[zz z]; % 				DEBUG
  catframe(z,FeatureFN,[],'Interm',FeatDec);  % Write feature file
end
% gimage(zz); fprintf(1,'Press any key'); pause; % DEBUG

  


