% UTP_AO_FILTFILT a set of UTPs for the ao/filtfilt method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_filtfilt.m,v 1.12 2010/03/23 20:36:38 ingo Exp $
%

% <MethodDescription>
%
% The filtfilt method of the ao class filters time-series AOs forwards and
% backwards.
%
% </MethodDescription>

function results = utp_ao_filtfilt(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);
    pli = plist('filter', miir(plist('type', 'lowpass', 'fs', at1.fs)));

    % 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 with miir filter and different fs
    results = [results utp_10];    % Test with mfir filter
    
    results = [results utp_11(mthd, at1, ple1, pli)];    % Test plotinfo doesn't disappear    
    
    results = [results utp_12];    % Test with fsdata + miir filter
    results = [results utp_13];    % Test with fsdata + mfir filter

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

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

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      avec   = [at1 at5 at6];
      [out, outf] = filtfilt(avec, pl);
      % </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.
    % 3) Check the output-filter
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= numel(avec), atest = false; end
      % Check the output data
      for kk = 1:numel(avec)
        y = filtfilt(fobj.a, fobj.b, avec(kk).y);
        % Check y-axis
        if ~isequal(out(kk).y, y), atest = false; end
        % y-units: yunits.*ounits./iunits
        if ~eq(out(kk).yunits, avec(kk).data.yunits.*ounits./iunits), atest = false; end
      end
      % Check filter
      if numel(outf) ~= numel(avec), atest = false; end
      if ~isa(outf, 'miir'), atest = false; 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 filtfilt method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

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

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      amat   = [at1 at5 at6; at5 at6 at1];
      [out, outf] = filtfilt(amat, pl);
      % </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.
    % 3) Check the output-filter
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= numel(amat), atest = false; end
      % Check the output data
      for kk = 1:numel(amat)
        y = filtfilt(fobj.a, fobj.b, amat(kk).y);
        % Check y-axis
        if ~isequal(out(kk).y, y), atest = false; end
        % y-units: yunits.*ounits./iunits
        if ~eq(out(kk).yunits, amat(kk).data.yunits.*ounits./iunits), atest = false; end
      end
      % Check filter
      if numel(outf) ~= numel(amat), atest = false; end
      if ~isa(outf, 'miir'), atest = false; 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 filtfilt method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

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

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      out = filtfilt(at1,at5,at6,pl);
      % </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, at5, at6];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 3, atest = false; end
      % Check the output data
      for kk = 1:numel(aoin)
        y = filtfilt(fobj.a, fobj.b, aoin(kk).y);
        % Check y-axis
        if ~isequal(out(kk).y, y), atest = false; end
        % y-units: yunits.*ounits./iunits
        if ~eq(out(kk).yunits, aoin(kk).data.yunits.*ounits./iunits), atest = false; 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 filtfilt method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

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

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      out = filtfilt(at1,[at5 at6],at5,[at5 at1; at6 at1],at6,pl);
      % </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([at5 at6], 1, []), at5, reshape([at5 at1; at6 at1], 1, []), at6];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 9, atest = false; end
      % Check the output data
      for kk = 1:numel(aoin)
        y = filtfilt(fobj.a, fobj.b, aoin(kk).y);
        % Check y-axis
        if ~isequal(out(kk).y, y), atest = false; end
        % y-units: yunits.*ounits./iunits
        if ~eq(out(kk).yunits, aoin(kk).data.yunits.*ounits./iunits), atest = false; 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 filtfilt method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

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

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      out  = filtfilt(at5,pl);
      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
    %    'filtfilt'.
    % 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(out.hist.methodInfo.mname, 'filtfilt'), 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 filtfilt method can modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

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

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      % copy at1 to work with
      ain   = ao(at1);
      % modify ain
      aout   = ain.filtfilt(pl);
      ain.filtfilt(pl);
      % </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 filtfilt(at1).
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that filtfilt modified the input by comparing to the copy
      if eq(ao(at1), ain, ple1), atest = false; end
      % Check that filtfilt doesn't modified the input for the function notation
      if ~eq(aout, ain, ple1), atest = false; end
      % Check that the modified input is the filtfilt of the copy
      y = filtfilt(fobj.a, fobj.b, at1.y);
      % Check y-axis
      if ~isequal(ain.y, y), atest = false; end
      % y-units: yunits.*ounits./iunits
      if ~eq(ain.yunits, at1.data.yunits.*ounits./iunits), 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 filtfilt method keeps the data shape of the input object.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the filtfilt 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>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'fs', 10, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobj);
      out1   = filtfilt(at5, pl);
      out2   = filtfilt(at6, pl);
      % </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(out1.data.y,2) ~= 1, atest = false; end
      if size(out2.data.y,1) ~= 1, 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>
  %
  % Tests that the filtfilt method with a iir filter which have a different sample
  % rate as the input object.
  %
  % </TestDescription>
  function result = utp_09

    % <SyntaxDescription>
    %
    % Test that the filtfilt method can change the sample rate of the filter and
    % that the filtfilt-object is returned.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobjin = miir(plist('type', 'highpass', 'fs', 3, 'iunits', iunits, 'ounits', ounits));
      pl     = plist('filter', fobjin);
      % modify ain
      [out, outf] = at1.filtfilt(pl);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output AO contains the correct data.
    % 2) Check that the second output is a iir filtfilt.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output data
      fobj = fobjin.redesign(at1.fs);
      y = filtfilt(fobj.a, fobj.b, at1.y);
      % Check y-axis
      if ~isequal(out.y, y), atest = false; end
      % y-units: yunits.*ounits./iunits
      if ~eq(out.yunits, at1.data.yunits.*ounits./iunits), atest = false; end
      % Check the output filter. The filter must have the new frequency
      if ~isa(outf, 'miir'), atest = false; end
      if ~isequal(outf.fs, out.fs), 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>
  %
  % Tests that the filtfilt method with a fir filter.
  %
  % </TestDescription>
  function result = utp_10

    % <SyntaxDescription>
    %
    % Test that the filtfilt method with a fir filter.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = mfir(plist('type', 'highpass', 'iunits', iunits, 'ounits', ounits, 'order', 64, 'fs', 10));
      pl     = plist('filter', fobj);
      % modify ain
      [out, outf] = at5.filtfilt(pl);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output AO contains the correct data.
    % 2) Check that the second output is a fir filter.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output data
      fobj = redesign(fobj, at5.fs);
      y = filtfilt(fobj.a, 1, at5.y);
      % Check y-axis
      if ~isequal(out.y, y), atest = false; end
      % y-units: yunits.*ounits./iunits
      if ~eq(out.yunits, at5.data.yunits.*ounits./iunits), atest = false; end
      % Check the output filter. The filter must have the new frequency
      if ~isa(outf, 'mfir'), 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_12

  % <TestDescription>
  %
  % Tests the filtfilt method with an AO and fsdata-object and an iir filter.
  %
  % </TestDescription>
  function result = utp_12

    % <SyntaxDescription>
    %
    % Test that the result of the filtfilt method is the product of the AO and the
    % response of the filter.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = miir(plist('type', 'highpass', 'iunits', iunits, 'ounits', ounits));
      fobj   = redesign(fobj, at2.fs);
      pl     = plist('filter', fobj);
      % modify ain
      [out, outf] = at2.filtfilt(pl);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output AO contains the correct data.
    % 2) Check that the second output is a iir filter.
    % 3) Check the units
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output data
      r = resp(fobj, plist('f', at2));
      a = at2.*r.*conj(r);
      if ~isequal(a.x, out.x), atest = false; end
      if ~isequal(a.y, out.y), atest = false; end
      % Check the output filter. The filter must have the new frequency
      if ~isa(outf, 'miir'), atest = false; end
      % Check units
      if ~eq(out.xunits, unit('Hz')), atest = false; end
      y_units = simplify(at2.yunits.*r.yunits.*r.yunits, 'Hz');
      if ~eq(out.yunits, y_units), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_13

  % <TestDescription>
  %
  % Tests the filtfilt method with an AO and fsdata-object and an iir filter.
  %
  % </TestDescription>
  function result = utp_13

    % <SyntaxDescription>
    %
    % Test that the result of the filtfilt method is the product of the AO and the
    % response of the filter.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      iunits = unit('m');
      ounits = unit('V');
      fobj   = mfir(plist('type', 'highpass', 'iunits', iunits, 'ounits', ounits));
      fobj   = redesign(fobj, at2.fs);
      pl     = plist('filter', fobj);
      % modify ain
      [out, outf] = at2.filtfilt(pl);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output AO contains the correct data.
    % 2) Check that the second output is a fir filter.
    % 3) Check the units
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the output data
      r = resp(fobj, plist('f', at2));
      a = at2.*r.*conj(r);
      if ~isequal(a.x, out.x), atest = false; end
      if ~isequal(a.y, out.y), atest = false; end
      % Check the output filter. The filter must have the new frequency
      if ~isa(outf, 'mfir'), atest = false; end
      % Check units
      if ~eq(out.xunits, unit('Hz')), atest = false; end
      % Compute y-units
      y_units = simplify(at2.yunits.*r.yunits.*r.yunits, 'Hz');
      if ~eq(out.yunits, y_units), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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