clear all;clc
files = dir(pwd);

dfolders = files([files(:).isdir]) 
dfolders = dfolders(~ismember({dfolders(:).name},{'.','..'}))
RipCh = [11,23,8,8]
for ii = 1:length(dfolders)-1
    filepath = 'H:\ACh_OXT_Paper_revision_experiment\Ripple_induced_session\Control' ;
    cd([filepath,filesep, dfolders(ii).name])

    basepath = pwd;
    basename = bz_BasenameFromBasepath(basepath);
    load([basename,'.EMGFromLFP.LFP.mat']);
    load([basename,'.SleepScoreLFP.LFP.mat']);
    load([basename,'.SleepState.states.mat']);
    if exist('*.ripples.events.mat')~=0
        load([basename,'.ripples.events.mat']);
    else
        ripples =  bz_FindRipples(basepath, RipCh(ii));
    end


    info = read_Intan_RHD2000_file(basepath);
    digitalin = bz_getDigitalIn;
    lfp_time = digitalin.intsPeriods{1,2};
    delete digital
    % sitmulation and signal
    if exist('dFoverF.mat')==0
        name = basename(1:14);
        ext = '.mat'
        load([name,ext],['OpT_TTL']);
        ACh_sti = eval(['OpT_TTL']);
        ACh_sti.times = ([1:ACh_sti.length].*ACh_sti.interval)';
        fbtime = ACh_sti.times(find(ACh_sti.values(:,1)>1000,1));fbtime(1,2) = ACh_sti.times(find(ACh_sti.values(:,1)>1000,1,'last'));
        fbIDX = round(fbtime*10000);
        load([name,ext],['UF_OXT']);
        OXT_signal = eval(['UF_OXT']);  
        OXT_UF= OXT_signal.values((fbIDX(1,1)):(fbIDX(1,2)));
        times = ([1:length(OXT_UF)]./10000)';
        
        load([name,ext],['Stim_OXT']);
        ref_signal = eval(['Stim_OXT']);
        OXT_UF_ref = ref_signal.values((fbIDX(1,1)):(fbIDX(1,2)));

        load([name,ext],['Em_OXT']);
        OXT_Em_signal = eval(['Em_OXT']);
        OXT_Em = double(OXT_Em_signal.values((fbIDX(1,1)):(fbIDX(1,2))));

    
   
    else load('dFoverF.mat')
    end
    Sp2.OXT_Em  = OXT_Em ;
    Sp2.OXT_UF_ref = OXT_UF_ref;
    Sp2.times = times;
    Sp2.OXT_UF = OXT_UF;
    Sp2.lfp_time = lfp_time;
    Sp2.SleepState = SleepState;
    Sp2.ripples = ripples;
    datpath = 'G:\My Drive\Papers_Buzsaki\Paper_ACh_OXT\RevisionFigures\Induced_Ripple'
    save(fullfile(datpath,[basename,'_OXT_NoArti_ripples.mat']),'EMGFromLFP','Sp2');

end

cd(datpath)



