

set(0, 'DefaultLineLineWidth', 2);
set(0,'defaultTextFontSize', 12);
set(0,'defaultAxesFontSize',12);
set(0,'DefaultFigureColormap',jet)
set(0,'defaultfigurecolor',[1 1 1])
set(0,'DefaultAxesTitleFontWeight', 'normal')
set(0, 'DefaultAxesFontName', 'Arial');
set(0, 'DefaultLineLineWidth', 1.5);

%%
clear all
animalNames = {'LIMP013';
    'LIMP014';
    'LIMP019'};
sessionCount = 0;
edges = 1:1e3;
bins = 0.5*(edges(1:end-1) + edges(2:end));
for anID = 1:length(animalNames)
    Names_CNO = dir([animalNames{anID},'*.mat']);
    filenamesCNO = {Names_CNO.name};
    foldernameCNO = {Names_CNO.folder};
    for cnID = 1:size(Names_CNO,1)
        sessionCount = sessionCount + 1
        load(fullfile(foldernameCNO{cnID},filenamesCNO{cnID}));

        MA  = nNREM_Data.MA;
        dt = 1;
        tedges = -dt/2:dt:max(MA.times)+dt/2;
        tcenter = 0:dt:max(MA.times);
        prob_c1 = smoothdata(histcounts(MA.peaks,tedges),'gaussian',10);
        %figure
        disp(sum(prob_c1))
        %plot(prob_c1)

        [Pxx,f] = pwelch(prob_c1,1e3,5e2,512,1/dt);

        Pall(:,sessionCount) = Pxx;
        IAI_hist = smoothdata(histcounts(diff(MA.peaks),edges),'gaussian',10);
        IAI_hist_normalized(:,sessionCount) = IAI_hist/sum(IAI_hist);
        
    end

end

