| 1 | #include "mbeamcyl.h"
 | 
|---|
| 2 | #include "fftpserver.h"
 | 
|---|
| 3 | #include "vector3d.h"
 | 
|---|
| 4 | #include "matharr.h"
 | 
|---|
| 5 | #include "srandgen.h"
 | 
|---|
| 6 | 
 | 
|---|
| 7 | static double cLight=0.3;       // in 1E9 m/s
 | 
|---|
| 8 | static double tClock = 2.; // should come from param file !!!!
 | 
|---|
| 9 | //static double cLight=1;       
 | 
|---|
| 10 | //static double tClock = 1.; 
 | 
|---|
| 11 | 
 | 
|---|
| 12 | //=================================================
 | 
|---|
| 13 | 
 | 
|---|
| 14 | MultiBeamCyl::MultiBeamCyl(int nr, int ns, double posx, double posy)
 | 
|---|
| 15 |   : texact_(ns) , tjitt_(ns) , toffset_(nr) ,
 | 
|---|
| 16 |     gain_(nr), signal_(ns), sigjitt_(ns) 
 | 
|---|
| 17 | {
 | 
|---|
| 18 |   NR_ = nr;
 | 
|---|
| 19 |   NS_ = ns;
 | 
|---|
| 20 |   posY_ = posy;
 | 
|---|
| 21 |   posX_ = posx;
 | 
|---|
| 22 | 
 | 
|---|
| 23 |   SetPrintLevel(0);
 | 
|---|
| 24 | 
 | 
|---|
| 25 |   SetBaseFreqDa(2., 0.25);
 | 
|---|
| 26 |   SetNoiseSigma(0.);
 | 
|---|
| 27 |   SetTimeJitter(0.);
 | 
|---|
| 28 |   SetTimeOffsetSigma(0.);
 | 
|---|
| 29 |   SetGains(1., 0., 0);
 | 
|---|
| 30 |   adfg_ = false; src_ = NULL;
 | 
|---|
| 31 |   SetSources(new BRSourceGen, true);
 | 
|---|
| 32 | }
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //-----------------------------------------------------------------------------
 | 
|---|
| 35 | MultiBeamCyl::~MultiBeamCyl()
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   if (adfg_ && src_) delete src_;
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | //-----------------------------------------------------------------------------
 | 
|---|
| 41 | void MultiBeamCyl::SetSources(BRSourceGen* brs, bool ad)
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   if (brs == NULL) return;
 | 
|---|
| 44 |   if (adfg_ && src_) delete src_;
 | 
|---|
| 45 |   src_ = brs;  adfg_=ad;
 | 
|---|
| 46 |   if (PrtLev_ > 1) 
 | 
|---|
| 47 |     cout << " MultiBeamCyl::SetSources(brs=" << brs <<" ,bool) NbSrc=" 
 | 
|---|
| 48 |          << src_->NbSources() << endl;
 | 
|---|
| 49 | 
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | //-----------------------------------------------------------------------------
 | 
|---|
| 53 | void MultiBeamCyl::SetGains(double g, double sigg, int nzerogain)
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   if (sigg < 1.e-6)  gain_ = g;
 | 
|---|
| 56 |   else gain_ = RandomSequence(RandomSequence::Gaussian, g, sigg);
 | 
|---|
| 57 |   int k;
 | 
|---|
| 58 |   for (k=0; k<NR_; k++) if (gain_(k) < 0) gain_(k) = 0.;
 | 
|---|
| 59 |   for(k=0; k<nzerogain; k++) {
 | 
|---|
| 60 |     int zg = random()%NR_;
 | 
|---|
| 61 |     if ((zg >=0) && (zg < NR_))  gain_(zg) = 0.; 
 | 
|---|
| 62 |   }
 | 
|---|
| 63 |   if (PrtLev_ > 1) 
 | 
|---|
| 64 |     cout << " MultiBeamCyl::SetGains(g=" << g <<" ,sigg=" << sigg 
 | 
|---|
| 65 |          << " ,nzg=" << nzerogain << " )" << endl;
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | //-----------------------------------------------------------------------------
 | 
|---|
| 69 | void MultiBeamCyl::ComputeTimeVectors()
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   texact_ = RegularSequence(0., tClock);        // 0, tClock, 2*tClock, ...
 | 
|---|
| 72 | //  for (int i=0;i<1024;i++) {cout << texact(i)<< endl;};
 | 
|---|
| 73 |   toffset_ = RandomSequence(RandomSequence::Gaussian, 0., toffsig_);
 | 
|---|
| 74 |   NewTJitVector();
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | //-----------------------------------------------------------------------------
 | 
|---|
| 78 | void MultiBeamCyl::NewTJitVector(int num)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   if (timejitter_ > 1.e-19) {
 | 
|---|
| 81 |     tjitt_ = RandomSequence(RandomSequence::Gaussian, 0., timejitter_);
 | 
|---|
| 82 |     tjitt_ += texact_;  
 | 
|---|
| 83 |   }
 | 
