%% LFP spectrogram

display('To examine spectrograms for the first two LFP channels, press return');
pause
%
% Preliminary parameter settings
%
movingwin=[0.5 0.05]; % set the moving window dimensions
params.Fs=1000; % sampling frequency
params.fpass=[0 100]; % frequency of interest
params.tapers=[5 9]; % tapers
params.trialave=1; % average over trials
params.err=0; % no error computation
%
% Compute spectrogram
%
data=dlfp1t; % data from channel 1
[S1,t,f]=mtspecgramc(data,movingwin,params); % compute spectrogram
figure; 
subplot(221);
plot_matrix(S1,t,f); xlabel([]); % plot spectrogram
caxis([8 28]); colorbar;
set(gca,'FontName','Times New Roman','Fontsize', 14);
title({['LFP 1,  W=' num2str(params.tapers(1)/movingwin(1)) 'Hz']; ['moving window = ' num2str(movingwin(1)) 's, step = ' num2str(movingwin(2)) 's']});
ylabel('frequency Hz');
%
% Compute spectrogram
%
data=dlfp2t; % data from channel 2
[S2,t,f]=mtspecgramc(data,movingwin,params); % compute spectrogram
subplot(222);
plot_matrix(S2,t,f); xlabel([]);% Plot spectrogram
caxis([8 28]); colorbar;
set(gca,'FontName','Times New Roman','Fontsize', 14);
title({['LFP 2,  W=' num2str(params.tapers(1)/movingwin(1)) 'Hz']; ['moving window = ' num2str(movingwin(1)) 's, step = ' num2str(movingwin(2)) 's']});
ylabel('frequency Hz');
position_figure

%% Deviation of instantaneous spectrum from the baseline

display('To also display deviation of the instantaneous power from the baseline, press enter');
pause
S1b=S1(1,:); % spectrum in the first movingwindow taken to be baseline
subplot(223); plot_matrix(S1./repmat(S1b,[size(S1,1) 1]),t,f);caxis([-5 10]);colorbar; % normalization of spectrogram
set(gca,'FontName','Times New Roman','Fontsize', 14);
title('Deviation from baseline');
xlabel('time s');
ylabel('frequency Hz');

S2b=S2(1,:); % spectrum in the first movingwindow taken to be baseline
subplot(224); plot_matrix(S1./repmat(S1b,[size(S1,1) 1]),t,f);caxis([-5 10]);colorbar;  % normalization of spectrogram
set(gca,'FontName','Times New Roman','Fontsize', 14);
title('Deviation from baseline');
xlabel('time s');
ylabel('frequency Hz');
position_figure
