% cpsdForCorrelatedInputs computes the output theoretical CPSD shape with given inputs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: cpsdForCorrelatedInputs computes the output theoretical CPSD
%              or PSD shape with given inputs.
%              It returns summed and contributions only and takes
%              input arrays of objects (instead of vectors) 
%
% CALL: [mat_out] = CPSD(sys, pl)
%
% INPUTS:
%         - sys, (array of) ssm object
%
% OUTPUTS:
%          _ mat_out contains specified returned aos
%
% <a href="matlab:utils.helper.displayMethodInfo('ssm', 'cpsdForCorrelatedInputs')">Parameters Description</a>
% 
% VERSION: $Id: cpsdForCorrelatedInputs.m,v 1.2 2011/05/23 14:18:20 adrien Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = cpsdForCorrelatedInputs(varargin)
  
  %% starting initial checks
  
  % use the caller is method flag
  callerIsMethod = utils.helper.callerIsMethod;
  
  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end
  
  utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
  
  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end
  
  % Collect all SSMs and plists
  [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
  [pl, invars2, rest]  = utils.helper.collect_objects(rest(:), 'plist'); 
  if ~isempty(rest)
    pl = combine(pl, plist(rest{:}));
  end
  pl = combine(pl, getDefaultPlist());
  
  %%% Internal call: Only one object + don't look for a plist
  internal = strcmp(varargin{end}, 'internal');
  
  %% begin function body
  
  %% retrieve system infos
  
  if numel(sys)~=1
    error('noisespectrum needs exactly one ssm as an input')
  end
  if ~sys.isnumerical
    error(['error because system ',sys.name,' is not numerical']);
  end
  if ~sys.isStable
    error('input ssm is not stable!')
  end
  if sys.timestep==0
   timestep = 1;
  else
    timestep = sys.timestep;
  end
  if ~internal
    inhist  = sys.hist;
  end
  
  %% modifying system's ordering
  if find(pl, 'reorganize')
    sys = reorganize(sys, pl, 'set', 'for cpsdForCorrelatedInputs', 'internal', 'internal');
  end
  
  %% collecting functions i/o data
  aos_in = find(pl, 'aos');
  PZ_in = find(pl, 'PZmodels');
  cov_in = find(pl, 'covariance');
  cpsd_in = find(pl, 'CPSD');
  noise_in = blkdiag(cov_in, cpsd_in/(timestep*2));
  powWhiteNoise = noise_in;
  [U1,S1,V1] = svd(noise_in.'); % testing hermitian symmetry and definite positiveness
  if (sum(S1<0)>0)
    error('covariance/cpsd matrix is not positive')
  elseif norm(U1-V1')>1e-15*sqrt(numel(U1))
    error('covariance/cpsd matrix is not hermitian symmetric')
  end
    
  %% getting system's i/o sizes
  inputSizes = sys.inputsizes;
  outputSizes = sys.outputsizes; %#ok<NASGU>
  
  Naos_in = inputSizes(1);
  NPZmodels = inputSizes(3);
  
  %% retrieving frequency vector
  if isempty(Naos_in)==0
    f1 = find(pl,'f1');
    f2 = find(pl,'f2');
    NFreqs = find(pl,'nf');
    if isempty(f1) || isempty(f2)|| isempty(NFreqs)
      error('### Please specify frequency vector a start and stop frequency .');
    else
      freqs = 10.^linspace(log10(f1), log10(f2), NFreqs);
    end
  else
    freqs = aos_in(1).x;
  end
  
  %% checking frequency vector
  for i=2:numel(aos_in)
    if ~isequal(freqs,aos_in(i).x)
      error('there exist different frequency vectors');
    end
  end
  
  %% reshape pzmodels and aos for input cross-spectra
  if size(PZ_in,1)==NPZmodels
    PZdata = zeros(Npzmodels,Npzmodels,NFreqs);
    for i=1:NPZmodels
      for j=1:Npzmodels
        a = resp(PZ_in(i,j), freqs);
        PZdata(i,j,:) = reshape(a.y,[1,NFreqs]) ;
      end
    end
  else
    error('Wrong size for field PZ_in')
  end
  
  if size(aos_in,1)==Naos_in && size(aos_in,2)==Naos_in
    AOdata = zeros(Naos_in,Naos_in,NFreqs);
    for i=1:Naos_in
      for j=1:Naos_in
        AOdata(i,j,:) = reshape(aos_in(i,j).y,[1,NFreqs]) ;
      end
    end
  else
    error('Wrong size for field aos_in')
  end
    
  %% SSM Transfer function
  [a, b, c, d, Ts, InputName, StateName, OutputName,...
    inputvarunits, ssvarunits, outputvarunits] = double(sys);      %#ok<ASGLU>
  resps    = ssm.doBode(a, b, c, d, 2*pi*freqs, Ts);
  Noutputs = numel(OutputName);

  %% power for each frequency with SVD computation
  diagOnly = pl.find('DIAGONAL ONLY');
  if diagOnly
    Result = zeros(Noutputs,NFreqs);
  else
    Result = zeros(Noutputs,Noutputs,NFreqs);
  end
  
  for i_freq=1:NFreqs
    %% contribution from aos, testing positiveness
    powAO = squeeze(AOdata(:,:,i_freq));
    [U1,S1,V1] = svd(powAO.'); % testing hermitian symmetry and definite positiveness
    if (sum(S1<0)>0)
      error('AO covariance matrix is not positive')
    elseif norm(U1-V1')>1e-15*sqrt(numel(U1))
      error('AO covariance matrix is not hermitian symmetric')
    end
    %% contribution from PZmodels, testing positiveness
    tfPZ = squeeze(PZdata(:,:,i_freq));
    powPZ = tfPZ * tfPZ';
    %% summing all three contributions sources, computing CPSD
    pow = blkdiag(powAO, powWhiteNoise, powPZ);
    RespLoc = squeeze(resps(:,:,i_freq));
    noise = RespLoc * pow * RespLoc' * (2*timestep) ;
    if diagOnly
      Result(:,i_freq) = real(diag(noise)) ;
    else
      Result(:,:,i_freq) = noise ;
    end
  end
  
  %% saving in aos
  if diagOnly    % making a psd only
    ao_out = ao.initObjectWithSize(Noutputs, 1);
    for io=1:Noutputs
        ao_out(io).setData(fsdata(freqs, squeeze(Result(io,:)) ));
        ao_out(io).setName( ['PSD of ' , OutputName{io}]);
        ao_out(io).setXunits('Hz');
        ao_out(io).setYunits(outputvarunits(io)*outputvarunits(io)/unit('Hz'));
        ao_out(io).setDescription( ['PSD of ' , OutputName{io}]);
    end
  else    % making a cpsd matrix
    ao_out = ao.initObjectWithSize(Noutputs, Noutputs);
    for io=1:Noutputs
      for jo=1:Noutputs
        ao_out(io,jo).setData(fsdata(freqs, squeeze(Result(jo,io,:)) ));
        ao_out(io,jo).setXunits('Hz');
        ao_out(io,jo).setYunits(outputvarunits(io)*outputvarunits(jo)/unit('Hz'));
        if io~=jo
          ao_out(io,jo).setName( ['Cross PSD of ', OutputName{jo}, ' and ', OutputName{io}]);
          ao_out(io,jo).setDescription( ['Cross PSD of ', OutputName{jo}, ' and ', OutputName{io}]);
        else
          ao_out(io,jo).setName( ['PSD of ' , OutputName{jo}]);
          ao_out(io,jo).setDescription( ['PSD of ' , OutputName{jo}]);
        end
      end
    end
  end
  
  %% construct output matrix object
  out = matrix(ao_out);
  if callerIsMethod
    % do nothing
  else
    myinfo = getInfo('None');
    out.addHistory(myinfo, pl , ssm_invars(1), inhist );
  end
  
  %% Set output depending on nargout
  if nargout == 1;
    varargout = {out};
  elseif nargout == 0;
    iplot(ao_out);
  else
    error('Wrong number of outputs')
  end
end


%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.op, '$Id: cpsdForCorrelatedInputs.m,v 1.2 2011/05/23 14:18:20 adrien Exp $', sets, pl);
  
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function pl = getDefaultPlist()
  pl = ssm.getInfo('reorganize', 'for cpsdForCorrelatedInputs').plists;
  pl.remove('set');
  
  p = param({'covariance', 'The covariance matrix of this noise between input ports for the <i>time-discrete</i> noise model.'}, []);
  pl.append(p);
  
  p = param({'CPSD', 'The one sided cpsd matrix of the white noise between input ports.'}, []);
  pl.append(p);
  
  p = param({'aos', 'An array of input AOs, provides the cpsd of the input noise.'}, ao.initObjectWithSize(1,0));
  pl.append(p);
  
  p = param({'PZmodels', 'An array of input pzmodels, used to filter the input noise.'}, paramValue.DOUBLE_VALUE(zeros(0,1))); 
  pl.append(p);
  
  p = param({'reorganize', 'When set to 0, this means the ssm does not need be modified to match the requested i/o. Faster but dangerous!'}, paramValue.TRUE_FALSE);
  pl.append(p);

  p = param({'f2', 'The maximum frequency. Default is Nyquist or 1Hz.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  p = param({'f1', 'The minimum frequency. Default is f2*1e-5.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  p = param({'nf', 'The number of frequency bins. Frequencies are scale logarithmically'}, paramValue.DOUBLE_VALUE(200));
  pl.append(p);
  
  p = param({'diagonal only', 'Set to true if you want the PSD instead of the CPSD'}, paramValue.TRUE_FALSE);
  pl.append(p);

end

