clear all;

files = dir('*.mat')



for ii = [1,3,5,7,9,11,13,15]
    load(files(ii).name);
    load(files(ii+1).name);

    popRate = ACh_popRate_noRip.popRate;
    IDX = round(ripples.timestamps*100);
    for kk = 1:length(ripples.peaks)
        popRate(IDX(kk,1):IDX(kk,2)) = min(popRate);
    end

    popRate_NREM = ACh_popRate_noRip.popNREM;
    IDX = round(ripples.timestamps*100);
    for kk = 1:length(ripples.peaks)
        popRate_NREM(IDX(kk,1):IDX(kk,2)) = min(popRate_NREM);
    end
    
%% save data
    datpath = 'G:\My Drive\Paper_ACh_OXT\Fig4\Figure 4C\ACh_popRate_noRip';
    ACh_popRate_noRip.signal = ACh_popRate_noRip.signal;
    ACh_popRate_noRip.lfp_fbtime = ACh_popRate_noRip.lfp_fbtime;
    ACh_popRate_noRip.popREM = ACh_popRate_noRip.popREM;
    ACh_popRate_noRip.popWAKE = ACh_popRate_noRip.popWAKE;
    ACh_popRate_noRip.popNREM = popRate_NREM;
    ACh_popRate_noRip.popRate = popRate;
    save(fullfile(datpath,[files(ii).name(1:18),'_ACh_popRate_noRip_renew']),'ACh_popRate_noRip')
    close all
    
end