% Set default line width, font size, figure color, and title font weight
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')

%% Get LFP data and perform spectrogram
basepath = pwd;
THLFPs = bz_GetLFP(58,'basepath',basepath,'noPrompts',true);
Nf = 1000;
frerange = linspace(0,250,Nf);
lfp = double(THLFPs.data);
lfp(abs(lfp)>5000) = nan;
lfp = fillmissing(lfp,'linear');
[ss,ft,tt] = spectrogram(lfp,2*THLFPs.samplingRate,0.6*THLFPs.samplingRate,frerange,THLFPs.samplingRate);

% Get optimal time and calculate start and end points
optitime = getopti_time();
dt = mean(diff(tt));
Nstart = ceil(optitime(1)/dt);
Nend = floor(optitime(2)/dt);

% Plot the spectrogram
tplot = tt(Nstart:Nend)-tt(Nstart);
ssplot = ss(:,Nstart:Nend);
bands = SpectrogramBands(abs(ssplot),frerange);
figure;
set(gcf,'Position',[100 100 1000 150])
colormap('parula')
imagesc(tplot,frerange,abs(ssplot));clim([0,2.5e5]);ylim([0 30])
yticks([0.5,20,50,100,200,250]); ylabel('Frequency (Hz)'); xlabel('Time (s)');
set(gca,'YDir','normal');
hold on
yyaxis right
line(tplot,12+bands.spindles/max(bands.spindles)*30,'Color','w','LineWidth',1)
xlim([2500,5000])
box off
exportgraphics(gcf,'Fig1B_Spectrogram.pdf','BackgroundColor','none','ContentType','vector')

%% save my plot with high resolution and use blue background
% set(gcf,'color','w');
% export_fig('Fig1B_Spectrogram.pdf','-painters','-r300')
    
% Get fluorescence data and sleep state
FluoData = Get_OxtAch_Fluos();
SleepState = Get_Aligned_Sleep();

% Load acceleration data
load('ACh_OXT004_007_230306_152436.accel.behavior.mat')
acc_fs = accel.samplingRate;
Nstart = ceil(optitime(1)*acc_fs);
Nend = floor(optitime(2)*acc_fs);
acc = accel.acceleration.motion(Nstart:Nend);
acc_t = accel.timestamps(Nstart:Nend)-optitime(1);

% Plot OXT and Ach data
figure
set(gcf,'Position',[100 100 1000 300])
plot(FluoData.time,FluoData.Oxt_dFF,'color',[0 0.5 0])
hold on
plot(FluoData.time,FluoData.Ach_dFF+0.025,'color',[1 0 0])
xlabel('Time (s)')
plot(SleepState.WAKE,0.065,'k|','MarkerSize',1);
plot(SleepState.NREM,0.07,'b|','MarkerSize',1);
plot(SleepState.REM,0.075,'m|','MarkerSize',1);
plot(acc_t,acc/peak2peak(acc)/40-0.04,'k');
xlim([2500,5000])
ylim([-0.06,0.08])
box off
exportgraphics(gcf,'Fig1B_AChOXT.pdf','BackgroundColor','none','ContentType','vector')