function [N_features,FiltPars]=cp_weldon(x,FeatureFN,ImageId,NonlinId,SmoothId,NormId,FeatDec,N_tex,NumFilt,FiltPars)
% CP_WELDON  Comparative texture measure study: Optimal Gabor filterbank feat.

% Default values
if N_tex<10
  sigmas=[2 4 8 16];
else
  sigmas=[2 4 8];
end
nstd=2.5;

% Design filter
if isempty(FiltPars)
  data=[];
  for TexNo=1:N_tex
    data=[data;cp_image(ImageId,TexNo)];
  end
  sigma_ps=2*sigmas;
  [FiltPars,J_min]=weldon(NumFilt,N_tex,data,sigmas,sigma_ps);
  clear data
end
N_features=NumFilt;

% Feature extraction
for n=1:N_features
  fprintf(1,'.');
  UV=FiltPars(n,1);
  sigma=FiltPars(n,2);
  sigma_p=2*sigma;
  [dummy,h]=gabor([],abs(UV),angle(UV),sigma,sigma,nstd);
  [M_h,N_h]=size(h);
  H=zeros(size(x)); H(1:M_h,1:N_h)=h; H=fft2(H);
  y=ifft2(H .* fft2(x));
  f_0=1./(2*sqrt(2)*sigma_p);
  z=cp_energy(y,NonlinId,SmoothId,NormId,f_0);  % Local energy function
  catframe(z,FeatureFN,[],'Interm',FeatDec);  % Write feature file
end




