% Compute all probe signals from fields (see computeFields)
%
% [sigDC, sigAC] = computeSignals(opt, fDC, fAC)
% opt - Optickle model
% fDC - DC fields at this position (Nfld x Nrf)
% fAC - transfer matrix (Nrff x Nopt x Naf)
% sigDC - DC signals for each probe (Nprb x 1)
% sigAC - transfer matrix for each probe (Nprb x Nopt x Naf)
%
% Example:
% f = logspace(0, 3, 300);
% opt = optFP;
% [fDC, fAC, mechTF] = computeFields(opt, [], f);
% [sigDC, sigAC] = computeSignals(opt, fDC, fAC);

function [sigDC, sigAC] = computeSignals(opt, fDC, fAC)

  % === Get Field Source Info
  vFrf = getFieldSource(opt);

  % ==== Sizes of Things
  Nopt = opt.Noptic;			% number of optics
  Nlnk = opt.Nlink;			% number of links
  Nprb = opt.Nprobe;			% number of probes
  Nrf  = size(fDC, 2);			% number of RF components
  Narf = size(fAC, 1);			% number of audio field components
  Naf  = size(fAC, 3);			% number of audio frequencies
  
  % ==== Compute Signals
  sigDC = zeros(Nprb, 1);
  sigAC = zeros(Nprb, Nopt, Naf);
  
  % loop over probes
  for nPrb = 1:Nprb
    prb = opt.probe(nPrb);
    
    nFld = prb.nField;
    phi = exp(pi * i * prb.phase / 180);
    
    % look for RF frequency matches
    for mRF = 1:Nrf
      for pRF = mRF:Nrf
        if abs(vFrf(pRF) - vFrf(mRF) - prb.freq) < 1e-3
          % ==== Field Components
          % for the lower RF component
          aDCm = fDC(nFld, mRF) * phi;
          aAFmm = fAC(2 * Nlnk * (mRF - 0.5) + nFld, :, :);
          aAFmp = fAC(2 * Nlnk * (mRF - 1) + nFld, :, :);
           
          % for the upper RF component
          aDCp = fDC(nFld, pRF) * conj(phi);
          aAFpm = fAC(2 * Nlnk * (pRF - 0.5) + nFld, :, :);
          aAFpp = fAC(2 * Nlnk * (pRF - 1) + nFld, :, :);
          
          % ==== Add this RF beat to the signals
          sigAC(nPrb, :, :) = sigAC(nPrb, :, :) + ...
              (conj(aDCm) * aAFpm + aDCm * aAFpp + ...
               aDCp * aAFmp + conj(aDCp) * aAFmm);

          sigDC(nPrb) = sigDC(nPrb) + real(conj(fDC(nFld, pRF)) * aDCm);
        end
      end
    end
  end
