% Code to flatten PSP traces away from detected PSP's as in Ikegaya et al., 2004

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');


% Flatten the trace away from detected PSP's
% Use length_PSP as length of PSP's to preserve...

flat_trace=-92+zeros( size(tenminilandec_190sec) );

for i=1:numel(timewave)
    flat_trace(timewave(i):timewave(i)+length_PSP )=tenminilandec_190sec( timewave(i):timewave(i)+length_PSP );
end
    
    
clf


plot(0:.001:189.642,tenminilandec_190sec), hold on
plot(0:.001:189.642,flat_trace,'r')


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    

    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
