% Code for running the Lampl data on an LRI shuffle test

% 0. Set Parameters
% 1. Load Data, Set Parameters
% 2. Extract PSP's and Start Times
% 3. Compute LRI on original flattened trace
% 4. Shuffle k times, compute LRI's, store


% 0. Parameters

    % (for ref., data is 1 kHz sampling rate)

    pspwavename='rmean_tenminilandec_190sec';
    fina='tenminilandec_190sec';
    length_PSP=30;pspthresh=90;  % parms for PSP finder & PSP extraction

    num_surrogates=100;
    baseline=-92;
    
    
% 1. Load data

    PSP_finder(fina,pspwavename,length_PSP,pspthresh);

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

    vtrace=tenminilandec_190sec;   % "voltage trace"
    length_flat_trace=numel(vtrace);
    
% 2. Extract PSP's

    PSP_start=zeros(size(timewave));
    for j=1:max(size(timewave))
        PSP{j}=vtrace( timewave(j):timewave(j)+length_PSP-1 );
        PSP_start(j)=timewave(j);
    end

% 3. Compute LRI on original flattened trace

    
    flat_trace=ConstructFlatTrace( PSP,PSP_start,length_flat_trace,baseline );
    flat_trace_orig=flat_trace;
        
    savename='flat_trace30';
    flat_trace30=flat_trace;

    save( savename,'flat_trace30');

    %LRI_find_orig=HRI_repeatfinder_1secAA(savename);

% 4.     
    
    for cc=1:num_surrogates
        
        PSP_start_perm=ShufflePSPStart( PSP_start );
        flat_trace_surr{cc}=ConstructFlatTrace( PSP,PSP_start_perm,length_flat_trace,baseline );
        flat_trace30=flat_trace_surr{cc};
        
        save( savename,'flat_trace30')
              
        LRI_find_surr{cc}=HRI_repeatfinder_1secAA(savename);
        
        save LRIShuffSurr flat_trace_surr LRI_find_surr flat_trace_orig LRI_find_orig
        
    end
        

    
    