|---|
| 84 |   else tjitt_ = texact_;  
 | 
|---|
| 85 |   if (num >= 0) tjitt_ += toffset_(num);
 | 
|---|
| 86 |   
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | //-----------------------------------------------------------------------------
 | 
|---|
| 90 | int MultiBeamCyl::ComputeSignalVector(int num, bool fgsignojit)
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   int nok = 0;
 | 
|---|
| 93 |   signal_ = 0.;
 | 
|---|
| 94 |   sigjitt_ = 0.;
 | 
|---|
| 95 |   for(int is=0; is<src_->freq.Size(); is++) {
 | 
|---|
| 96 |     double fr = src_->freq(is);
 | 
|---|
| 97 |     if ((fr < 0.) || (fr > 0.5)) continue;
 | 
|---|
| 98 |     nok++;
 | 
|---|
| 99 |     // Pour le dephasage entre recepteurs, on doit utiliser la frequence vraie,
 | 
|---|
| 100 |     // pas celle apres shift (freq-reduite)
 | 
|---|
| 101 |     // lambda = c T = c/freq avec c = 1, dephasage = 2*pi*num*Da*sin(ang)/lambda
 | 
|---|
| 102 |     double dephasage = (posX_+num*Da_)*sin(src_->angX(is)) 
 | 
|---|
| 103 |                                 + posY_*sin(src_->angY(is)) ;
 | 
|---|
| 104 |     dephasage *= 2*M_PI*(fr+freq0_)/cLight;
 | 
|---|
| 105 |     // On ajoute alors la phase propre de chaque source
 | 
|---|
| 106 |     dephasage += src_->phase(is);
 | 
|---|
| 107 |     double amprep = src_->amp(is)*AngResponse(src_->angX(is), src_->angY(is));
 | 
|---|
| 108 |     for(int k=0; k<NS_; k++) {
 | 
|---|
| 109 |       sigjitt_(k) += amprep*sin(2.*M_PI*fr*tjitt_(k)+dephasage);
 | 
|---|
| 110 |       if (fgsignojit) 
 | 
|---|
| 111 |         signal_(k) += amprep*sin(2.*M_PI*fr*texact_(k)+dephasage);
 | 
|---|
| 112 |     }
 | 
|---|
| 113 |   }
 | 
|---|
| 114 |   // Application du gain du detecteur 
 | 
|---|
| 115 |   r_4 ga = gain_(num);
 | 
|---|
| 116 |   if (fabs(ga-1.) > 1.e-9) {
 | 
|---|
| 117 |     signal_ *= ga;
 | 
|---|
| 118 |     sigjitt_ *= ga;
 | 
|---|
| 119 |   }
 | 
|---|
| 120 |   // Ajout de bruit (ampli ...) 
 | 
|---|
| 121 |   if (signoise_ > 1.e-18) {
 | 
|---|
| 122 |     for(int k=0; k<NS_; k++) {
 | 
|---|
| 123 |       sigjitt_(k) += GauRnd(0., signoise_);
 | 
|---|
| 124 |       if (fgsignojit) signal_(k) += GauRnd(0., signoise_);
 | 
|---|
| 125 |     }
 | 
|---|
| 126 |   }
 | 
|---|
| 127 | //............      FFT in time
 | 
|---|
| 128 |   FFTPackServer ffts;
 | 
|---|
| 129 |   ffts.FFTForward(sigjitt_, f_sigjit_);
 | 
|---|
| 130 |   if (fgsignojit) ffts.FFTForward(signal_, f_sig_);
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   return nok;
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | 
 | 
|---|
| 136 | //-----------------------------------------------------------------------------
 | 
|---|
| 137 | /*  --- a supprimer ?
 | 
|---|
| 138 | inline float myZmodule(complex<r_4>& z) 
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   return (float)sqrt((double)(z.real()*z.real()+z.imag()*z.imag()));
 | 
|---|
| 141 | }
 | 
|---|
| 142 | ----- */
 | 
|---|
| 143 | 
 | 
|---|
| 144 | //-----------------------------------------------------------------------------
 | 
|---|
| 145 | void MultiBeamCyl::ReconstructSourcePlane(bool fgzerocentre)
 | 
