function [tempwins]=HRI_repeatfinder_1sec(inwave,pwidth) 
%this does everything: LRI, LRIcutter, and HRI
qcount=0;


%SET thresholds

LRIthresh = 0.4;  % threshold passing LRI calculation
LRInormthresh = LRIthresh;  % can be altered if amplitude differences are considered (see below)
HRthresh = 0.5;  % threshold for each PSP or PSC to pass
HRIthresh = 0.5; % threshold for PSP amp normalized calculation into final HRI index

fina=inwave;

numcells=1;
SampleRate = 1;     % points/second
increment = 1000;      % [points] - template length
starttime = 1;
resolutionvar = 4;
width = increment/resolutionvar;    % [points] - step size
template_start = starttime;
wincount = 0;
increment_out = increment;
%threshold_out = threshold;

threshcounter = 1;
yy=0;

%   Load matlab file
namefile=fina;
axowave=load(fina);     % Always keep your file in Matlab/work directory
axowave=getfield(axowave,fina);

if(numcells==1)
    dagwave = axowave;
else
    dagwave=load(finb);
end;

if(numcells==2)
    namefile=name2file;
end;

axonpnts = length(axowave);
dagnpnts = length(dagwave);

%if axonpnts~=dagnpnts
%    print ('Warning!!!');
%end;

% differentwaves = prod(axowave == dagwave);


num = min(axonpnts,dagnpnts)-increment;

%  dagwave = [(zeros([1,increment])),dagwave];

%   Define matrices to fill out later

winnerlist = [];
tempwins = [];


winners_counter = 1;

while template_start < num
    template_start;
    if (numcells==1)
        counter = template_start + increment;
    else counter = 1;
    end;

    while counter < num
        delta_axo = axowave(template_start:template_start+increment);
        delta_dag = dagwave(counter:counter+increment);

%        SDaxoreal = std(delta_axo);
        %SDaxo = (SDaxoreal^2)*increment;

%        SDdagreal = std(delta_dag);
        %SDdag = (SDdagreal^2)*increment;
        if (max(abs(delta_axo))*max(abs(delta_dag))==0)
            maxvalue=0;
        else
            shift_CCG =  xcov(delta_axo,delta_dag,'coef');
            maxvalue = max(shift_CCG);
            maxloc = find(shift_CCG == maxvalue)-increment;
            maxloc=maxloc(1);
        end;
  %      if((SDaxo*SDdag)==0)
           % maxvalue=0;
            %   else
           % maxvalue = maxvalue/sqrt(SDaxo*SDdag);        
           %     end;


        % if ((-width) < maxloc) & (maxloc < width)
            yy=0;
            if ((maxvalue > LRIthresh) & (abs(counter+maxloc-template_start)>width))
            counterB=counter;
            while yy < 3    % below procedure centers templates according to peak of CCG
                threshcounter = counterB-maxloc;
                if threshcounter < increment
                    threshcounter=counterB+maxloc;
                end;
                if threshcounter > num
                    threshcounter=counterB+maxloc;
                end;
                if threshcounter < 1
                    threshcounter=1;
                end;
                delta_dag = dagwave(threshcounter:(threshcounter+increment));
 %               SDdagreal = std(delta_dag);
%                SDdag = (SDdagreal^2)*increment;
                
                if(max(abs(delta_axo))*max(abs(delta_dag))==0)
                    vmax=0;
                    maxloc=0;
                    shift_CCG(increment+1)=0;
                    break;
                   else 
                    shift_CCG =  xcov(delta_axo,delta_dag,'coef');
                    max_shift_CCG = max(shift_CCG);
                    maxloc = find(shift_CCG == max_shift_CCG)-increment;
                    maxloc = maxloc(1);   
                end;                
                         
                
                if abs(maxloc)<50
                    yy=20;
                end;
                yy=yy+1;
                counterB=threshcounter;    
            end;
   %             delta_axo=delta_axo-mean(delta_axo);
   %             delta_dag=delta_dag-mean(delta_dag);                
   %             delta_axo=delta_axo-1000;delta_axo=abs(delta_axo);
   %             delta_dag=delta_dag-1000;delta_dag=abs(delta_dag);
   %             subaxodag = (abs(delta_axo - delta_dag))/2;
   %             delta_axo=delta_axo-1000;delta_dag=delta_dag-1000;
   %             deltawaveavg = (abs(delta_axo) + abs(delta_dag))/2;
   %             mean_twowaves = mean(deltawaveavg);
                
              
                if (template_start==threshcounter)
                    err=1;  %ensures vmax=0 if threshcounter has been adjusted to equal template_start, resulting in the same waves compared
                else
