% UTP_AO_GETDOF a set of UTPs for the ao/getdof method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_getdof.m,v 1.3 2011/07/14 05:32:11 mauro Exp $
%

% <MethodDescription>
%
% The getdof method of the ao class computes the degrees of freedom for spectral estimate.
%
% </MethodDescription>

function results = utp_ao_getdof(varargin)
  
  % Check the inputs
  if nargin == 0
    
    addpath(fullfile(fileparts(which(mfilename)), 'reference_files'))
    
    % Some keywords
    class   = 'ao';
    mthd    = 'getdof';
    
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');
    
    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
    
    % reference dofs
    rdof = [32;34;38;44;52;59;62;63;63;63];
    
    % reference ao for dof test
    plrefdata = plist('fs', 1, 'nsecs', 1024, ...
           'tsfcn',  'randn(size(t))');
    refdata = ao(plrefdata);
    
    % reference overlap for dof test
    rolap = (0:10:90);
    
    
    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test against reference data

    
    disp('Done.');
    disp('******************************************************');
    
  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end
  
  
  %% UTP_01
  
  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01
    
    
    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    if stest
      % check we have minfo objects
      if isa(io, 'minfo')
        
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   Default PList
        if io(3).plists.nparams ~= 4, atest = false; end
        % Check key
        if ~io(3).plists.isparam('method'), atest = false; end
        if ~io(3).plists.isparam('DataLength'), atest = false; end
        if ~io(3).plists.isparam('conf'), atest = false; end
        if ~io(3).plists.isparam('dof'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('method'), 'psd'), atest = false; end
        if ~isequal(io(3).plists.find('DataLength'), paramValue.EMPTY_DOUBLE), atest = false; end
        if ~isequal(io(3).plists.find('conf'), 95), atest = false; end
        if ~isequal(io(3).plists.find('dof'), paramValue.EMPTY_DOUBLE), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('method'), {'psd','lpsd','mscohere','mslcohere'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('DataLength'), {paramValue.EMPTY_DOUBLE}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('conf'), {95}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('dof'), {paramValue.EMPTY_DOUBLE}), atest = false; end
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01
  
  %% UTP_06
  
  % <TestDescription>
  %
  % Tests that the getdof method properly applies history.
  %
  % </TestDescription>
  function result = utp_06
    
    % <SyntaxDescription>
    %
    % Test that the result of applying the getdof method can be processed back
    % to an m-file.
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
      plsp = plist('order',1,'navs',1);
      rsp = psd(refdata,plsp);
      pldof = plist('method','psd','DataLength',numel(refdata.y));
      out  = getdof(rsp,pldof);
      mout = rebuild(out);
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'getdof'.
    % 2) Check that the re-built object is the same object as 'out'.
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    if stest
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'getdof'), atest = false; end
      % Check the re-built object
      if ~eq(mout, out, ple2), atest = false; end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_06
  
  %% UTP_07
  
  % <TestDescription>
  %
  % Tests that the getdof method provides correct values for spectra
  % degrees of freedom.
  % Rreference on D B Percival and A T Walden, Spectral Analysis for
  % Physical Applications, pg. 293
  %
  % </TestDescription>
  function result = utp_07
    
    % <SyntaxDescription>
    %
    % Test that the applying psd works on a single AO.
    %
    % </SyntaxDescription>
    
    % <SyntaxCode>
    try
      dofs = zeros(numel(rolap),1);
      for jj=1:numel(rolap)
        plsp = plist('win','Hanning','order',1,'nfft',64,'olap',rolap(jj));
        axx = psd(refdata,plsp);

        df = getdof(axx,plist('method','psd','DataLength',numel(refdata.y)));
        dofs(jj) = df.y;
      end
      
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end
    % </SyntaxCode>
    
    % <AlgoDescription>
    %
    % 1) Check that output agrees with the reference values.
    %
    % </AlgoDescription>
    
    % <AlgoCode>
    atest = true;
    TOL = eps;
    
    if stest
      if ~isequal(dofs, rdof) || any((abs(dofs - rdof) ./ rdof) >= TOL)
        atest = false;
      end
    else
      atest = false;
    end
    % </AlgoCode>
    
    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07
  
  
  
  
end
