clear all;clc;

filename = dir(['*_OXT_Arti_ripples_dFoverF_Ripple_sleepstate.mat']);

NREM_ArtRipple_OXT = [];
WAKE_ArtRipple_OXT = [];
REM_ArtRipple_OXT = [];

NREM_Ripple_OXT = [];
WAKE_Ripple_OXT = [];
REM_Ripple_OXT = [];

for ii = 1:length(filename)

    load(filename(ii).name);
    if ii > 5
        dFoverF_SleepState.NREM_ArtRipple = resample(dFoverF_SleepState.NREM_ArtRipple', 1001, 1301)';
        dFoverF_SleepState.WAKE_ArtRipple = resample(dFoverF_SleepState.WAKE_ArtRipple', 1001, 1301)';
        dFoverF_SleepState.REM_ArtRipple = resample(dFoverF_SleepState.REM_ArtRipple', 1001, 1301)';
        if isempty(dFoverF_SleepState.NREM_Ripple) == 0
            dFoverF_SleepState.NREM_Ripple = resample(dFoverF_SleepState.NREM_Ripple', 1001, 1301)';
        end 

        if isempty(dFoverF_SleepState.WAKE_Ripple) == 0
            dFoverF_SleepState.WAKE_Ripple = resample(dFoverF_SleepState.WAKE_Ripple', 1001, 1301)';
        end
        
        if isempty(dFoverF_SleepState.REM_Ripple) == 0
            dFoverF_SleepState.REM_Ripple = resample(dFoverF_SleepState.REM_Ripple', 1001, 1301)';
        end
    end

        

    NREM_ArtRipple_OXT = [NREM_ArtRipple_OXT;zscore(mean(dFoverF_SleepState.NREM_ArtRipple,1))];
    WAKE_ArtRipple_OXT = [WAKE_ArtRipple_OXT;zscore(mean(dFoverF_SleepState.WAKE_ArtRipple,1))];
    REM_ArtRipple_OXT = [REM_ArtRipple_OXT;zscore(mean(dFoverF_SleepState.REM_ArtRipple,1))];

    NREM_Ripple_OXT = [NREM_Ripple_OXT;zscore(mean(dFoverF_SleepState.NREM_Ripple,1))];
    WAKE_Ripple_OXT = [WAKE_Ripple_OXT;zscore(mean(dFoverF_SleepState.WAKE_Ripple,1))];
    REM_Ripple_OXT = [REM_Ripple_OXT;zscore(mean(dFoverF_SleepState.REM_Ripple,1))];

end

%%
figure;
set(gcf,'Position',[100 100 1200 600])
subplot(2,3,1)
x_axis = [1:size(NREM_ArtRipple_OXT,2)]./100-5;
plot_areaerrorbar_v2(smoothdata(NREM_ArtRipple_OXT,2,'movmean',30),'x_axis',x_axis,'color_line',[0,0,1]);
xlim([-5 5]);box off
title('NREM_ArtRipple_OXT');ylim([-2.5 2]);
xline(0,'r--');
% exportgraphics(gcf,['NREM_ArtRipple_OXT.pdf'],'BackgroundColor','none','ContentType','vector');


subplot(2,3,2);
x_axis = [1:size(NREM_ArtRipple_OXT,2)]./100-5;
plot_areaerrorbar_v2(smoothdata(WAKE_ArtRipple_OXT,2,'movmean',30),'x_axis',x_axis,'color_line',[0,0,1]);
xlim([-5 5]);box off
title('WAKE_ArtRipple_OXT');ylim([-2.5 2]);xline(0,'r--');

% exportgraphics(gcf,['WAKE_ArtRipple_OXT.pdf'],'BackgroundColor','none','ContentType','vector');


subplot(2,3,3)
x_axis = [1:size(NREM_ArtRipple_OXT,2)]./100-5;
plot_areaerrorbar_v2(smoothdata(REM_ArtRipple_OXT,2,'movmean',30),'x_axis',x_axis,'color_line',[0,0,1]);
xlim([-5 5]);box off
title('REM_ArtRipple_OXT');ylim([-2.5 2]);xline(0,'r--');

% exportgraphics(gcf,['REM_ArtRipple_OXT.pdf'],'BackgroundColor','none','ContentType','vector');

subplot(2,3,4)
x_axis = [1:size(NREM_ArtRipple_OXT,2)]./100-5;
plot_areaerrorbar_v2(smoothdata(NREM_Ripple_OXT,2,'movmean',30),'x_axis',x_axis,'color_line',[0,0.5,0]);
xlim([-5 5]);box off
title('NREM_Ripple_OXT');ylim([-2.5 2]);xline(0,'r--');

% exportgraphics(gcf,['NREM_Ripple_OXT.pdf'],'BackgroundColor','none','ContentType','vector');


subplot(2,3,5)
x_axis = [1:size(WAKE_Ripple_OXT,2)]./100-5;
plot_areaerrorbar_v2(smoothdata(WAKE_Ripple_OXT,2,'movmean',30),'x_axis',x_axis,'color_line',[0,0.5,0]);
xlim([-5 5]);box off
title('WAKE_Ripple_OXT');ylim([-2.5 2]);xline(0,'r--');

 % exportgraphics(gcf,['Artifical_Ripple_OXT.pdf'],'BackgroundColor','none','ContentType','vector');


% figure;
% options.color_area = [200 0 0]./255;
% options.color_line = [200 0 0]./255;
% options.alpha      = 0.2;
% options.line_width = 2;
% options.error      = 'sem';
% options.x_axis = [1:size(REM_Ripple_OXT,2)]./100-5;
% plot_areaerrorbar(smoothdata(REM_Ripple_OXT,2,'movmean',30), options);set(gcf,'Position',  [100, 100, 400, 300]);
% xlim([-2 5]);box off
% title('REM_Ripple_OXT');
% exportgraphics(gcf,['REM_Ripple_OXT.pdf'],'BackgroundColor','none','ContentType','vector');


