%
% Play with the spectrogram calculation for the spikes

display('We will now change the tapers and moving window parameters for the spike spectra');
display('Press return to continue');
pause
%
% Declares default values just in case they have been deleted
%
params.Fs=1000; % inverse of the spacing of the grid onw hcih tapers are computed
params.fpass=[0 100]; % frequency range of interest
params.pad=2; % pad
params.tapers=[5 9]; % tapers
params.err=0; % no eror computation
params.trialave=1; % average over trials

user_continues='y';
while strcmp(user_continues,'y')
    TW=input('Give me a time-bandwidth product between 1 and 10 ');  
    if ~isempty(TW);
       if TW>10; TW=3;display('Time-bandwidth product too high, using default, 3 '); end;
       if TW<1;  TW=3;display('Time-bandwidth product has to be greater than 1, using default, 3 '); end;
    else isempty(TW);
       display('User did not specify the time-bandwidth product, using defaults, 3');
       TW=3;
    end   
    params.tapers=[TW 2*TW-1];
    win=input('Give me a moving window size in seconds ');
    winstep=input('Give me the step size for the moving window ');
    if ~isempty(win) && ~isempty(winstep);
       if winstep>win; winstep=win/10;display('Step size has to smaller than duration, using default, step=window/10'); end;
       if win>=1; win=0.5;display('Do not use such a large duration, data is only 1 s long'); end;
    else;
       display('User did not specify one or more of the window parameters, using defaults [0.5 0.05]');
       win=0.5;
       winstep=0.05;
    end;
    movingwin=[win winstep];
    %
    % spectrogram of the first cell
    %
    data=dsp1t; % data from 1st cell
    [S,t,f,R]=mtspecgrampt(data,movingwin,params); % compute spectrogram
    %
    % Plot the spectrogram
    %
    figure; 
    subplot(211);
    plot_matrix(S./repmat(R,[1 size(S,2)]),t,f);xlabel([]);caxis([-5 6]);colorbar; % plot spectrogram normalized by rate
    set(gca,'FontName','Times New Roman','Fontsize', 14);
    title({['Spectrogram normalized by the rate: Cell 1, W=' num2str(params.tapers(1)/movingwin(1)) 'Hz'];['moving window = ' num2str(movingwin(1)) 's, step = ' num2str(movingwin(2)) 's']});
    ylabel('frequency Hz');
    %
    % spectrogram of second cell
    data=dsp2t; % data from 2nd cell
    [S,t,f,R]=mtspecgrampt(data,movingwin,params); % compute spectrogram
    %
    % Plot the spectrogram
    %
    subplot(212);
    plot_matrix(S./repmat(R,[1 size(S,2)]),t,f);caxis([-5 6]); colorbar; % plot spectrogram normalized by rate
    set(gca,'FontName','Times New Roman','Fontsize', 14);
    title({['Spectrogram normalized by the rate: Cell 2, W=' num2str(params.tapers(1)/movingwin(1)) 'Hz'];['moving window = ' num2str(movingwin(1)) 's, step = ' num2str(movingwin(2)) 's']});
    xlabel('time s'); ylabel('frequency Hz');
    position_figure
    user_continues=input('Do you want to continue to play ? Type y for Yes, and n for No ','s');
end