function [cc,cvid]=JCSD_detector_DCCP2(x,libs,wavel,clusters, list_corr_pairs)


[posPeaks, ampsPeaks, yy, y_smoothed]=extracting_peaks(mean(x,2)',wavel);   
baseline=y_smoothed-yy;
x=x-baseline'*ones(1,size(x,2));


while rank(x)~=size(x,2)
    x=x(:,1:end-1);
end

% Both are column vectors
[numclusters,n]=size(clusters);
BB=zeros(numclusters,n);
for j=1:numclusters
    icount(j)=length(find(clusters(j,:)>0));
    cp=libs(:,clusters(j,1:icount(j)));     
    [A,B,r1] = canoncorr(x,cp);
    
    
    rr(j)=r1(1);
    
    BB(j,1:icount(j))=B(:,1)';
end

cc=max(rr);
pos=find(rr==cc);
if length(pos)~=1
    error('Two clusters having maximum canonical correlation! Take a look here!!');
end
ic=pos(1); % the cluster that target belongs to
cvs=abs(BB(ic,:));
pos2=find(cvs==max(cvs));
cvid=clusters(ic,pos2(1));


% 
% 
% % If spectrum in high correlated list is found, then perform the second
% % stage indentification
% if ismember(cvid,list_corr_pairs(:,2))
%     tp=find(list_corr_pairs(:,2)==cvid);
%     list1=list_corr_pairs(:,1);    
%     [cc,cvid]=JCSD_detector_2ndstage(x,libs,cvid,list1(tp));
% end




