function N_features=cp_optxf(x,FeatureFN,Criterion,ImageId,NonlinId,SmoothId,NormId,FeatDec)
% CP_OPTXF  Comparative texture measure study: Optimal filter features
%	N_features=cp_optxf(x,FeatureFN,Criterion,ImageId,...
%	  NonlinId,SmoothId,NormId,FeatDec)

% Initialization
N_features=1;
if strcmp(lower(SmoothId),'gauss')
  f_0=2.^(-5)*sqrt(2);
  sigma=1./(2*sqrt(2)*f_0);
  [dummy,w]=gausslp([],sigma);
elseif strcmp(lower(SmoothId),'average')
  M_w=31;
  N_w=31;
  f_0=[M_w N_w];
  w=ones(M_w,N_w)/(M_w*N_w);
else
  error(fprintf('Smoothing option "%s" not supported',SmoothId));
end
if ~strcmp(lower(NonlinId),'sqr')
  error(fprintf('Nonlinearity option "%s" not supported',NonlinId));
end
M_h=7;
N_h=7;

% Filter design
[x_1,N_tex,r_xx1]=cp_image(ImageId,1); x_1=x_1-mean(x_1(:));
[x_2,N_tex,r_xx2]=cp_image(ImageId,2); x_2=x_2-mean(x_2(:));
h=optxf(Criterion,x_1,x_2,[M_h N_h],w,r_xx1,r_xx2);

% Filtering
y=filter2(h,x);

% Local energy function
z=cp_energy(y,NonlinId,SmoothId,NormId,f_0);

% Write feature file
catframe(z,FeatureFN,[],'Interm',FeatDec);




