%% Ripple detection
clear
addpath \\mycloudpr4100\ivan\Ivan_analysis\Ivan\human-20190516T170723Z-001\human\FMAToolbox\Helpers
addpath  \\mycloudpr4100\ivan\Ivan_analysis\Ivan\human-20190516T170723Z-001\human\FMAToolbox\Analyses
addpath  \\mycloudpr4100\ivan\single_unit\human\wcst\awake_swr
addpath C:\HaoxinAnalyses\EMOP\Data\codes

% 
% addpath /Volumes/Ivan/Ivan_analysis/Ivan/human-20190516T170723Z-001/human/FMAToolbox/Helpers
% addpath /Volumes/Ivan/Ivan_analysis/Ivan/human-20190516T170723Z-001/human/FMAToolbox/Analyses
% addpath /Volumes/Ivan/single_unit/human/wcst/awake_swr
subnum = 0;
for sub = {'IR29','IR35','IR51','IR54','IR57' ,'IR75','IR84'}    

    clearvars -except subnum sub
    subnum=subnum+1;
    sub=char(sub)
    cd(['C:\HaoxinAnalyses\EMOP\Data\' sub])

    load([sub '_HaoxinROI.mat'])
    try
        MTL_roi.EC_Stan;
    catch
        MTL_roi.EC_Stan=[];
    end
    try
        MTL_roi.PRC_Stan;
    catch
        MTL_roi.PRC_Stan=[];
    end
    try
        MTL_roi.PHC_Stan;
    catch
        MTL_roi.PHC_Stan=[];
    end
    try
        MTL_roi.Sub_Stan;
    catch
        MTL_roi.Sub_Stan=[];
    end
    try
        MTL_roi.AMY_Stan;
    catch
        MTL_roi.AMY_Stan=[];
    end
    
    chan_ROI = unique([MTL_roi.CA1_Stan MTL_roi.CA3_Stan]);
%     chan_ROI = unique(MTL_roi.AMY_Stan)
    
    
    load([sub '.mat'])
    load([sub '_evnt_clean.mat'])
    load([sub '_evnt.mat'])
    load([sub '_log_EMOPSData.mat'])
    
    
    Fs=2000;
    raw_eeg_encoding = extractTrials(data_ecog,event_study_onsets(ok_epochs_study),chan_ROI,[-1 5]*Fs);
    raw_eeg_retrieval = extractTrials(data_ecog,event_test_onsets(ok_epochs_test),chan_ROI,[-1 5]*Fs);
    
    trials_time_axis_encoding = ( -Fs+1 : 5*Fs )'; trials_time_axis_encoding = repmat(trials_time_axis_encoding,1,size(raw_eeg_encoding,3));
    trials_time_axis_retrieval = ( -Fs+1 : 5*Fs )'; trials_time_axis_retrieval = repmat(trials_time_axis_retrieval,1,size(raw_eeg_retrieval,3));
    
    trials_ind_mat_encoding = repmat(ok_epochs_study,size(raw_eeg_encoding,2),1);
    trials_ind_mat_retrieval = repmat(ok_epochs_test,size(raw_eeg_retrieval,2),1);
    
    N_chanel = length(chan_ROI);
    for nch = 1:N_chanel
        tic
        %% Encoding
        lfp_task_range = reshape(raw_eeg_encoding(nch,:,:),1,[]);
        trials_time_axis = reshape(trials_time_axis_encoding,1,[]);
        trials_ind_mat = reshape(trials_ind_mat_encoding ,1,[]);
        [ripples_encoding] = detect_awake_ripple_haoxin(lfp_task_range, trials_time_axis, Fs);
        ripple_encoding_ch(nch).ripples=ripples_encoding; ripple_peaks_ind = round(ripples_encoding(:,2).*2000);
        for j = 1:numel(ripple_peaks_ind)
            if ripple_peaks_ind(j) > 2000 & (ripple_peaks_ind(j) + 2000) < length(lfp_task_range)
                ripple_encoding_ch(nch).ripple_traces(j,:) = lfp_task_range((ripple_peaks_ind(j)-2000):(ripple_peaks_ind(j)+2000));
                ripple_encoding_ch(nch).trial_time(j) = trials_time_axis(ripple_peaks_ind(j));
                ripple_encoding_ch(nch).trial(j) = trials_ind_mat(ripple_peaks_ind(j));
            else
                ripple_encoding_ch(nch).ripple_traces(j,1:4001) = NaN;
                ripple_encoding_ch(nch).trial_time(j) = NaN;
                ripple_encoding_ch(nch).trial(j) = NaN;
            end
        end
        
        %% Retrieval
        lfp_task_range = reshape(raw_eeg_retrieval(nch,:,:),1,[]);
        trials_time_axis = reshape(trials_time_axis_retrieval,1,[]);
        trials_ind_mat = reshape(trials_ind_mat_retrieval ,1,[]);
        [ripples_retrieval] = detect_awake_ripple_haoxin(lfp_task_range, trials_time_axis, Fs);
        ripple_retrieval_ch(nch).ripples=ripples_retrieval; ripple_peaks_ind = round(ripples_retrieval(:,2).*2000);
        for j = 1:numel(ripple_peaks_ind)
            if ripple_peaks_ind(j) > 2000 & (ripple_peaks_ind(j) + 2000) < length(lfp_task_range)
                ripple_retrieval_ch(nch).ripple_traces(j,:) = lfp_task_range((ripple_peaks_ind(j)-2000):(ripple_peaks_ind(j)+2000));
                ripple_retrieval_ch(nch).trial_time(j) = trials_time_axis(ripple_peaks_ind(j));
                ripple_retrieval_ch(nch).trial(j) = trials_ind_mat(ripple_peaks_ind(j));
            else
                ripple_retrieval_ch(nch).ripple_traces(j,1:4001) = NaN;
                ripple_retrieval_ch(nch).trial_time(j) = NaN;
                ripple_retrieval_ch(nch).trial(j) = NaN;
            end
        end
        
        
        toc
    end
    
%     
%     save(['R00_Output_RippleDetection_' sub],'ripple_encoding_ch','ripple_retrieval_ch','chan_ROI','MTL_roi',...
%         'trials_time_axis_encoding','trials_time_axis_retrieval')
%     
end


rmpath  \\mycloudpr4100\ivan\Ivan_analysis\Ivan\human-20190516T170723Z-001\human\FMAToolbox\Helpers
rmpath \\mycloudpr4100\ivan\Ivan_analysis\Ivan\human-20190516T170723Z-001\human\FMAToolbox\Analyses