%                    if (mean_twowaves>0)
%                        err = mean(subaxodag)/mean_twowaves;  
%                        % currently disabled.  If re-enabled, then amplitude differences are considered  
                err=0;
                        %                        else
                        %                        err=1;
                end;
                    
                % end;
                
                

                    vmax = shift_CCG(increment+1);%/sqrt(SDaxo*SDdag);

                

       %         vmax = vmax*(1-err);
                if (vmax>0)
                 vmax = vmax*(1-err);
                end;
                 if ((vmax > LRInormthresh) & (abs(threshcounter+maxloc-template_start)>width))

                    tempwins(winners_counter, 1) = template_start;
                    tempwins(winners_counter, 2) = threshcounter;
                    tempwins(winners_counter, 3) = vmax;
               %     tempwins(winners_counter, 4) = err;
              %      winners_counter;
                    winners_counter = winners_counter+1;
                  end;
                if (winners_counter>2)
                    if ((template_start==tempwins((winners_counter-2),1)) & (threshcounter==...
                             tempwins((winners_counter-2),2)))
                        winners_counter = winners_counter-1;
                        tempwins(winners_counter,:)=[];
                    end;
                end;
                     
            end;
            %end;
     counter = counter + ceil(increment/resolutionvar);
     end;
  template_start = template_start + ceil(increment/resolutionvar);



%SDaxo = 0;
%SDdag = 0;
shift_CCG = [];

end; %>>>>>>>>>>>>> END of template_start-num loop

winnerslist = tempwins;

% Save variables
%if (numcells==1)
%    namefile(length(namefile)-3)='x'
%end;

%result = ['K', namefile];
result=namefile
table=result;
%result
lengthdis=size(winnerslist,1);
%save (result, 'winnerslist','-ASCII');
%LRI_cutter

%<<<<<<<<<<<<<<<<<<<<  BEGIN LRI CUTTER  >>>>>>>>>>>>>>>>>>>>>

xx=0; maxx=0; minn=0;kk=0;
bbb=0;aaa=0;


SampleRate=1;
skips=3;
cutwidth=350*SampleRate;
cutdepth=30;
namefile=table;
%tempwins=load(table);               %turn on if running LRI_cutter alone

winnerslist_npnts=size(tempwins,1);
zz=1;
xx=1;
kk=1;
kk=xx+1;
while (zz<50)
xx=1;
kk=1;
kk=xx+1;
    dontstop=0;
while ((kk)<winnerslist_npnts)

   while ((kk-xx)<cutdepth)
	xxx=1;
	if (bbb==skips)
		kk=kk+1;
		bbb=0;
    end;
		if ((abs(tempwins(xx,1)-tempwins(kk,1))<cutwidth) & (abs(tempwins(xx,2)-tempwins(kk,2))<cutwidth))
			dontstop=1;
            bbb=bbb+1;
				if (tempwins(xx,3)>tempwins(kk,3))
					tempwins(kk,:)=[];
					kk=kk-1;
					xx=xx-1;
					xxx=0;
                end;
                if (xx<1)
                    xx=1;
                end;
				if (tempwins(xx,3)<tempwins(kk,3))
				if (xxx==1)
					tempwins(xx,:)=[];
					xx=xx-1;
					kk=kk-1;
					xxx=0;
                end;
                end;
                if (xx<1)
                    xx=1;
                end;
				if (tempwins(xx,3)==tempwins(kk,3))
				if (xxx==1)
				    tempwins(xx,:)=[];   
					xx=xx-1;
					kk=kk-1;
					xxx=0;
                end;
                end;
				
				
		else			
			bbb=0;
		
        end;


		xxx=1;
		winnerslist_npnts=size(tempwins,1);				
        
		if (xx<1)
			xx=1;
        end;
	kk=kk+1;
	if (kk>winnerslist_npnts)
		break
    end;
end;

xx=xx+1;
kk=xx+1;
end;
zz=zz+1;
if (dontstop==0)
    numrepeats=zz-1;
    zz=100;
end;
end;

result = [namefile,'C'];
distable=result
lengthdisC=size(tempwins,1);
LRIcutresult=tempwins;
save (result, 'tempwins','-ASCII');  %can be turned off.  leaving it on can be
                                     %useful for diagnostics

                                     

return                                   
                                     
                                     
 
%--------------------------------begin HRI_finder-------------------------------------
                                     
                                     
increment = pwidth*SampleRate;      % [points] - template length
%threshold = HRthresh;
starttime = 1;
resolutionvar = pwidth;
width = ceil(increment/resolutionvar);    % [points] - step size
template_start = starttime;
wincount = 0;
increment_out = increment;
%threshold_out = threshold;
%threshold_final = HRIthresh;
threshcounter = 1;
xx=0;vmaxmark=0;vmaxold=0;
counterold=-25;
tally=0;totaltally=0;dividetally=0;onit=0;tgcount=1;

%   Load txt file
%axowave=load(fina);        %turn on if running this program alone

%if (numcells==2)
%    dagwave=load(finb);
%else    
%    dagwave = axowave;
%end;


tempwins = LRIcutresult;

axonpnts = length(axowave);
dagnpnts = length(dagwave);
winnerslistnpnts = size(tempwins,1);

winnerlist = [];
tempwinsHR = [];
tempgrouped=[];
axoseg=[];dagseg=[];axosegnorm=[];dagsegnorm=[];


