11#ifndef __epsic_modulated_h 
   12#define __epsic_modulated_h 
   32  class modulated_mode : 
public field_transformer
 
   35    mutable double tot, totsq;
 
   41    modulated_mode (mode* s) : field_transformer(s)
 
   44      tot=0; totsq=0; count=0; 
 
   49    virtual double modulation () = 0;
 
   52    virtual double get_mod_mean () 
const = 0;
 
   55    virtual double get_mod_variance () 
const = 0;
 
   57    Spinor<double> transform (
const Spinor<double>& field)
 
   59      double mod = modulation();
 
   65      return sqrt(mod) * field;
 
   68    Matrix<4,4,double> get_covariance ()
 const 
   70      double mean = get_mod_mean();
 
   71      double var = get_mod_variance();
 
   74      cerr << 
"modulated_mode::get_covariance expected mean=" << mean
 
   75    << 
" var=" << var << endl;
 
   79      cerr << 
" measured mean=" << tot << 
" var=" << totsq << endl;
 
   82      Matrix<4,4,double> C = source->get_covariance();
 
   83      C *= (mean*mean + var);
 
   84      Matrix<4,4,double> o = outer (source->get_mean(), source->get_mean());
 
   89    Stokes<double> get_mean ()
 const 
   91      return get_mod_mean () * source->get_mean();
 
   96  class lognormal_mode : 
public modulated_mode
 
  104    lognormal_mode (mode* s, 
double beta) : modulated_mode (s) { set_beta (beta); }
 
  106    void set_beta (
double beta)
 
  108      log_sigma = sqrt( log( beta*beta + 1.0 ) );
 
  111    double get_beta ()
 const 
  113      return sqrt( get_mod_variance() );
 
  117    double get_log_sigma ()
 const 
  125      return exp ( log_sigma * (get_normal()->evaluate() - 0.5*log_sigma) ) ;
 
  128    double get_mod_mean ()
 const { 
return 1.0; }
 
  130    double get_mod_variance ()
 const { 
return exp(log_sigma*log_sigma) - 1.0; }
 
  135  class boxcar_modulated_mode : 
public modulated_mode
 
  137    std::vector< double > instances;
 
  144      instances.resize (smooth);
 
  145      for (
unsigned i=1; i<smooth; i++)
 
  146        instances[i] = mod->modulation();
 
  153    boxcar_modulated_mode (modulated_mode* s, 
unsigned n)
 
  154      : modulated_mode(s->get_source()) { smooth = n; mod = s; }
 
  158      if (instances.size() < smooth)
 
  161      instances[current] = mod->modulation();
 
  162      current = (current + 1) % smooth;
 
  165      for (
unsigned i=0; i<smooth; i++)
 
  166        result += instances[i];
 
  173    double get_mod_variance ()
 const 
  175      return mod->get_mod_variance() / smooth;
 
  178    double get_mod_mean ()
 const 
  180      return mod->get_mod_mean ();
 
  183    Matrix<4,4, double> get_crosscovariance (
unsigned ilag)
 const 
  188      Matrix<4,4, double> result = outer(source->get_mean(), source->get_mean());
 
  189      result *= double (smooth - ilag) / smooth * get_mod_variance();
 
  196  class square_modulated_mode : 
public modulated_mode
 
  204    std::vector<double> cross_correlation;
 
  208    square_modulated_mode (modulated_mode*,
 
  210        unsigned sample_size);
 
  212    void compute_cross_correlation (
unsigned sample_size);
 
  216      if (current == width)
 
  218        value = mod->modulation();
 
  226    double get_mod_variance ()
 const 
  228      return mod->get_mod_variance();
 
  231    double get_mod_mean ()
 const 
  233      return mod->get_mod_mean ();
 
  237    Matrix<4,4, double> get_crosscovariance (
unsigned ilag)
 const 
  242      Matrix<4,4, double> result = outer(source->get_mean(), source->get_mean());
 
  243      result *= cross_correlation[ilag] * get_mod_variance();