% UTP_AO_WHITEN1D a set of UTPs for the ao/whiten1D method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_whiten1D.m,v 1.24 2011/04/18 16:57:44 ingo Exp $
%

% <MethodDescription>
%
% The whiten1D method of the ao class computes the whiten1Dolute value of the y
% and/or x data.
%
% </MethodDescription>

function results = utp_ao_whiten1D(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'whiten1D';

    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = get_test_objects_ao;

    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();
    
    % add useful params to standard exceptions
    ple3 = plist('Exceptions', {'history', 'created', 'UUID', 'param/desc', 'name', 'procinfo'});

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test with mixed input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the modify call works
    results = [results utp_08];    % Test input data shape == output data shape
    results = [results utp_09];    % Test output of the data
    results = [results utp_10];    % Test flatness capability - no model input
    results = [results utp_11];    % Test flatness capability - model input

    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>

    try
      % <SyntaxCode>
      % 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
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        prefs = getappdata(0, 'LTPDApreferences');
        defaultWinType = char(prefs.getMiscPrefs.getDefaultWindow);
        
        %%% 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 ~any(strcmpi(io(2).sets, 'psd')), atest = false; end
        if ~any(strcmpi(io(2).sets, 'lpsd')), atest = false; end
        %%%%%%%%%%   SET 'Default'        
        pn = 3;
        if io(pn).plists.nparams ~= 15, atest = false; end        
        % Check key
        if ~io(pn).plists.isparam('model'), atest = false; end
        if ~io(pn).plists.isparam('range'), atest = false; end
        if ~io(pn).plists.isparam('fs'), atest = false; end
        if ~io(pn).plists.isparam('complete_hf'), atest = false; end        
        if ~io(pn).plists.isparam('maxiter'), atest = false; end
        if ~io(pn).plists.isparam('poletype'), atest = false; end
        if ~io(pn).plists.isparam('minorder'), atest = false; end
        if ~io(pn).plists.isparam('maxorder'), atest = false; end
        if ~io(pn).plists.isparam('weights'), atest = false; end
        if ~io(pn).plists.isparam('plot'), atest = false; end
        if ~io(pn).plists.isparam('disp'), atest = false; end
        if ~io(pn).plists.isparam('msevartol'), atest = false; end
        if ~io(pn).plists.isparam('fittol'), atest = false; end
        if ~io(pn).plists.isparam('flim'), atest = false; end
        if ~io(pn).plists.isparam('scaleout'), atest = false; end
        
        % Check default value
        if ~isEmptyDouble(io(pn).plists.find('model')), atest = false; end
        if ~isequal(io(pn).plists.find('range'), []), atest = false; end
        if ~isequal(io(pn).plists.find('fs'), []), atest = false; end
        if ~isequal(io(pn).plists.find('complete_hf'), 'flat'), atest = false; end
        if ~isequal(io(pn).plists.find('maxiter'), 30), atest = false; end
        if ~isequal(io(pn).plists.find('poletype'), 1), atest = false; end
        if ~isequal(io(pn).plists.find('minorder'), 2), atest = false; end
        if ~isequal(io(pn).plists.find('maxorder'), 25), atest = false; end
        if ~isequal(io(pn).plists.find('weights'), '1/abs'), atest = false; end
        if ~isequal(io(pn).plists.find('plot'), false), atest = false; end
        if ~isequal(io(pn).plists.find('disp'), false), atest = false; end
        if ~isequal(io(pn).plists.find('msevartol'), .1), atest = false; end
        if ~isequal(io(pn).plists.find('fittol'), .01), atest = false; end
        if ~isequal(io(pn).plists.find('flim'), [1e-3 30e-3]), atest = false; end
        if ~isequal(io(pn).plists.find('scaleout'), false), atest = false; end
        
        % Check options
        if ~isequal(io(pn).plists.getOptionsForParam('model'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('range'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('fs'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('complete_hf'), {'flat', 'lowpass'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('maxiter'), {30}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('minorder'), {2}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('maxorder'), {25}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('weights'), {'equal', '1/abs', '1/abs^2'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('plot'), {false, true}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('disp'), {false, true}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('msevartol'), {.1}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('fittol'), {.01}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('poletype'), {1 2 3}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('flim'), {[1e-3 30e-3]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('scaleout'), {false, true}), atest = false; end
        
        %%%%%%%%%%   SET 'psd'        
        pn = 4;
        if io(pn).plists.nparams ~= 29, atest = false; end        
        % Check key
        if ~io(pn).plists.isparam('model'), atest = false; end
        if ~io(pn).plists.isparam('range'), atest = false; end
        if ~io(pn).plists.isparam('fs'), atest = false; end
        if ~io(pn).plists.isparam('complete_hf'), atest = false; end        
        if ~io(pn).plists.isparam('maxiter'), atest = false; end
        if ~io(pn).plists.isparam('poletype'), atest = false; end
        if ~io(pn).plists.isparam('minorder'), atest = false; end
        if ~io(pn).plists.isparam('maxorder'), atest = false; end
        if ~io(pn).plists.isparam('weights'), atest = false; end
        if ~io(pn).plists.isparam('plot'), atest = false; end
        if ~io(pn).plists.isparam('disp'), atest = false; end
        if ~io(pn).plists.isparam('msevartol'), atest = false; end
        if ~io(pn).plists.isparam('fittol'), atest = false; end 
        if ~io(pn).plists.isparam('nfft'), atest = false; end
        if ~io(pn).plists.isparam('win'), atest = false; end
        if ~io(pn).plists.isparam('psll'), atest = false; end        
        if ~io(pn).plists.isparam('olap'), atest = false; end
        if ~io(pn).plists.isparam('order'), atest = false; end
        if ~io(pn).plists.isparam('navs'), atest = false; end
        if ~io(pn).plists.isparam('times'), atest = false; end
        if ~io(pn).plists.isparam('split'), atest = false; end        
        if ~io(pn).plists.isparam('scale'), atest = false; end
        if ~io(pn).plists.isparam('method'), atest = false; end
        if ~io(pn).plists.isparam('xscale'), atest = false; end
        if ~io(pn).plists.isparam('resolution'), atest = false; end
        if ~io(pn).plists.isparam('xvals'), atest = false; end
        if ~io(pn).plists.isparam('inherit_dy'), atest = false; end
        if ~io(pn).plists.isparam('flim'), atest = false; end
        if ~io(pn).plists.isparam('scaleout'), atest = false; end
        
        % Check default value        
        if ~isequal(io(pn).plists.find('model'), 'PSD'), atest = false; end
        if ~isequal(io(pn).plists.find('range'), []), atest = false; end
        if ~isequal(io(pn).plists.find('fs'), []), atest = false; end
        if ~isequal(io(pn).plists.find('complete_hf'), 'flat'), atest = false; end
        if ~isequal(io(pn).plists.find('maxiter'), 30), atest = false; end
        if ~isequal(io(pn).plists.find('poletype'), 1), atest = false; end
        if ~isequal(io(pn).plists.find('minorder'), 2), atest = false; end
        if ~isequal(io(pn).plists.find('maxorder'), 25), atest = false; end
        if ~isequal(io(pn).plists.find('weights'), '1/abs'), atest = false; end
        if ~isequal(io(pn).plists.find('plot'), false), atest = false; end
        if ~isequal(io(pn).plists.find('disp'), false), atest = false; end
        if ~isequal(io(pn).plists.find('msevartol'), .1), atest = false; end
        if ~isequal(io(pn).plists.find('fittol'), .01), atest = false; end        
        if ~isequal(io(pn).plists.find('nfft'), -1), atest = false; end        
        if ~strcmpi(io(pn).plists.find('win'), defaultWinType), atest = false; end
        if ~isequal(io(pn).plists.find('psll'), 200), atest = false; end        
        if ~isequal(io(pn).plists.find('olap'), 50), atest = false; end
        if ~isequal(io(pn).plists.find('order'), 1), atest = false; end
        if ~isequal(io(pn).plists.find('navs'), 16), atest = false; end
        if ~isequal(io(pn).plists.find('times'), []), atest = false; end        
        if ~isequal(io(pn).plists.find('split'), []), atest = false; end        
        if ~isequal(io(pn).plists.find('scale'), 'PSD'), atest = false; end                
        if ~isequal(io(pn).plists.find('method'), 'MEAN'), atest = false; end        
        if ~isequal(io(pn).plists.find('xscale'), 'LOG'), atest = false; end
        if ~isequal(io(pn).plists.find('resolution'), 50), atest = false; end
        if ~isequal(io(pn).plists.find('xvals'), []), atest = false; end
        if ~isequal(io(pn).plists.find('inherit_dy'), 'yes'), atest = false; end
        if ~isequal(io(pn).plists.find('flim'), [1e-3 30e-3]), atest = false; end
        if ~isequal(io(pn).plists.find('scaleout'), false), atest = false; end
        
        % Check options        
        if ~isequal(io(pn).plists.getOptionsForParam('model'), {'PSD'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('range'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('fs'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('complete_hf'), {'flat', 'lowpass'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('maxiter'), {30}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('poletype'), {1 2 3}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('minorder'), {2}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('maxorder'), {25}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('weights'), {'equal', '1/abs', '1/abs^2'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('plot'), {false, true}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('disp'), {false, true}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('msevartol'), {.1}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('fittol'), {.01}), atest = false; end                
        if ~isequal(io(pn).plists.getOptionsForParam('nfft'), {-1}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('win'), specwin.getTypes), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('psll'), {200}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('olap'), {50}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('order'), {1}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('navs'), {16}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('times'), {[]}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('split'), {[]}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('scale'), {'PSD', 'ASD', 'PS', 'AS'}), atest = false; end             
        if ~isequal(io(pn).plists.getOptionsForParam('method'), {'MEAN'}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('xscale'), {'LOG','LIN'}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('resolution'), {50}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('xvals'), {[]}), atest = false; end     
        if ~isequal(io(pn).plists.getOptionsForParam('inherit_dy'), {'yes','no'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('flim'), {[1e-3 30e-3]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('scaleout'), {false, true}), atest = false; end
        
        %%%%%%%%%%   SET 'lpsd'        
        pn = 5;
        if io(pn).plists.nparams ~= 25, atest = false; end        
        % Check key
        if ~io(pn).plists.isparam('model'), atest = false; end
        if ~io(pn).plists.isparam('range'), atest = false; end
        if ~io(pn).plists.isparam('fs'), atest = false; end
        if ~io(pn).plists.isparam('complete_hf'), atest = false; end        
        if ~io(pn).plists.isparam('maxiter'), atest = false; end
        if ~io(pn).plists.isparam('poletype'), atest = false; end
        if ~io(pn).plists.isparam('minorder'), atest = false; end
        if ~io(pn).plists.isparam('maxorder'), atest = false; end
        if ~io(pn).plists.isparam('weights'), atest = false; end
        if ~io(pn).plists.isparam('plot'), atest = false; end
        if ~io(pn).plists.isparam('disp'), atest = false; end
        if ~io(pn).plists.isparam('msevartol'), atest = false; end
        if ~io(pn).plists.isparam('fittol'), atest = false; end
        if ~io(pn).plists.isparam('kdes'), atest = false; end
        if ~io(pn).plists.isparam('jdes'), atest = false; end
        if ~io(pn).plists.isparam('lmin'), atest = false; end
        if ~io(pn).plists.isparam('win'), atest = false; end
        if ~io(pn).plists.isparam('psll'), atest = false; end        
        if ~io(pn).plists.isparam('olap'), atest = false; end
        if ~io(pn).plists.isparam('order'), atest = false; end
        if ~io(pn).plists.isparam('times'), atest = false; end
        if ~io(pn).plists.isparam('split'), atest = false; end
        if ~io(pn).plists.isparam('scale'), atest = false; end
        if ~io(pn).plists.isparam('flim'), atest = false; end
        if ~io(pn).plists.isparam('scaleout'), atest = false; end
        
        % Check default value        
        if ~isequal(io(pn).plists.find('model'), 'LPSD'), atest = false; end        
        if ~isequal(io(pn).plists.find('range'), []), atest = false; end
        if ~isequal(io(pn).plists.find('fs'), []), atest = false; end
        if ~isequal(io(pn).plists.find('complete_hf'), 'flat'), atest = false; end
        if ~isequal(io(pn).plists.find('maxiter'), 30), atest = false; end
        if ~isequal(io(pn).plists.find('poletype'), 1), atest = false; end
        if ~isequal(io(pn).plists.find('minorder'), 2), atest = false; end
        if ~isequal(io(pn).plists.find('maxorder'), 25), atest = false; end
        if ~isequal(io(pn).plists.find('weights'), '1/abs'), atest = false; end
        if ~isequal(io(pn).plists.find('plot'), false), atest = false; end
        if ~isequal(io(pn).plists.find('disp'), false), atest = false; end
        if ~isequal(io(pn).plists.find('msevartol'), .1), atest = false; end
        if ~isequal(io(pn).plists.find('fittol'), .01), atest = false; end        
        if ~isequal(io(pn).plists.find('kdes'), 100), atest = false; end
        if ~isequal(io(pn).plists.find('jdes'), 1000), atest = false; end
        if ~isequal(io(pn).plists.find('lmin'), 0), atest = false; end
        if ~strcmpi(io(pn).plists.find('win'), defaultWinType), atest = false; end
        if ~isequal(io(pn).plists.find('psll'), 200), atest = false; end
        if ~isequal(io(pn).plists.find('olap'), -1), atest = false; end
        if ~isequal(io(pn).plists.find('order'), 0), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('times')), atest = false; end
        if ~isEmptyDouble(io(pn).plists.find('split')), atest = false; end
        if ~isequal(io(pn).plists.find('scale'), 'PSD'), atest = false; end
        if ~isequal(io(pn).plists.find('flim'), [1e-3 30e-3]), atest = false; end
        if ~isequal(io(pn).plists.find('scaleout'), false), atest = false; end
        
        % Check options        
        if ~isequal(io(pn).plists.getOptionsForParam('model'), {'LPSD'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('range'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('fs'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('complete_hf'), {'flat', 'lowpass'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('maxiter'), {30}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('poletype'), {1 2 3}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('minorder'), {2}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('maxorder'), {25}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('weights'), {'equal', '1/abs', '1/abs^2'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('plot'), {false, true}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('disp'), {false, true}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('msevartol'), {.1}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('fittol'), {.01}), atest = false; end        
        if ~isequal(io(pn).plists.getOptionsForParam('kdes'), {100}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('jdes'), {1000}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('lmin'), {0}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('win'), specwin.getTypes), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('psll'), {200}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('olap'), {-1}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('order'), {-1 0 1 2 3 4 5 6 7 8 9}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('times'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('split'), {[]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('scale'), {'PSD', 'ASD', 'PS', 'AS'}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('flim'), {[1e-3 30e-3]}), atest = false; end
        if ~isequal(io(pn).plists.getOptionsForParam('scaleout'), {false, true}), atest = false; end
        
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01

  %% UTP_02

  % <TestDescription>
  %
  % Tests that the whiten1D method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the whiten1D method works for a vector of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      u = get_random_unit();
      atvec.setYunits(u);
      out = whiten1D(atvec);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in 'atvec'
    % 2) Check that each output AO contains the correct data.
    % 3) Check that each output AO contains empty yunits
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out), size(atvec)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out)
        % Check the whiten ao(tsdata) objects
        if isa(out(kk).data, 'tsdata')
          used_filt = out(kk).procinfo.find('Filter');
          obj = atvec(kk).filter(used_filt);
          if ~eq(out(kk), obj, ple3), atest = false; end
          if ~eq(out(kk).yunits, unit('')), atest = false; end
        else
          % Check the other objects (they must be the same)
          if ~eq(out(kk), atvec(kk), ple1), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_02

  %% UTP_03

  % <TestDescription>
  %
  % Tests that the whiten1D method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Test that the whiten1D method works for a matrix of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      u = get_random_unit();
      atmat.setYunits(u);
      out = whiten1D(atmat);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in 'atmat'
    % 2) Check that each output AO contains the correct data.
    % 3) Check that each output AO contains empty yunits
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out), size(atmat)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out)
        % Check the whiten ao(tsdata) objects
        if isa(out(kk).data, 'tsdata')
          used_filt = out(kk).procinfo.find('Filter');
          obj = atmat(kk).filter(used_filt);
          if ~eq(out(kk), obj, ple3), atest = false; end
          if ~eq(out(kk).yunits, unit('')), atest = false; end
        else
          % Check the other objects (they must be the same)
          if ~eq(out(kk), atmat(kk), ple1), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_03

  %% UTP_04

  % <TestDescription>
  %
  % Tests that the whiten1D method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Test that the whiten1D method works for a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      at1.setYunits(get_random_unit());
      at2.setYunits(get_random_unit());
      at3.setYunits(get_random_unit());
      out = whiten1D(at1,at2,at3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in
    %    input.
    % 2) Check that each output AO contains the correct data.
    % 3) Check that each output AO contains empty yunits
    %
    % </AlgoDescription>

    atest = true;
    aoin = [at1,at2,at3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out), size(aoin)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out)
        % Check the whiten ao(tsdata) objects
        if isa(out(kk).data, 'tsdata')
          used_filt = out(kk).procinfo.find('Filter');
          obj = aoin(kk).filter(used_filt);
          if ~eq(out(kk), obj, ple3), atest = false; end
          if ~eq(out(kk).yunits, unit('')), atest = false; end
        else
          % Check the other objects (they must be the same)
          if ~eq(out(kk), aoin(kk), ple1), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_04

  %% UTP_05

  % <TestDescription>
  %
  % Tests that the whiten1D method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Test that the whiten1D method works with an input of matrices and vectors
    % and single AOs.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out = whiten1D(at1,[at2 at3],[at4 at5; at6 at1]);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the number of elements in 'out' is the same as in
    %    input.
    % 2) Check that each output AO contains the correct data.
    % 3) Check that each output AO contains empty yunits
    %
    % </AlgoDescription>

    atest = true;
    aoin  = [at1,reshape([at2 at3],1,[]),reshape([at4 at5; at6 at1],1,[])];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out), size(aoin)), atest = false; end
      % Check each output against the absolute value of the input
      for kk=1:numel(out)
        % Check the whiten ao(tsdata) objects
        if isa(out(kk).data, 'tsdata')
          used_filt = out(kk).procinfo.find('Filter');
          obj = aoin(kk).filter(used_filt);
          if ~eq(out(kk), obj, ple3), atest = false; end
          if ~eq(out(kk).yunits, unit('')), atest = false; end
        else
          % Check the other objects (they must be the same)
          if ~eq(out(kk), aoin(kk), ple1), atest = false; end
        end
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_05

  %% UTP_06

  % <TestDescription>
  %
  % Tests that the whiten1D method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the whiten1D method can be processed back.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out  = whiten1D(at1);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'whiten1D'.
    % 2) Check that the re-built object is the same object as the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'whiten1D'), atest = false; end
      % Check the re-built object
      if ~eq(mout, out, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_06

  %% UTP_07

  % <TestDescription>
  %
  % Tests that the whiten1D method can modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the whiten1D method can modify the input AO by calling with no
    % output and that the method doesn't change the input of the function
    % notation (with a equal sign).
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % copy at1 to work with
      ain = ao(at1);
      % modify ain
      aout = ain.whiten1D();
      
      % It is necessary that ain uses the same random state
      ain.whiten1D(plist('rand_stream', aout.hist.plistUsed.find('rand_stream')));
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that 'at1' and 'ain' are now different.
    % 2) Check that 'ain' is whiten1D(at1).
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that whiten1D modified the input by comparing to the copy
      if eq(ao(at1), ain, ple1), atest = false; end
      % Check that whiten1D doesn't modified the input for the function
      % notation
      if ~eq(aout, ain, ple3), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07

  %% UTP_08

  % <TestDescription>
  %
  % Test the shape of the output.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the whiten1D method keeps the data shape of the input object. The
    % input AO must be an AO with row data and an AO with column data.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      at5.setYunits(get_random_unit());
      at6.setYunits(get_random_unit());
      out1 = whiten1D(at5);
      out2 = whiten1D(at6);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shape of the data doesn't change.
    % 2) Check that the output AOs have empty yunits
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the shape of the output data
      if size(out1.data.x) ~= size(at5.data.x), atest = false; end
      if size(out1.data.y) ~= size(at5.data.y), atest = false; end
      if size(out2.data.x) ~= size(at6.data.x), atest = false; end
      if size(out2.data.y) ~= size(at6.data.y), atest = false; end
      if ~eq(out1.yunits, unit('')), atest = false; end
      if ~eq(out2.yunits, unit('')), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_08

  %% UTP_09

  % <TestDescription>
  %
  % Check that the whiten1D method pass back the output objects to a list of
  % output variables or to a single variable.
  %
  % </TestDescription>
  function result = utp_09

    % <SyntaxDescription>
    %
    % Call the method with a list of output variables and with a single output
    % variable. Additionaly check that the rebuild method works on the output.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      [o1, o2] = whiten1D(at5, at6);
      o3  = whiten1D(at5, at6);
      mout1 = rebuild(o1);
      mout2 = rebuild(o2);
      mout3 = rebuild(o3);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output contains the right number of objects
    % 2) Check that the 'rebuild' method produces the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the number of outputs
      if numel(o1) ~=1, atest = false; end
      if numel(o2) ~=1, atest = false; end
      if numel(o3) ~=2, atest = false; end
      % Check the rebuilding of the object
      if ~eq(o1, mout1, ple2), atest = false; end
      if ~eq(o2, mout2, ple2), atest = false; end
      if ~eq(o3, mout3, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_09
  
  %% UTP_10

  % <TestDescription>
  %
  % Check that the whiten1D method is capable to increase spectral flatness
  % in case of no model input
  %
  % </TestDescription>
  function result = utp_10

    % <SyntaxDescription>
    %
    % Generate a fixed series of noise data, apply a filter to colour data
    % and run the method with a certain number of parameters.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Making test data
      
      a = ao(plist('tsfcn', 'randn(size(t))', 'fs', 10, 'nsecs', 10000, ...
        'yunits', get_random_unit())); % random noise
      pzm10 = pzmodel(1, {0.01}, {0.1});
      ft = miir(pzm10);
      af = filter(a, ft); % Colored noise
      
      % Running whiten1D with no model input
      pl10 = plist(...
        'model', [], ...
        'MaxIter', 30, ...
        'PoleType', 1, ...
        'MinOrder', 2, ...
        'MaxOrder', 9, ...
        'Weights', 2, ...
        'Plot', false,...
        'Disp', false,...
        'RMSEVar', 3,...
        'FitTolerance', 0.6); % tolerance on fit residuals spectral flatness
      
      aw = whiten1D(af,pl10);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output spectrum is flatter than input colored
    % spectrum
    % 2) Check that the output AOs have empty yunits
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Make spectra
      afxx = af.psd;
      awxx = aw.psd;
      
      % Claculating flatness
      sf = utils.math.spflat([afxx.data.y awxx.data.y]);
      
      % Checking flatness
      if sf(1)>sf(2), atest = false; end
      if ~eq(aw.yunits, unit('')), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_10
  
  %% UTP_11

  % <TestDescription>
  %
  % Check that the whiten1D method is capable to increase spectral flatness
  % when a model is input
  %
  % </TestDescription>
  function result = utp_11

    % <SyntaxDescription>
    %
    % Generate a fixed series of noise data, apply a filter to colour data
    % and run the method with a certain number of parameters.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Making test data
      a = ao(plist('tsfcn', 'randn(size(t))', 'fs', 10, 'nsecs', 10000, ...
        'yunits', get_random_unit())); % random noise
      pzm11 = pzmodel(1, {0.01}, {0.1});
      ft = miir(pzm11);
      af = filter(a, ft); % Colored noise
      
      % Running whiten1D with model input
      pl11 = plist(...
        'model', abs(pzm11.resp).^2, ...
        'fs', 10, ...
        'MaxIter', 30, ...
        'PoleType', 1, ...
        'MinOrder', 2, ...
        'MaxOrder', 9, ...
        'Weights', 2, ...
        'Plot', false,...
        'Disp', false,...
        'RMSEVar', 8,...
        'FitTolerance', 2); % tolerance on fit residuals log difference with model data
      
      aw = whiten1D(af,pl11);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output spectrum is flatter than input colored
    % spectrum
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Make spectra
      afxx = af.psd;
      awxx = aw.psd;
      
      % Claculating flatness
      sf = utils.math.spflat([afxx.data.y awxx.data.y]);
      
      % Checking flatness
      if sf(1)>sf(2), atest = false; end
      if ~eq(aw.yunits, unit('')), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_11

end