winners_counter = 1;
xx=1;
if (winnerslistnpnts>0)
while xx < winnerslistnpnts

    axoseg = axowave(tempwins(xx,1):tempwins(xx,1)+999);
    dagseg = dagwave(tempwins(xx,2):tempwins(xx,2)+999);
    axosegnorm = axoseg - mean(axoseg);
    dagsegnorm = dagseg - mean(dagseg);
    normize = sqrt(std(axosegnorm)*std(dagsegnorm));
    normize = normize/mean(abs(axosegnorm-dagsegnorm));
    
    
    counter=1;
    theend=length(axoseg);
    tempgrouped(tgcount,1)=tempwins(xx,1);
    tempgrouped(tgcount,2)=tempwins(xx,2);
    tempgrouped(tgcount,3)=0;
    
    while (counter+increment) < theend 

        delta_axo = axoseg(counter:counter+increment);
        delta_dag = dagseg(counter:counter+increment);

        SDaxo = std(delta_axo);
        SDaxoreal = SDaxo;

        SDdag = std(delta_dag);
        SDdagreal = SDdag;


        if (max(abs(delta_axo))*max(abs(delta_dag))==0)
            vmax=0;
        else 
            shift_CCG =  xcov(delta_axo,delta_dag,'coef');
            vmax = shift_CCG(increment);    
        end;
          
            
            if vmax>HRthresh
                
                    
                delta_axo=delta_axo-mean(delta_axo);
                delta_dag=delta_dag-mean(delta_dag);                
                
                subaxodag = (abs(delta_axo - delta_dag));
                
                delta_axo=abs(delta_axo);
                delta_dag=abs(delta_dag);
                
                
                deltawaveavg = abs(delta_axo) + abs(delta_dag);
                mean_twowaves = mean(deltawaveavg);
  
                err = mean(subaxodag)/mean_twowaves;
                
                    vmax = vmax*(1-err);
                    
                

                if (vmax > HRIthresh)

                    if (((counter+tempwins(xx,1)-0)-counterold)>100)    
                        tempwinsHR(winners_counter, 1) = counter+tempwins(xx,1);
                        tempwinsHR(winners_counter, 2) = counter+tempwins(xx,2);
                        vmaxold=vmax;
                        counterold=tempwinsHR(winners_counter,1);

                        tally=tally+1;
                        onit=onit+1;
                        if (onit==1)
                            tempgrouped(tgcount,3)=vmax;    
                        else
                            tempgrouped(tgcount,3)=tempgrouped(tgcount,3)+vmax;    
                        end;
                        winners_counter = winners_counter+1;
                    
                    else
                        if (vmax>vmaxold)
                            if (winners_counter>1)
                                winners_counter=winners_counter-1;
                            end;
                            vmaxsub=vmaxold;
                            tempwinsHR(winners_counter, 1) = counter+tempwins(xx,1);
                            tempwinsHR(winners_counter, 2) = counter+tempwins(xx,2);
                            vmaxold=vmax;
                            counterold=tempwinsHR(winners_counter,1);
   
                            if (onit==1)
                                %tempgrouped(tgcount,1)=tempwinsHR(winners_counter,1);
                                %tempgrouped(tgcount,2)=tempwinsHR(winners_counter,2);
                                tempgrouped(tgcount,3)=vmax;
                            end; 
                            tempgrouped(tgcount,3)=tempgrouped(tgcount,3)+vmax;
                            tempgrouped(tgcount,3)=tempgrouped(tgcount,3)-vmaxsub;
                            winners_counter = winners_counter+1;     
                        end;
                    end;
                end;
           
             end;
             %end;       
     counter = counter + width;
      
     
 end;

onit=0;
SDaxo = 0;
SDdag = 0;
shift_CCG = [];
counterold=-100;
vmax=0;
if (tally>2)
    dividetally=dividetally+1;
    totaltally=totaltally+tally;
   % tempgrouped(tgcount,3)=tempgrouped(tgcount,3)-vmaxmark;
    tempgrouped(tgcount,4)=tally;
   % tempgrouped(tgcount,5)=tempwinsHR((winners_counter-1), 1)-tempgrouped(tgcount,1);
    tempgrouped(tgcount,5)=(tempgrouped(tgcount,3)*tempgrouped(tgcount,3))/tally;
    tempgrouped(tgcount,6)=normize;
    tempgrouped(tgcount,7)=tempgrouped(tgcount,5)*normize;
    tgcount=tgcount+1;
end;
vmaxmark=0;
oldtally=tally;
tally=0;
xx=xx+1;
end;
end;
if(tgcount>1)
    if(oldtally<3)
        tempgrouped((tgcount-1),:)=[];
    end;
end;
winnerslist = tempwinsHR;
lengthHR=size(winnerslist,1);
sufname = ['H',num2str(pwidth)];
table = [table,sufname];
%result = [HRnamefile,'HRs'];
%HRtable=result;
%namefile=[HRnamefile,'HRI'];
%save (result, 'winnerslist','-ASCII');   %saves a table of all increments found to pass threshold_final
save (table, 'tempgrouped', '-ASCII');                                     


%clear

%end
