% Determine target from high correlated list of spectra using DCB
function [cc,cvid]=JCSD_detector_2ndstage(x,libs,curid,idlist)

[n,m]=size(x);
for k=1:m
    xc(:,k)=x(:,k)-mean(x(:,k));
end              
ccDCB=xc*inv(xc'*xc)*xc';   


[n1,numofcurlib]=size(libs);    
idx=[curid idlist'];
len=length(idx);
for k=1:len
    rrs(k)=sqrt(libs(:,idx(k))'*ccDCB*libs(:,idx(k)));
end

cc=max(rrs);
pos=find(rrs==cc);
cvid=pos(1);

%disp('second stage detection!!');
