% UTP_AO_FFT a set of UTPs for the ao/fft method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_fft.m,v 1.13 2011/02/04 11:08:54 luigi Exp $
%

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

function results = utp_ao_fft(varargin)

  % Check the inputs
  if nargin == 0

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

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

    % Test AOs
    [at1,at2,at3,at4,at5,at6,atvec,atmat] = eval(['get_test_objects_' class]);

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

    % Run the tests
    atvec = [at1 at5 at6];
    atmat = [at1 at5 at6; at5 at6 at1];
    
    results = [results utp_01];    % getInfo call
    
    results = [results utp_02(mthd, atvec, @algo_test_y, [], ple3)];    % Vector input
    results = [results utp_03(mthd, atmat, @algo_test_y, [], ple3)];    % Matrix input
    results = [results utp_04(mthd, at1, at5, at6, @algo_test_y, [], ple3)];    % List input
    results = [results utp_05(mthd, at1, atvec, atmat, @algo_test_y, [], ple3)];    % Test with mixed input
    results = [results utp_06(mthd, at1, [], ple2)];    % Test history is working
    results = [results utp_07(mthd, at1, plist('neval', true), ple2)];    % Test the modify call works
    results = [results utp_09(mthd, at5, at6)];    % Test input data shape == output data shape
    results = [results utp_10(mthd, at1, at5, ple2)];    % Test output of the data
    results = [results utp_11(mthd, at1, ple1)];    % Test plotinfo doesn't disappear    
    
    results = [results utp_12];    % Test against MATLAB's fft
    results = [results utp_13];    % Test with different data types
    results = [results utp_14];    % Test with plist: 'type' = 'two'

    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
  
  %% Algorithm test for UTP 02,03,04,05
  
  function atest = algo_test_y(in, out, pli)    
    atest = true;
    fs     = in.data.fs;
    fmin   = 0;
    nfft   = length(in.y);
    ft     = fft(in.y);
    ft   = ft(1:nfft/2+1);
    f    = linspace(fmin, fs/2, length(ft)).';
    % Check y-axis
    if ~isequal(out.y, ft), atest = false; end
    % Check x-axis
    if ~isequal(out.x, f), atest = false; end
    % Check fs
    if ~isequal(out.fs, fs), atest = false; end
    % Check units
    if ~eq(out.xunits, unit('Hz')), atest = false; end
    if ~eq(out.yunits, in.yunits), atest = false; end
  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 ~= 2, atest = false; end
        % Check key
        if ~io(3).plists.isparam('type'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('type'), 'one'), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('type'), {'plain', 'one', 'two'}), atest = false; end
        % Check key
        if ~io(3).plists.isparam('scale'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('scale'), false), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('scale'), paramValue.FALSE_TRUE{2}), 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_12

  % <TestDescription>
  %
  % Tests that the fft method agrees with MATLAB's fft when
  % configured to use the same parameters.
  %
  % </TestDescription>
  function result = utp_12

    % <SyntaxDescription>
    %
    % Test that the applying fft works on a single AO.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Construct two test AOs
      nsecs = 10;
      fs    = 1000;
      pl = plist('nsecs', nsecs, 'fs', fs, 'tsfcn', 'randn(size(t))');
      a1 = ao(pl);
      % Compute fft
      out = fft(a1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that output agrees with the output of MATLAB's fft.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Compute fft using MATLAB's fft
      % ao/fft returns only one-sided fft (0-Nyquist)
      yxx = fft(a1.data.y);
      if ~isequal(yxx(1:length(yxx)/2+1), out.data.y), 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 that the fft method works with different data types. The testing of
  % tsdata types are done before.
  %
  % </TestDescription>
  function result = utp_13

    % <SyntaxDescription>
    %
    % Test that the applying fft works on cdata and xydata.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Compute fft
      out3 = fft(at3);
      out4 = fft(at4);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that each output AO contains the correct data.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Compute fft for cdata and xydata
      fs   = 2+length(at3.y);
      nfft = length(at3.y);
      ft   = fft(at3.y);
      ft   = ft(1:nfft/2+1);
      f    = (0:floor(nfft/2)).*fs./nfft;
      % Check y-axis
      if ~isequal(out3.y, ft), atest = false; end
      % Check x-axis
      if ~isequal(out3.x, f.'), atest = false; end
      % Check fs
      if ~isequal(out3.fs, fs), atest = false; end
      % Check units
      if ~eq(out3.xunits, unit('Hz')), atest = false; end
      if ~eq(out3.yunits, at3.yunits), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

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

  %% UTP_14

  % <TestDescription>
  %
  % Tests that the fft method works with a plist which constains the key/value
  % pair 'type'/'two'.
  %
  % </TestDescription>
  function result = utp_14

    % <SyntaxDescription>
    %
    % Test that the applying fft works with a plist.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      pl = plist('type', 'two');
      % Compute fft
      out  = fft(at5, pl);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that each output AO contains the correct data.
    % 2) Check that the re-built object is the same object as 'out'.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Compute fft for cdata and xydata
      fs   = at5.data.fs;
      nfft = length(at5.y);
      ft     = fft(at5.data.y);
      ft   = fftshift(ft);
      f = fftshift([0:floor(nfft/2)-1 -floor(nfft/2):-1].*fs./nfft);
      % Check y-axis
      if ~isequal(out.y, ft), atest = false; end
      % Check x-axis
      if ~isequal(out.x, f.'), atest = false; end
      % Check fs
      if ~isequal(out.fs, fs), atest = false; end
      % Check units
      if ~eq(out.xunits, unit('Hz')), atest = false; end
      if ~eq(out.yunits, at5.yunits), 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_14

end
