%% Error bars for lfp spectra

display('To compute error bars, press return');
pause
delay_samples=1001:2000; % delay period sample numbers
data=dlfp1t(delay_samples,:); % data for all trials in delay period
params.Fs=1000; % sampling frequency
params.fpass=[0 100]; % frequency range of interest
params.pad=2; % set the pad factor
params.tapers=[10 19]; % set the tapers
params.trialave=1;% average over trial
p=0.05; % p value for error bars
%
% Compute spectrum with chi2 errors
%
params.err=[1 p]; % specification for chi2 error bars
[S,f,Serr]=mtspectrumc(data,params);
%
% Plot spectra and error bars
%
plot_vector(S,f,[],Serr,'y', 1.5);
set(gca,'FontName','Times New Roman','Fontsize', 16);
xlabel('frequency Hz'); ylabel('Spectrum dB');
%
% Compute jackknife eror bars
%
params.err=[2 p]; % Jackknife error
[S,f,Serr]=mtspectrumc(data,params);
%
% Plot jackknife error bars
%
hold on; 
plot(f,10*log10(Serr(1,:)),'m',f,10*log10(Serr(2,:)),'m');
title(['LFP spectrum averaged across trials with error bars (yellow:\chi_{2\nu}, magenta:jackknife) W=' num2str(params.tapers(1)/size(data,1)*params.Fs) 'Hz']);
position_figure