%
fig1 = figure();
options.handle     =  fig1;
set(gcf,'Position',[100 100 600 700])
options.color_area = [255 0 0]./255;    
options.color_line = [255 0 0]./255;
options.alpha      = 0.2;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = f(5:end);
subplot(2,1,1)
plot_areaerrorbar(Pall(5:end,:)',options)
set(gca,'XScale', 'log')
xlabel('Frequency (Hz)')
box off
subplot(2,1,2)
options.x_axis = bins;
options.color_area = [0 0 0]./255;    
options.color_line = [0 0 0]./255;
plot_areaerrorbar(IAI_hist_normalized',options)
set(gca,'XScale', 'log')
xlim([1,1e3])
ylim([0,0.03])
xlabel('Time (s)')
box off
exportgraphics(gcf,'LIMP_MA_Psd.pdf','BackgroundColor','none','ContentType','vector')


%%
clear all
animalNames = {'OXYT010';
    'OXYT011';
    'OXYT013';
    'OXYT014'};
sessionCount = 0;
edges = 1:1e3;
bins = 0.5*(edges(1:end-1) + edges(2:end));
for anID = 1:length(animalNames)
    Names_CNO = dir([animalNames{anID},'*.mat']);
    filenamesCNO = {Names_CNO.name};
    foldernameCNO = {Names_CNO.folder};
    for cnID = 1:size(Names_CNO,1)
        sessionCount = sessionCount + 1
        load(fullfile(foldernameCNO{cnID},filenamesCNO{cnID}));

        MA  = nNREM_Data.MA;
        dt = 1;
        tedges = -dt/2:dt:max(MA.times)+dt/2;
        tcenter = 0:dt:max(MA.times);
        prob_c1 = smoothdata(histcounts(MA.peaks,tedges),'gaussian',10);
        disp(sum(prob_c1))
        [Pxx,f] = pwelch(prob_c1,1e3,5e2,512,1/dt);

        Pall(:,sessionCount) = Pxx;
        IAI_hist = smoothdata(histcounts(diff(MA.peaks),edges),'gaussian',10);
        IAI_hist_normalized(:,sessionCount) = IAI_hist/sum(IAI_hist); 
    end
end

%
fig1 = figure();
options.handle     =  fig1;
set(gcf,'Position',[100 100 600 700])
options.color_area = [255 0 0]./255;    
options.color_line = [255 0 0]./255;
options.alpha      = 0.2;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = f(5:end);
subplot(2,1,1)
plot_areaerrorbar(Pall(5:end,:)',options)
set(gca,'XScale', 'log')
xlabel('Frequency (Hz)')
box off
subplot(2,1,2)
options.x_axis = bins;
options.color_area = [0 0 0]./255;    
options.color_line = [0 0 0]./255;
plot_areaerrorbar(IAI_hist_normalized',options)
set(gca,'XScale', 'log')
xlim([1,1e3])
ylim([0,0.03])
xlabel('Time (s)')
box off
exportgraphics(gcf,'OXYT_MA_Psd.pdf','BackgroundColor','none','ContentType','vector')

%% ALL  animals together
clear all
animalNames = {'LIMP013';
    'LIMP014';
    'LIMP019'};
sessionCount = 0;
edges = 1:1e3;
bins = 0.5*(edges(1:end-1) + edges(2:end));
for anID = 1:length(animalNames)
    Names_CNO = dir([animalNames{anID},'*.mat']);
    filenamesCNO = {Names_CNO.name};
    foldernameCNO = {Names_CNO.folder};
    for cnID = 1:size(Names_CNO,1)
        sessionCount = sessionCount + 1
        load(fullfile(foldernameCNO{cnID},filenamesCNO{cnID}));

        MA  = nNREM_Data.MA;
        dt = 1;
        tedges = -dt/2:dt:max(MA.times)+dt/2;
        tcenter = 0:dt:max(MA.times);
        prob_c1 = smoothdata(histcounts(MA.peaks,tedges),'gaussian',10);
        %figure
        disp(sum(prob_c1))
        %plot(prob_c1)

        [Pxx,f] = pwelch(prob_c1,1e3,5e2,512,1/dt);

        Pall(:,sessionCount) = Pxx;
        IAI_hist = smoothdata(histcounts(diff(MA.peaks),edges),'gaussian',10);
        IAI_hist_normalized(:,sessionCount) = IAI_hist/sum(IAI_hist);
        
    end

end


animalNames2 = {'OXYT010';
    'OXYT011';
    'OXYT013';
    'OXYT014'};
edges = 1:1e3;
for anID = 1:length(animalNames2)
    Names_CNO = dir([animalNames2{anID},'*.mat']);
    filenamesCNO = {Names_CNO.name};
    foldernameCNO = {Names_CNO.folder};
    for cnID = 1:size(Names_CNO,1)
        sessionCount = sessionCount + 1
        load(fullfile(foldernameCNO{cnID},filenamesCNO{cnID}));

        MA  = nNREM_Data.MA;
        dt = 1;
        tedges = -dt/2:dt:max(MA.times)+dt/2;
        tcenter = 0:dt:max(MA.times);
        prob_c1 = smoothdata(histcounts(MA.peaks,tedges),'gaussian',10);
        disp(sum(prob_c1))
        [Pxx,f] = pwelch(prob_c1,1e3,5e2,512,1/dt);

        Pall(:,sessionCount) = Pxx;
        IAI_hist = smoothdata(histcounts(diff(MA.peaks),edges),'gaussian',10);
        IAI_hist_normalized(:,sessionCount) = IAI_hist/sum(IAI_hist); 
    end
end

%%
fig1 = figure();
options.handle     =  fig1;
set(gcf,'Position',[100 100 600 700])
options.color_area = [255 0 0]./255;    
options.color_line = [255 0 0]./255;
options.alpha      = 0.2;
options.line_width = 2;
options.error      = 'sem';
options.x_axis = f(5:end);
subplot(2,1,1)
plot_areaerrorbar(Pall(5:end,:)',options)
set(gca,'XScale', 'log')
xlabel('Frequency (Hz)')
box off
subplot(2,1,2)
options.x_axis = bins;
options.color_area = [0 0 0]./255;    
options.color_line = [0 0 0]./255;
plot_areaerrorbar(IAI_hist_normalized',options)
set(gca,'XScale', 'log')
xlim([1,1e3])
ylim([0,0.03])
xlabel('Time (s)')
box off
exportgraphics(gcf,'AllAnimals_MA_Psd.pdf','BackgroundColor','none','ContentType','vector')
