%% Average over trials

display('To compute spectrum for all trials, press return');
pause
%
% Compute spectrum for all trials
%
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
[S,f]=mtspectrumc(data,params); % ok
display('The spectrum now has dimensions');
whos S

display('To average over trials, press return');
pause
%
% Compute spectrum averaged over trials
%
params.trialave=1; % setting this to 1 averages over trials
[S,f]=mtspectrumc(data,params);
display('After averaging over trials, the spectrum has dimensions');
whos S

display('To plot, press return');
pause
%
% Plot the average spectrum
%
figure;
plot_vector(S,f,[],[],'y');
set(gca,'FontName','Times New Roman','Fontsize', 16);
xlabel('frequency Hz'); ylabel('Spectrum dB');
title(['LFP spectrum averaged across trials  W=' num2str(params.tapers(1)/size(data,1)*params.Fs) 'Hz']);
position_figure