function [cm,L_all]=esc_class(classes,model,feat,spe,ipart,do_hmm,do_pbn,NSTATES,NMODE,min_std)
% spe = segments per event
% esc_class({'esc0','esc6','esc9'},'esc4z','mel4',16,0);
% OR:
% esc_class('ESC8a','esc4f',(ignored),16,0);
addpath ../CORE/PDF_Models/GmixHmm/
if iscell(classes),
   is_default=0;
   M=length(classes);
else,
   is_default=1;
   p=load([classes '.mat'],'tch');
   M=size(p.tch,1);
end;

% train HMMs
%min_std=.1;
%NSTATES=3;
%NMODE=4;
if(do_hmm>0),
    for iclass=1:M,
        % read in the features for training
        if(is_default),
           fn=[model,'_',classes,'_c',num2str(iclass),'_',classes,'_out0.mat'];
           pfwd=load(fn,'z','y');
           ii=find(pfwd.y(:,iclass));
           pfwd.z=pfwd.z(ii,:);
           [ns,dim]=size(pfwd.z);
           pfwd.z=reshape(pfwd.z,ns*spe,dim/spe);
           %fprintf('Read %s, ns=%d, dim=%d\n',fn,ns,dim);
           nev=ns;
           ns=ns*spe;
        else,
           fn=[model,'_',classes{iclass},'_',feat,'_',classes{iclass},'_',feat,'_out',num2str(ipart),'.mat'];
           pfwd=load(fn,'z');
           [ns,dim]=size(pfwd.z);
           nev=ns/spe;
        end;
        nsamp=ones(1,nev)*spe;
        istart=[1:spe:ns-spe+1];
        for i=1:dim,names{i}=sprintf('d%d',i); end;
        parm{iclass}=init_hmm(pfwd.z',NSTATES,NMODE,names,min_std);
        NIT=100;
        [log_pdf_val, parm{iclass}] = hmm_reest(parm{iclass}, pfwd.z', istart, nsamp, NIT);
    end;
end;

% test
cm=zeros(M,M);
nerr=0;
L_all = [];
for iclass=1:M,
    L=[];
    for imodel=1:M,
       if(do_hmm),
           % read in the features
           if(is_default),
              fn=[model,'_',classes,'_c',num2str(imodel),'_',classes,'_out',num2str(ipart),'.mat'];
              pfwd=load(fn,'z','y');
              ii=find(pfwd.y(:,iclass));
              pfwd.z=pfwd.z(ii,:);
              [ns,dim]=size(pfwd.z);
              pfwd.z=reshape(pfwd.z,ns*spe,dim/spe);
              %fprintf('Read %s, ns=%d, dim=%d\n',fn,ns,dim);
              nev=ns;
              ns=ns*spe;
           else,
              fn=[model,'_',classes{iclass},'_',feat,'_',classes{iclass},'_',feat,'_out',num2str(ipart),'.mat'];
              pfwd=load(fn,'z');
              [ns,dim]=size(pfwd.z);
              nev=ns/spe;
           end;
           nsamp=ones(1,nev)*spe;
           istart=[1:spe:ns-spe+1];
           log_pdf_val = hmm_reest(parm{imodel}, pfwd.z', istart, nsamp, 0);
       end;

       if(do_pbn),
          if(is_default),
              fn_pbn=[model,'_',classes,'_c',num2str(imodel),'_',classes,'_jout',num2str(ipart),'.mat'];
              ppbn=load(fn_pbn,'l_out','lpo','y');
              ii=find(ppbn.y(:,iclass));
              ppbn.l_out=ppbn.l_out(ii);
              ppbn.lpo=ppbn.lpo(ii);
              nev=length(ppbn.lpo); % lpo is the final output PDF g(z)
              lp=reshape(ppbn.l_out(:)-ppbn.lpo(:),nev,1);
          else,
              fn_pbn=[model,'_',classes{imodel},'_',feat,'_',classes{iclass},'_',feat,'_jout',num2str(ipart),'.mat'];
              ppbn=load(fn_pbn,'l_out','lpo');
              lp=reshape(ppbn.l_out(:)-ppbn.lpo(:),spe,nev);
          end;
       else,
          lp=0;
       end;
       if(do_hmm),
           lp=lp*do_pbn+log_pdf_val(:)*do_hmm;
       else,
          if(is_default),
             nev=length(ppbn.l_out);
             lp=ppbn.l_out(:);
          else,
             nev=length(ppbn.l_out)/spe;
             lp=sum(reshape(ppbn.l_out(:),spe,nev),1)';
          end;
       end;
       L=[L lp(:)];
    end;
    if(is_default),
        nt=size(pfwd.y,1);
        ii=find(pfwd.y(:,iclass));
        if(iclass==1),
           L_all=ones(M,nt);
        end;
        L_all(:,ii)=L';
    end;
       
    %fn=[classes, '_', num2str(hh];
    [lm,im]=max(L,[],2);
    nr=sum(im~=iclass);
    for j=1:M, cm(iclass,j)= cm(iclass,j) + sum(im==j); end;
    nerr=nerr+nr;
    imagesc(exp( (L-repmat(lm,1,M))/10))
    colormap hot
    drawnow
    if(is_default),
       fprintf('Class %s/%d, nerr=%d\n', classes,iclass,nr);
    else,
       fprintf('Class %s, nerr=%d\n', classes{iclass},nr);
    end;
end;
fprintf('Nerr=%d\n', nerr);
cm=nerr;
format compact