|---|
| 146 | {
 | 
|---|
| 147 |   ComputeTimeVectors();
 | 
|---|
| 148 |   int noksrc = ComputeSignalVector(0, false);
 | 
|---|
| 149 |   vector<TVector< complex<r_4> > >  vvfc;
 | 
|---|
| 150 |   cout << "MultiBeamCyl::ReconstructSourcePlane() NR=" << NR_ 
 | 
|---|
| 151 |        << " PosY=" << posY_ << " NFreq=" << f_sigjit_.Size()-1 << " NOkSrc=" << noksrc << endl;
 | 
|---|
| 152 |   if (PrtLev_ > 0) {
 | 
|---|
| 153 |     cout << " ... posY= " << posY_ << " MeanGain=" << Mean(gain_) << " MeanAmpSrc=" 
 | 
|---|
| 154 |          << Mean(src_->amp) << endl;
 | 
|---|
| 155 |     cout << " ...SigNoise=" << signoise_ << " TimeJitter=" << timejitter_ 
 | 
|---|
| 156 |          << " TOffSig=" << toffsig_ << " Da=" << Da_ << " Freq0=" << freq0_ << endl;
 | 
|---|
| 157 |   }
 | 
|---|
| 158 |   // On ne s'occupe pas de la composante continue
 | 
|---|
| 159 |   for(int jf=1; jf<f_sigjit_.Size(); jf++) {
 | 
|---|
| 160 |     TVector<complex<r_4> > cf(NR_);
 | 
|---|
| 161 |     vvfc.push_back(cf);
 | 
|---|
| 162 |   }
 | 
|---|
| 163 |   cout << "ReconstructSourcePlane()/Info: computing s(t) for each receptor ..." << endl;
 | 
|---|
| 164 |   int pmod = NR_/10;
 | 
|---|
| 165 |   for(int ir=0; ir<NR_; ir++) {
 | 
|---|
| 166 |     if (timejitter_ > 1.e-19) NewTJitVector(ir);
 | 
|---|
| 167 |     noksrc = ComputeSignalVector(ir, false);
 | 
|---|
| 168 |     for(int jf=1; jf<f_sigjit_.Size(); jf++)
 | 
|---|
| 169 |       vvfc[jf-1](ir) = f_sigjit_(jf);
 | 
|---|
| 170 |     if ( (PrtLev_>0) && (ir%pmod == 0) )
 | 
|---|
| 171 |       cout << " OK s(t) for ir=" << ir << " / NR=" << NR_ << " NOkSrc=" << noksrc << endl;
 | 
|---|
| 172 |   }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   cout << "ReconstructSourcePlane()/Info: computing s(ang) for each freq by FFT" << endl;
 | 
|---|
| 175 |   cmplx_srcplane_.SetSize(f_sigjit_.Size()-1, NR_);
 | 
|---|
| 176 |   // rec_srcplane.SetSize(f_sigjit_.Size()-1, NR_);
 | 
|---|
| 177 |   FFTPackServer ffts;
 | 
|---|
| 178 |   TVector<complex<r_4> > fcf;
 | 
|---|
| 179 |   pmod = vvfc.size()/10;
 | 
|---|
| 180 |   for(int jf=0; jf<(int)vvfc.size(); jf++) {            // loop over frequencies
 | 
|---|
| 181 |     ffts.FFTForward(vvfc[jf], fcf);     // FFT alomg cylinder
 | 
|---|
| 182 |     if (fcf.Size() != NR_) {
 | 
|---|
| 183 |       cout << "ReconstructSourcePlane()/BUG jf=" << jf << " fcf.Size() != NR "
 | 
|---|
| 184 |            << fcf.Size() << " != " << NR_ << endl;
 | 
|---|
| 185 |       continue;
 | 
|---|
| 186 |     }
 | 
|---|
| 187 |     if (fgzerocentre) {  // On veut avoir la direction angle=0 au milieu de la matrice
 | 
|---|
| 188 |       int milieu = (NR_-1)/2;
 | 
|---|
| 189 |       if (NR_%2 == 0) milieu++;
 | 
|---|
| 190 |       int decal = NR_ - milieu - 1;
 | 
|---|
| 191 |       for (int ir=0; ir<=milieu; ir++) 
 | 
|---|
| 192 |         cmplx_srcplane_(jf, ir+decal) = fcf(ir);
 | 
|---|
| 193 |       //        rec_srcplane(jf, ir+decal) = myZmodule(fcf(ir));
 | 
|---|
| 194 | 
 | 
|---|
| 195 |       for (int ir=milieu+1; ir<NR_; ir++) 
 | 
|---|
| 196 |         cmplx_srcplane_(jf, decal-(NR_-ir)) = fcf(ir);
 | 
|---|
| 197 |       //        rec_srcplane_(jf, decal-(NR_-ir)) = myZmodule(fcf(ir));
 | 
|---|
| 198 |       
 | 
|---|
| 199 |     }
 | 
|---|
| 200 |     else for (int ir=0; ir<NR_; ir++) 
 | 
|---|
| 201 |       cmplx_srcplane_(jf, ir) = fcf(ir);
 | 
|---|
| 202 |     //      rec_srcplane(jf, ir) = myZmodule(fcf(ir));
 | 
|---|
| 203 | 
 | 
|---|
| 204 |     if ( (PrtLev_ > 0) && (jf%pmod == 0)) 
 | 
|---|
| 205 |       cout << " OK rec_srcplane(jf, ir) for jf=" << jf << endl;
 | 
|---|
| 206 |   }
 | 
|---|
| 207 | 
 | 
|---|
| 208 |   
 | 
|---|
| 209 |   cout << "ReconstructSourcePlane()/Info: rec_srcplane computed OK" << endl;
 | 
|---|
| 210 | }
 | 
|---|
| 211 | 
 | 
|---|
| 212 | 
 | 
|---|