function [POffset,peakc, peakc1, HalfPeak, HPW,widths1,widths2,kindex]=peak_correl(correl,confint,signals,shoulder,MeanCorrel,PW50)
Rshoulder=length(correl{1}(1,:))-shoulder;
for i=1:length(correl);
    for h=1:length(correl);
    peakc1(i,h)=max(correl{i}(h,:));
%     if peakc1(i,h)<confint{i}(h,2)
%         peakc1(i,h)=0;
%     end
    SBins(1:shoulder)=correl{i}(h,1:shoulder);
    SBins(shoulder+1:2*shoulder)=correl{i}(h,Rshoulder+1:end);
    SBM=mean(SBins);
    kindex(i,h)=peakc1(i,h)/SBM;
    if isnan(kindex(i,h))==1;
        kindex(i,h)=0;
    end
    if isinf(kindex(i,h))==1;
        kindex(i,h)=0;
    end 
    Pindex=find(correl{i}(h,:)==peakc1(i,h))-((length(correl{i}(h,:))-1)/2+1)
    [~,inx]=min(abs(Pindex))
    POffset(h,i)=Pindex(inx)
    end
end


peakc=(peakc1./max(max(peakc1))).*1000;

%%HaLf Peak%%%%%%%%%%
for i=1:length(correl);
    for h=1:length(correl);
    HalfPeak(i,h)=MeanCorrel{i}(h)+(peakc1(i,h)-MeanCorrel{i}(h))/2;
    end
end


HPW=zeros(length(correl),length(correl));
for i=1:length(correl);
    for h=1:length(correl);
        
       a=find(correl{i}(h,:)>HalfPeak(i,h));
       if isempty(a)==0
       widths1(i,h)=a(1);
       widths2(i,h)=a(end);
       HPW(i,h)=(widths2(i,h)-widths1(i,h))+1;
       else
           HPW(i,h)=0;
       end
       if HPW(i,h)>PW50;
           HPW(i,h)=NaN
       end
       
    end
end

filedate=datestr(now,'yyyymmdd_HHMMSS');
cmd=strcat('Export_Data\','Results_',filedate);
for i=1:length(signals)
    rows{i}=signals(i).name
end

    
xlswrite(cmd,peakc1,'Peak','B2')
xlswrite(cmd,rows,'Peak','B1')
xlswrite(cmd,rows','Peak','A2')
xlswrite(cmd,HPW,'HPW','B2')
xlswrite(cmd,rows,'HPW','B1')
xlswrite(cmd,rows','HPW','A2')
xlswrite(cmd,kindex,'kindex','B2')
xlswrite(cmd,rows,'kindex','B1')
xlswrite(cmd,rows','kindex','A2')
xlswrite(cmd,POffset,'Peak Offset','B2')
xlswrite(cmd,rows,'Peak Offset','B1')
xlswrite(cmd,rows','Peak Offset','A2')
% 
% PeakbyHPW=peakzc./HPW
% PeakbyHPW1=(PeakbyHPW/max(max(PeakbyHPW)))*1000


