% UTP_AO_FFTFILT a set of UTPs for the ao/fftfilt method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_fftfilt.m,v 1.9 2011/05/28 05:38:23 mauro Exp $
%

% <MethodDescription>
%
% The ifft method of the ao class computes the inverse fast fourier
% transform of time-series AOs.
%
% </MethodDescription>

function results = utp_ao_fftfilt(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'fftfilt';
 
    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
    % Test filter objects
    [iirhp,iirlp,iirbp,iirbr,iirpzm,iirab,iirv,iirm] = get_test_objects_miir;
    [firhp,firlp,firbp,firbr,firpzm,firao,firv,firm] = get_test_objects_mfir;
    [ra1,ra2,ra3,rav,ram] = get_test_objects_rational;
    [pz1, pz2, pz3, pz4, pz5, pzv, pzm] = get_test_objects_pzmodel;
    [pf1,pf2,pf3,pfv,pfm] = get_test_objects_parfrac;
    
    % NOTE: smodel filtering will be tested as soon as standard test
    % objects will be added

    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();

    % 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 the data shape
    results = [results utp_09];    % Test output of the 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>

    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')
        %%% 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
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 3, atest = false; end
        % Check key
        if ~io(3).plists.isparam('filter'), atest = false; end
        if ~io(3).plists.isparam('Npad'), atest = false; end
        if ~io(3).plists.isparam('Initial Conditions'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('filter'), ''), atest = false; end
        if ~isequal(io(3).plists.find('Npad'), []), atest = false; end
        if ~isequal(io(3).plists.find('Initial Conditions'), {}), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('filter'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('Npad'), {[]}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('Initial Conditions'), {{}}), 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 fftfilt method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

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

    try
      % <SyntaxCode>
      avec = [at1 at1];
      out1  = fftfilt(avec, redesign(iirlp, at1.fs));
      out2  = fftfilt(avec, redesign(firlp, at1.fs));
      out3  = fftfilt(avec, ra1);
      out4  = fftfilt(avec, pz1);
      out5  = fftfilt(avec, pf1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= numel(avec), atest = false; end
      if numel(out2) ~= numel(avec), atest = false; end
      if numel(out3) ~= numel(avec), atest = false; end
      if numel(out4) ~= numel(avec), atest = false; end
      if numel(out5) ~= numel(avec), atest = false; end
      % Check each output

      % </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 fftfilt method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

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

    try
      % <SyntaxCode>
      amat = [at1 at1 at1; at1 at1 at1];
      out1  = fftfilt(amat, redesign(iirlp, at1.fs));
      out2  = fftfilt(amat, redesign(firlp, at1.fs));
      out3  = fftfilt(amat, ra1);
      out4  = fftfilt(amat, pz1);
      out5  = fftfilt(amat, pf1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= numel(amat), atest = false; end
      if numel(out2) ~= numel(amat), atest = false; end
      if numel(out3) ~= numel(amat), atest = false; end
      if numel(out4) ~= numel(amat), atest = false; end
      if numel(out5) ~= numel(amat), atest = false; end
      % Check each output
      
      % </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 fftfilt method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

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

    try
      % <SyntaxCode>
      out1  = fftfilt(at1, at1, at1, redesign(iirlp, at1.fs));
      out2  = fftfilt(at1, at1, at1, redesign(firlp, at1.fs));
      out3  = fftfilt(at1, at1, at1, ra1);
      out4  = fftfilt(at1, at1, at1, pz1);
      out5  = fftfilt(at1, at1, at1, pf1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
%     aoin  = [at1, at1, at1];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= 3, atest = false; end
      if numel(out2) ~= 3, atest = false; end
      if numel(out3) ~= 3, atest = false; end
      if numel(out4) ~= 3, atest = false; end
      if numel(out5) ~= 3, atest = false; end
      % Check each output
      
      % </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 fftfilt method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      out1  = fftfilt(at1,[at1 at1],at1,[at1 at1; at1 at1],at1, redesign(iirlp, at1.fs));
      out2  = fftfilt(at1,[at1 at1],at1,[at1 at1; at1 at1],at1, redesign(firlp, at1.fs));
      out3  = fftfilt(at1,[at1 at1],at1,[at1 at1; at1 at1],at1, ra1);
      out4  = fftfilt(at1,[at1 at1],at1,[at1 at1; at1 at1],at1, pz1);
      out5  = fftfilt(at1,[at1 at1],at1,[at1 at1; at1 at1],at1, pf1);
      % </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.
    %
    % </AlgoDescription>

    atest = true;
%     aoin  = [at1, reshape([at1 at1], 1, []), at1, reshape([at1 at1; at1 at1], 1, []), at1];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= 9, atest = false; end
      if numel(out2) ~= 9, atest = false; end
      if numel(out3) ~= 9, atest = false; end
      if numel(out4) ~= 9, atest = false; end
      if numel(out5) ~= 9, atest = false; end
      % Check each output
      
      % </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 fftfilt method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the fftfilt method can be processed back
    % to an m-file.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out1  = fftfilt(at1, redesign(iirlp, at1.fs));
      out2  = fftfilt(at1, redesign(firlp, at1.fs));
      out3  = fftfilt(at1, ra1);
      out4  = fftfilt(at1, pz1);
      out5  = fftfilt(at1, pf1);
      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      mout3 = rebuild(out3);
      mout4 = rebuild(out4);
      mout5 = rebuild(out5);
      % </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
    %    'fftfilt'.
    % 2) % Check that the re-built object is the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out1.hist.methodInfo.mname, 'fftfilt'), atest = false; end
      if ~strcmp(out2.hist.methodInfo.mname, 'fftfilt'), atest = false; end
      if ~strcmp(out3.hist.methodInfo.mname, 'fftfilt'), atest = false; end
      if ~strcmp(out4.hist.methodInfo.mname, 'fftfilt'), atest = false; end
      if ~strcmp(out5.hist.methodInfo.mname, 'fftfilt'), atest = false; end
      % Check the re-built object
      if ~eq(mout1, out1, ple2), atest = false; end
      if ~eq(mout2, out2, ple2), atest = false; end
      if ~eq(mout3, out3, ple2), atest = false; end
      if ~eq(mout4, out4, ple2), atest = false; end
      if ~eq(mout5, out5, 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 fftfilt method can modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the fftfilt method can modify the input AO by calling with no
    % output.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % copy at2 to work with
      ain1 = ao(at5);
      ain2 = ao(at5);
      ain3 = ao(at5);
      ain4 = ao(at5);
      ain5 = ao(at5);
      % modify ain
      out1  = fftfilt(ain1, redesign(iirlp, ain1.fs));
      out2  = fftfilt(ain2, redesign(firlp, ain2.fs));
      out3  = fftfilt(ain3, ra1);
      out4  = fftfilt(ain4, pz1);
      out5  = fftfilt(ain5, pf1);
      ain1.fftfilt(redesign(iirlp, ain1.fs));
      ain2.fftfilt(redesign(firlp, ain2.fs));
      ain3.fftfilt(ra1);
      ain4.fftfilt(pz1);
      ain5.fftfilt(pf1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

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

    atest = true;
    if stest
      % <AlgoCode>
      % Check that fftfilt modified the input by comparing to the copy
      if eq(ao(at5), ain1, ple1), atest = false; end
      if eq(ao(at5), ain2, ple1), atest = false; end
      if eq(ao(at5), ain3, ple1), atest = false; end
      if eq(ao(at5), ain4, ple1), atest = false; end
      if eq(ao(at5), ain5, ple1), atest = false; end

      % Check that fftfilt doesn't modified the input for the function
      % notation
      if ~eq(out1, ain1, ple1), atest = false; end
      if ~eq(out2, ain2, ple1), atest = false; end
      if ~eq(out3, ain3, ple1), atest = false; end
      if ~eq(out4, ain4, ple1), atest = false; end
      if ~eq(out5, ain5, ple1), 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>
  %
  % Tests that the fftfilt method keeps the data shape of the input object.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the fftfilt 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>
      out11 = fftfilt(at5, redesign(iirlp, at5.fs));
      out12 = fftfilt(at6, redesign(iirlp, at6.fs));
      out21 = fftfilt(at5, redesign(firlp, at5.fs));
      out22 = fftfilt(at6, redesign(firlp, at6.fs));
      out31 = fftfilt(at5, ra1);
      out32 = fftfilt(at6, ra1);
      out41 = fftfilt(at5, pz1);
      out42 = fftfilt(at6, pz1);
      out51 = fftfilt(at5, pf1);
      out52 = fftfilt(at6, pf1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shpe of the data doesn't change.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the shape of the output data
      if size(out11.data.y,1) == 1, atest = false; end
      if size(out12.data.y,2) == 2, atest = false; end
      if size(out21.data.y,1) == 1, atest = false; end
      if size(out22.data.y,2) == 2, atest = false; end
      if size(out31.data.y,1) == 1, atest = false; end
      if size(out32.data.y,2) == 2, atest = false; end
      if size(out41.data.y,1) == 1, atest = false; end
      if size(out42.data.y,2) == 2, atest = false; end
      if size(out51.data.y,1) == 1, atest = false; end
      if size(out52.data.y,2) == 2, 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 fftfilt 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>
      [o11, o12] = fftfilt(at5, at6, redesign(iirlp, 10));
      no1  = fftfilt(at5, at6, redesign(iirlp, 10));
      mout11 = rebuild(o11);
      mout12 = rebuild(o12);
      moutn1 = rebuild(no1);
      
      [o21, o22] = fftfilt(at5, at6, redesign(firlp, 10));
      no2  = fftfilt(at5, at6, redesign(firlp, 10));
      mout21 = rebuild(o21);
      mout22 = rebuild(o22);
      moutn2 = rebuild(no2);
      
      [o31, o32] = fftfilt(at5, at6, ra1);
      no3  = fftfilt(at5, at6, ra1);
      mout31 = rebuild(o31);
      mout32 = rebuild(o32);
      moutn3 = rebuild(no3);
      
      [o41, o42] = fftfilt(at5, at6, pz1);
      no4  = fftfilt(at5, at6, pz1);
      mout41 = rebuild(o41);
      mout42 = rebuild(o42);
      moutn4 = rebuild(no4);
      
      [o51, o52] = fftfilt(at5, at6, pf1);
      no5  = fftfilt(at5, at6, pf1);
      mout51 = rebuild(o51);
      mout52 = rebuild(o52);
      moutn5 = rebuild(no5);
      
      % </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(o11) ~=1, atest = false; end
      if numel(o12) ~=1, atest = false; end
      if numel(no1) ~=2, atest = false; end
      % Check the rebuilding of the object
      if ~eq(o11, mout11, ple2), atest = false; end
      if ~eq(o12, mout12, ple2), atest = false; end
      if ~eq(no1, moutn1, ple2), atest = false; end
      
      % Check the number of outputs
      if numel(o21) ~=1, atest = false; end
      if numel(o22) ~=1, atest = false; end
      if numel(no2) ~=2, atest = false; end
      % Check the rebuilding of the object
      if ~eq(o21, mout21, ple2), atest = false; end
      if ~eq(o22, mout22, ple2), atest = false; end
      if ~eq(no2, moutn2, ple2), atest = false; end
      
      % Check the number of outputs
      if numel(o31) ~=1, atest = false; end
      if numel(o32) ~=1, atest = false; end
      if numel(no3) ~=2, atest = false; end
      % Check the rebuilding of the object
      if ~eq(o31, mout31, ple2), atest = false; end
      if ~eq(o32, mout32, ple2), atest = false; end
      if ~eq(no3, moutn3, ple2), atest = false; end
      
      % Check the number of outputs
      if numel(o41) ~=1, atest = false; end
      if numel(o42) ~=1, atest = false; end
      if numel(no4) ~=2, atest = false; end
      % Check the rebuilding of the object
      if ~eq(o41, mout41, ple2), atest = false; end
      if ~eq(o42, mout42, ple2), atest = false; end
      if ~eq(no4, moutn4, ple2), atest = false; end
      
      % Check the number of outputs
      if numel(o51) ~=1, atest = false; end
      if numel(o52) ~=1, atest = false; end
      if numel(no5) ~=2, atest = false; end
      % Check the rebuilding of the object
      if ~eq(o51, mout51, ple2), atest = false; end
      if ~eq(o52, mout52, ple2), atest = false; end
      if ~eq(no5, moutn5, 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

  

end
