% UTP_MIIR_REDESIGN a set of UTPs for the miir/redesign method
%
% M Hewitson 06-08-08
%
% $Id: utp_miir_redesign.m,v 1.3 2011/04/17 15:47:40 ingo Exp $
%

% <MethodDescription>
%
% The redesign method of the miir class redesign the input filter to work for the
% given sample rate.
%
% </MethodDescription>

function results = utp_miir_redesign(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'miir';
    mthd    = 'redesign';

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

    % Test MIIR objects
    [iirhp,iirlp,iirbp,iirbr,iirpzm,iirab,iirv,iirm] = get_test_objects_miir;

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

    e = ple3.find('EXCEPTIONS');
    ple3 = plist('EXCEPTIONS', [e {'iunits', 'ounits'}]);

    % 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 redesign from a standard type
    results = [results utp_08];    % Test redesign from a pzmodel type
    results = [results utp_09];    % Test redesign from a parfrac type

    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 ~= 0, atest = false; end
        % Check key
        % Check default value
        % Check options
      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 redesign method works with a vector of MIIR objects as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the redesign method works for a vector of MIIR objects as input.
    % To keep this UTP simple use for the vector only one filter object. The
    % different filters will be tested in an other UTP.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      hp = miir(iirhp);
      hp.setIunits('Hz');
      hp.setOunits('m^-2/3');
      iirvec = [hp, hp, hp];
      out1   = redesign(iirvec, 123);
      out2   = redesign(iirvec, plist('fs', 123));
      % </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 'iirvec'
    % 2) Check that each output MIIR contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out1), size(iirvec)), atest = false; end
      if ~isequal(size(out2), size(iirvec)), atest = false; end
      % Check each output against the redesign of the filter with the
      % new frequency
      iirhp_123 = miir(plist('type', 'highpass', 'fs', 123));
      for kk=1:numel(iirvec)
        if ~eq(out1(kk), iirhp_123, ple3), atest = false; end
        % The following values must have the same as the input values
        if ~strcmp(out1(kk).name, hp.name), atest = false; end
        if ~eq(out1(kk).iunits,   hp.iunits), atest = false; end
        if ~eq(out1(kk).ounits,   hp.ounits), atest = false; end
      end
      % 'out1' must be the same as 'out2'
      if ~eq(out1, out2, ple1), 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 redesign method works with a matrix of MIIR objects as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Test that the redesign method works for a matrix of MIIR objects as input.
    % To keep this UTP simple use for the matrix only one filter object. The
    % different filters will be tested in an other UTP.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      hp = miir(iirhp);
      hp.setIunits('Hz');
      hp.setOunits('m^-2/3');
      iirmat = [hp, hp, hp; hp, hp, hp];
      out1 = redesign(iirmat, 123);
      out2 = redesign(iirmat, plist('fs', 123));
      % </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 'iirmat'
    % 2) Check that each output MIIR contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if ~isequal(size(out1), size(iirmat)), atest = false; end
      if ~isequal(size(out2), size(iirmat)), atest = false; end
      % Check each output against the redesign of the filter with the
      % new frequency
      iirhp_123 = miir(plist('type', 'highpass', 'fs', 123));
      for kk=1:numel(iirmat)
        if ~eq(out1(kk), iirhp_123, ple3), atest = false; end
        % The following values must have the same as the input values
        if ~strcmp(out1(kk).name, hp.name), atest = false; end
        if ~eq(out1(kk).iunits,   hp.iunits), atest = false; end
        if ~eq(out1(kk).ounits,   hp.ounits), atest = false; end
      end
      % 'out1' must be the same as 'out2'
      if ~eq(out1, out2, ple1), 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 redesign method works with a list of MIIR objects as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Test that the redesign method works for a list of MIIR objects as input.
    % To keep this UTP simple use for the list only one filter object. The
    % different filters will be tested in an other UTP.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      hp = miir(iirhp);
      hp.setIunits('Hz');
      hp.setOunits('m^-2/3');
      out1 = redesign(hp, hp, hp, 123);
      out2 = redesign(hp, hp, hp, plist('fs', 123));
      % </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 MIIR contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    iirin = [hp, hp, hp];
    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
      % Check each output against the redesign of the filter with the
      % new frequency
      iirhp_123 = miir(plist('type', 'highpass', 'fs', 123));
      for kk=1:numel(iirin)
        if ~eq(out1(kk), iirhp_123, ple3), atest = false; end
        % The following values must have the same as the input values
        if ~strcmp(out1(kk).name, hp.name), atest = false; end
        if ~eq(out1(kk).iunits,   hp.iunits), atest = false; end
        if ~eq(out1(kk).ounits,   hp.ounits), atest = false; end
      end
      % 'out1' must be the same as 'out2'
      if ~eq(out1, out2, ple1), atest = false; 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 redesign method works with a mix of different shaped
  % MIIR objects as input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Test that the redesign method works with an input of matrices and vectors
    % and single MIIR objects.
    % To keep this UTP simple use for the vector only one filter object. The
    % different filters will be tested in an other UTP.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      hp = miir(iirhp);
      hp.setIunits('Hz');
      hp.setOunits('m^-2/3');
      out1 = redesign(hp,[hp hp],hp,[hp hp hp; hp hp hp],hp, 123);
      out2 = redesign(hp,[hp hp],hp,[hp hp hp; hp hp hp],hp, plist('fs', 123));
      % </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 MIIR contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    iirin  = [hp hp hp hp hp hp hp hp hp hp hp];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out1) ~= numel(iirin), atest = false; end
      if numel(out2) ~= numel(iirin), atest = false; end
      % Check each output against the redesign of the filter with the
      % new frequency
      iirhp_123 = miir(plist('type', 'highpass', 'fs', 123));
      for kk=1:numel(iirin)
        if ~eq(out1(kk), iirhp_123, ple3), atest = false; end
        % The following values must have the same as the input values
        if ~strcmp(out1(kk).name, hp.name), atest = false; end
        if ~eq(out1(kk).iunits,   hp.iunits), atest = false; end
        if ~eq(out1(kk).ounits,   hp.ounits), atest = false; end
      end
      % 'out1' must be the same as 'out2'
      if ~eq(out1, out2, ple1), atest = false; 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 redesign method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

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

    try
      % <SyntaxCode>
      hp = miir(iirhp);
      hp.setIunits('Hz');
      hp.setOunits('m^-2/3');
      out  = redesign(hp, 123);
      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
    %    'redesign'.
    % 2) Check that 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, 'redesign'), atest = false; end
      % Run 'test.m' and check the result
      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 redesign method redesigns a standard filter type.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Tests that the redesign method redesigns a standard filter type.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      out1 = redesign(iirhp, 123);
      out2 = redesign(iirlp, 123);
      out3 = redesign(iirbp, 123);
      out4 = redesign(iirbr, 123);

      mout1 = rebuild(out1);
      mout2 = rebuild(out2);
      mout3 = rebuild(out3);
      mout4 = rebuild(out4);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the output
    % 2) Check the rebuilt object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check each output against the redesign of the filter with the new
      % frequency
      hp_123 = miir(plist('type', 'highpass', 'fs', 123));
      lp_123 = miir(plist('type', 'lowpass', 'fs', 123));
      bp_123 = miir(plist('type', 'bandpass', 'fs', 123, 'fc', [0.01 0.1]));
      br_123 = miir(plist('type', 'bandreject', 'fs', 123, 'fc', [0.01 0.1]));
      if ~eq(out1, hp_123, ple3), atest = false; end
      if ~eq(out2, lp_123, ple3), atest = false; end
      if ~eq(out3, bp_123, ple3), atest = false; end
      if ~eq(out4, br_123, ple3), atest = false; end
      % Run 'test[1..4].m' and check the result
      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
      % </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 redesign method redesigns a pzmodel filter type.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Tests that the redesign method redesigns a pzmodel filter type.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pzm = pzmodel(1, [pz(1) pz(200)], pz(50));
      pzm.setName();
      pl = plist('pzmodel', pzm, 'fs', 1000);
      iirpzm = miir(pl);
      out = redesign(iirpzm, 123);

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

    % <AlgoDescription>
    %
    % 1) Check the output
    % 2) Check the rebuilt object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check each output against the redesign of the filter with the new
      % frequency
      pzm_123 = miir(plist('pzmodel', pzm, 'fs', 123));
      if ~eq(out, pzm_123, ple3), atest = false; end
      % Check the result of the rebuilt 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_08

  %% UTP_09

  % <TestDescription>
  %
  % Tests that the redesign method redesigns a parfrac filter type.
  %
  % </TestDescription>
  function result = utp_09

    % <SyntaxDescription>
    %
    % Tests that the redesign method redesigns a parfrac filter type.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pfrac = parfrac([1 2], {4, 6+2i}, 1, 'my par frac', unit('V'), unit('Hz'));
      pl = plist('parfrac', pfrac, 'fs', 1000);
      iirpf = miir(pl);
      out = redesign(iirpf, 123);

      o1 = out.index(1);
      o2 = out.index(2);
      mo1 = rebuild(o1);
      mo2 = rebuild(o2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check the output
    % 2) Check the rebuilt object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check each output against the redesign of the filter with the new
      % frequency
      pf_123 = miir(plist('parfrac', pfrac, 'fs', 123));
      if ~eq(out, pf_123, ple3), atest = false; end
      % Check the result of the rebuilt object.
      if ~eq(mo1, o1, ple2), atest = false; end
      if ~eq(mo2, o2, 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
