pspwavename='rmean_tenminilandec_190sec';
fina='tenminilandec_190sec';
length_PSP=30;pspthresh=90;
%PSP_finder(fina,pspwavename,length_PSP,pspthresh);

load('tenminilandec_190sec.mat');
load('tenminilandec_190sec_psptimes90.mat');

f=timewave/1000;

trace=tenminilandec_190sec;

% compute template match similarities for 1 sec motif beginning
% with 1st PSP

%ln=1000;   % (length of traces to compare, here 1000=1 sec)

%t1=timewave(1);
%x=tenminilandec_190sec(t1:(t1+ln-1));
%s1=std(x);

%if 0
%for a=t1+1000:max( size(trace) )
%
%    f1(a)=(x-mean(x))' * ( trace(a:a+ln-1)-mean(trace(a:a+ln-1)) )/ln;
%    f1(a)=f1(a)/( s1 * std( trace(a:a+ln-1)) );
%
%end
%end

align_max=20;   % try re-aligning by as much as align_max (msecs)
                % to maximize the correlation


for i=1:1 %max(size(timewave))
    
    if timewave(i)+ln-1 <= max(size(trace))
        x=trace( timewave(i):timewave(i)+ln-1 );
    else
        break
    end
    s1=std(x);
    
    for j=i+1:max(size(timewave))

        if timewave(j)+ln-1 <= max(size(trace))
            y=trace( timewave(j):timewave(j)+ln-1 );
        else
            break
        end
        
        ccorr(i,j) = (x-mean(x))' * ( y-mean(y) )/ln;
        ccorr(i,j) = ccorr(i,j)/( s1*std(y));
        
        %  take corr from best re-alignment
        
        if j>1  % don't let it move into a region of another PSP-triggered song
            a0=max(timewave(j)-align_max,timewave(j-1)+align_max+1);
        else
            a0=timewave(j)-align_max;
        end
        a0=max(a0,1);  % .. or out of trace
        
        if j<max(size(timewave))  % same on other side
            a1=min( timewave(j)+align_max,timewave(j+1)-align_max-1);
        else
            a1=timewave(j)+align_max;
        end
        a1=min(a1,max(size(trace))-ln+1);
        
        ccorr_align(i,j)=-2;
        for t_align=a0:a1
            
            y=trace( t_align:t_align+ln-1 );
                
            ccorr_temp = (x-mean(x))' * ( y-mean(y) )/ln;
            ccorr_temp = ccorr_temp/( s1*std(y));
            
            if ccorr_temp>ccorr_align(i,j)
                ccorr_align(i,j)=ccorr_temp;
                align(i,j)=t_align-timewave(j);
            end
                
        end

    end   % for j
    i
end  % for i


% check out alignment procedure
i=1;
for j=2:1337
    
  if ccorr(i,j)>=.45  
    
    clf
    subplot(2,1,1)
    x=trace( timewave(i):timewave(i)+ln-1 );
    y=trace( timewave(j):timewave(j)+ln-1 );
    plot(x)
    hold on
    plot(y,'r')
    title( [ 'corr ' num2str( ccorr(i,j) ) ] )
    
    subplot(2,1,2)
    y=trace( timewave(j)+align(i,j):timewave(j)+align(i,j)+ln-1 );
    plot(x), hold on, plot(y,'r')
    title( [ 'align ' num2str( align(i,j) ) ' corr ' num2str( ccorr_align(i,j) ) ] )
    
    pause
    
  end 
    
end



return

clf
i=223;  j=838;
x=trace( timewave(i):timewave(i)+ln-1 );
y=trace( timewave(j):timewave(j)+ln-1 );
plot(x)
hold on
plot(y,'r')
timewave(i)
timewave(j)





clf

% subplot(3,1,1)
% plot(0:.001:188.643,f1)
% 
% subplot(3,1,2)
% plot(0:.001:189.642,tenminilandec_190sec,'r')
% 
% subplot(3,1,3)
% plot( x) 
% a=axis; axis([ a(1) a(2) -100 -20])


% clf
% plot(0:.001:189.642,tenminilandec_190sec), hold on
% 
% 
% f=timewave/1000;
% x=zeros( size(f,1),1 );
% r0=min( tenminilandec_190sec ); r1= max( tenminilandec_190sec );
% %hl=line( repmat(f',2,1),[ r0+x' ; r0+((r1-r0)*.2)+x' ] );
% hl=line( repmat(f',2,1),[ -100+x' ; -95+x' ] );
% 
% pause
% 

 for cc=0:1:199    
 
     subplot(3,1,1)
     axis([cc cc+1 -1 1 ])
     
     subplot(3,1,2)
     axis([cc cc+1 -100 -20])
     
%     
%     %plot(cc:.001:cc+1-.001,tenminilandec(t0:t1))
%     %hold on
%     %plot(t0:t1,threshwave(t0:t1),'r')
%     
%     %r0=min( sample(t0:t1) ); r1=max( sample(t0:t1) );
%         
%     % display psp detected....
%     %f=timewave(find(timewave<=t1 & timewave>=t0));
%     %x=zeros( size(f,1),1 );
%     
%     %hold on
%     
%     %hl=line( repmat(f',2,1),[ r0+x' ; r0+((r1-r0)*.2)+x' ] );
% 
     pause
     
 end
