[3756] | 1 |
|
---|
[3930] | 2 | /* ------------------------ Projet BAORadio --------------------
|
---|
| 3 | Classes to compute 3D power spectrum and noise power spectrum
|
---|
| 4 | R. Ansari - Nov 2008 ... Dec 2010
|
---|
| 5 | --------------------------------------------------------------- */
|
---|
| 6 |
|
---|
[3756] | 7 | #include "specpk.h"
|
---|
[4026] | 8 | #include "radutil.h"
|
---|
[3756] | 9 | #include "randr48.h"
|
---|
[3930] | 10 | #include "ctimer.h"
|
---|
[3756] | 11 |
|
---|
| 12 | //------------------------------------
|
---|
| 13 | // Class SpectralShape
|
---|
| 14 | // -----------------------------------
|
---|
| 15 |
|
---|
| 16 | double Pnu1(double nu)
|
---|
| 17 | {
|
---|
| 18 | return ( sqrt(sqrt(nu)) / ((nu+1.0)/0.2) *
|
---|
| 19 | (1+0.2*cos(2*M_PI*(nu-2.)*0.15)*exp(-nu/50.)) );
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | double Pnu2(double nu)
|
---|
| 23 | {
|
---|
| 24 | if (nu < 1.e-9) return 0.;
|
---|
| 25 | return ((1.-exp(-nu/0.5))/nu*(1+0.25*cos(2*M_PI*nu*0.1)*exp(-nu/20.)) );
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | double Pnu3(double nu)
|
---|
| 30 | {
|
---|
| 31 | return ( log(nu/100.+1)*(1+sin(2*M_PI*nu/300))*exp(-nu/4000) );
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | double Pnu4(double nu)
|
---|
| 36 | {
|
---|
| 37 | double x = (nu-0.5)/0.05;
|
---|
| 38 | double rc = 2*exp(-x*x);
|
---|
| 39 | x = (nu-3.1)/0.27;
|
---|
| 40 | rc += exp(-x*x);
|
---|
| 41 | x = (nu-7.6)/1.4;
|
---|
| 42 | rc += 0.5*exp(-x*x);
|
---|
| 43 | return ( rc+2.*exp(-x*x) );
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | //--------------------------------------------------
|
---|
| 47 | // -- SpectralShape class : test P(k) class
|
---|
| 48 | //--------------------------------------------------
|
---|
| 49 | // Constructor
|
---|
| 50 | SpectralShape::SpectralShape(int typ)
|
---|
| 51 | {
|
---|
| 52 | typ_=typ;
|
---|
[3825] | 53 | SetRenormFac();
|
---|
[3756] | 54 | }
|
---|
| 55 |
|
---|
| 56 | // Return the spectral power for a given wave number wk
|
---|
| 57 | double SpectralShape::operator() (double wk)
|
---|
| 58 | {
|
---|
| 59 | wk/=DeuxPI;
|
---|
[3825] | 60 | double retv=1.;
|
---|
[3756] | 61 | switch (typ_)
|
---|
| 62 | {
|
---|
| 63 | case 1:
|
---|
[3825] | 64 | retv=Pnu1(wk);
|
---|
[3756] | 65 | break;
|
---|
| 66 | case 2:
|
---|
[3825] | 67 | retv=Pnu2(wk);
|
---|
[3756] | 68 | break;
|
---|
| 69 | case 3:
|
---|
[3825] | 70 | retv=Pnu3(wk);
|
---|
[3756] | 71 | break;
|
---|
| 72 | case 4:
|
---|
[3825] | 73 | retv=Pnu4(wk);
|
---|
[3756] | 74 | break;
|
---|
| 75 | default :
|
---|
| 76 | {
|
---|
| 77 | // global shape
|
---|
| 78 | double csp = pow( (2*sin(sqrt(sqrt(wk/7.)))),2.);
|
---|
| 79 | if (csp < 0.) return 0.;
|
---|
| 80 |
|
---|
| 81 | // Adding some pics
|
---|
| 82 | double picpos[5] = {75.,150.,225.,300.,375.,};
|
---|
| 83 |
|
---|
| 84 | for(int k=0; k<5; k++) {
|
---|
| 85 | double x0 = picpos[k];
|
---|
| 86 | if ( (wk > x0-25.) && (wk < x0+25.) ) {
|
---|
| 87 | double x = (wk-x0);
|
---|
| 88 | csp *= (1.+0.5*exp(-(x*x)/(2.*5*5)));
|
---|
| 89 | break;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
[3825] | 92 | retv=csp;
|
---|
[3756] | 93 | }
|
---|
| 94 | break;
|
---|
| 95 | }
|
---|
[3825] | 96 | return retv*renorm_fac;
|
---|
[3756] | 97 | }
|
---|
| 98 | // Return a vector representing the power spectrum (for checking)
|
---|
| 99 | Histo SpectralShape::GetPk(int n)
|
---|
| 100 | {
|
---|
| 101 | if (n<16) n = 256;
|
---|
| 102 | Histo h(0.,1024.*DeuxPI,n);
|
---|
| 103 | for(int k=0; k<h.NBins(); k++) h(k) = Value((k+0.5)*h.BinWidth());
|
---|
| 104 | return h;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[3825] | 107 | double SpectralShape::Sommek2Pk(double kmax, int n)
|
---|
| 108 | {
|
---|
| 109 | double dk=kmax/(double)n;
|
---|
| 110 | double s=0.;
|
---|
| 111 | for(int i=1; i<=n; i++) {
|
---|
| 112 | double ck=(double)i*dk;
|
---|
| 113 | s += Value(ck)*ck*ck;
|
---|
| 114 | }
|
---|
| 115 | return s*dk*4.*M_PI;
|
---|
| 116 | }
|
---|
[3756] | 117 | //--------------------------------------------------
|
---|
| 118 | // -- Four2DResponse class : test P(k) class
|
---|
| 119 |
|
---|
| 120 | //---------------------------------------------------------------
|
---|
| 121 | // -- Four3DPk class : 3D fourier amplitudes and power spectrum
|
---|
| 122 | //---------------------------------------------------------------
|
---|
| 123 | // Constructeur avec Tableau des coeff. de Fourier en argument
|
---|
| 124 | Four3DPk::Four3DPk(TArray< complex<TF> > & fourcoedd, RandomGeneratorInterface& rg)
|
---|
| 125 | : rg_(rg), fourAmp(fourcoedd)
|
---|
| 126 | {
|
---|
| 127 | SetPrtLevel();
|
---|
| 128 | SetCellSize();
|
---|
[4027] | 129 | hp_pk_p_=NULL; hmcnt_p_=NULL; hmcntok_p_=NULL; s2cut_=0.;
|
---|
[3756] | 130 | }
|
---|
| 131 | // Constructor
|
---|
| 132 | Four3DPk::Four3DPk(RandomGeneratorInterface& rg, sa_size_t szx, sa_size_t szy, sa_size_t szz)
|
---|
| 133 | : rg_(rg), fourAmp(szx, szy, szz)
|
---|
| 134 | {
|
---|
| 135 | SetPrtLevel();
|
---|
| 136 | SetCellSize();
|
---|
[4027] | 137 | hp_pk_p_=NULL; hmcnt_p_=NULL; hmcntok_p_=NULL; s2cut_=0.;
|
---|
[3756] | 138 | }
|
---|
| 139 |
|
---|
[4027] | 140 | // Destructor
|
---|
| 141 | Four3DPk::~Four3DPk()
|
---|
| 142 | {
|
---|
| 143 | if (hp_pk_p_) delete hp_pk_p_;
|
---|
| 144 | if (hmcnt_p_) delete hmcnt_p_;
|
---|
| 145 | if (hmcntok_p_) delete hmcntok_p_;
|
---|
| 146 | }
|
---|
[3756] | 147 |
|
---|
| 148 | // Generate mass field Fourier Coefficient
|
---|
| 149 | void Four3DPk::ComputeFourierAmp(SpectralShape& pk)
|
---|
| 150 | {
|
---|
| 151 | // We generate a random gaussian real field
|
---|
| 152 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 153 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 154 | // double fnorm = 1./sqrt(2.*fourAmp.Size());
|
---|
| 155 | double fnorm = 1.;
|
---|
| 156 | double kxx, kyy, kzz;
|
---|
| 157 | // sa_size_t is large integer type
|
---|
| 158 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
| 159 | kzz = (kz>fourAmp.SizeZ()/2) ? (double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
| 160 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
| 161 | kyy = (ky>fourAmp.SizeY()/2) ? (double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
| 162 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
| 163 | double kxx=(double)kx*dkx_;
|
---|
| 164 | double wk = sqrt(kxx*kxx+kyy*kyy+kzz*kzz);
|
---|
| 165 | double amp = sqrt(pk(wk)*fnorm/2.);
|
---|
| 166 | fourAmp(kx, ky, kz) = complex<TF>(rg_.Gaussian(amp), rg_.Gaussian(amp)); // renormalize fourier coeff usin
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
[3930] | 170 | if (prtlev_>2)
|
---|
[3756] | 171 | cout << " Four3DPk::ComputeFourierAmp() done ..." << endl;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[4026] | 174 |
|
---|
[3756] | 175 | // Generate mass field Fourier Coefficient
|
---|
[4026] | 176 | void Four3DPk::ComputeNoiseFourierAmp(Four2DResponse& resp, double angscale, bool crmask)
|
---|
| 177 | // angscale is a multiplicative factor converting transverse k (wave number) values to angular wave numbers
|
---|
| 178 | // typically = ComovRadialDistance
|
---|
[3756] | 179 | {
|
---|
| 180 | TMatrix<r_4> mask(fourAmp.SizeY(), fourAmp.SizeX());
|
---|
| 181 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 182 | // The second half of the array along Y and Z contain negative frequencies
|
---|
[3787] | 183 | double kxx, kyy, kzz, rep, amp;
|
---|
[3756] | 184 | // sa_size_t is large integer type
|
---|
| 185 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
[3769] | 186 | kzz = (kz>fourAmp.SizeZ()/2) ? -(double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
[3756] | 187 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
[3769] | 188 | kyy = (ky>fourAmp.SizeY()/2) ? -(double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
[3756] | 189 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
[3787] | 190 | kxx=(double)kx*dkx_;
|
---|
[4026] | 191 | rep = resp(kxx*angscale, kyy*angscale);
|
---|
[3756] | 192 | if (crmask&&(kz==0)) mask(ky,kx)=((rep<1.e-8)?9.e9:(1./rep));
|
---|
| 193 | if (rep<1.e-8) fourAmp(kx, ky, kz) = complex<TF>(9.e9,0.);
|
---|
| 194 | else {
|
---|
[3787] | 195 | amp = 1./sqrt(rep)/sqrt(2.);
|
---|
[3756] | 196 | fourAmp(kx, ky, kz) = complex<TF>(rg_.Gaussian(amp), rg_.Gaussian(amp));
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
[3930] | 201 | if (prtlev_>2) fourAmp.Show();
|
---|
[3756] | 202 | if (crmask) {
|
---|
| 203 | POutPersist po("mask.ppf");
|
---|
| 204 | po << mask;
|
---|
| 205 | }
|
---|
| 206 | if (prtlev_>0)
|
---|
| 207 | cout << " Four3DPk::ComputeNoiseFourierAmp() done ..." << endl;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[4026] | 210 | // Generate mass field Fourier Coefficient
|
---|
[4030] | 211 | void Four3DPk::ComputeNoiseFourierAmp(Four2DResponse& resp, double f0, double df, Vector& angscales, Vector& noisevec)
|
---|
[4026] | 212 | // angscale is a multiplicative factor converting transverse k (wave number) values to angular wave numbers
|
---|
| 213 | // typically = ComovRadialDistance
|
---|
| 214 | {
|
---|
[4028] | 215 | uint_8 nmodeok=0;
|
---|
[4030] | 216 | if ((angscales.Size() != fourAmp.SizeZ())||(noisevec.Size() != fourAmp.SizeZ()))
|
---|
| 217 | throw SzMismatchError("ComputeNoiseFourierAmp(): angscales/noisevec.Size()!=fourAmp.SizeZ()");
|
---|
[4026] | 218 | H21Conversions conv;
|
---|
| 219 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 220 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 221 | double kxx, kyy, kzz, rep, amp;
|
---|
| 222 | // sa_size_t is large integer type
|
---|
| 223 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
| 224 | conv.setFrequency(f0+kz*df);
|
---|
| 225 | resp.setLambda(conv.getLambda());
|
---|
[4027] | 226 | double angsc=angscales(kz);
|
---|
[4030] | 227 | double noisepow=noisevec(kz);
|
---|
[4027] | 228 | if (prtlev_>2)
|
---|
[4030] | 229 | cout << " Four3DPk::ComputeNoiseFourierAmp(...) - freq=" << f0+kz*df << " -> AngSc=" << angsc
|
---|
| 230 | << " NoisePow=" << noisepow << endl;
|
---|
[4026] | 231 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
| 232 | kyy = (ky>fourAmp.SizeY()/2) ? -(double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
| 233 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
| 234 | kxx=(double)kx*dkx_;
|
---|
[4027] | 235 | rep = resp(kxx*angsc, kyy*angsc);
|
---|
[4028] | 236 | if (rep<1.e-19) rep=1.e-19;
|
---|
| 237 | else nmodeok++;
|
---|
[4030] | 238 | amp = sqrt(noisepow/rep/2.);
|
---|
[4028] | 239 | fourAmp(kx, ky, kz) = complex<TF>(rg_.Gaussian(amp), rg_.Gaussian(amp));
|
---|
[4026] | 240 | }
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[4028] | 244 | if (prtlev_>1) {
|
---|
| 245 | cout << " Four3DPk::ComputeNoiseFourierAmp(...) Computing FFT along frequency ... \n"
|
---|
| 246 | << " NModeOK=" << nmodeok << " / NMode=" << fourAmp.Size()
|
---|
| 247 | << " -> " << 100.*(double)nmodeok/(double)fourAmp.Size() << "%" << endl;
|
---|
| 248 | }
|
---|
[4026] | 249 | TVector< complex<TF> > veczin(fourAmp.SizeZ()), veczout(fourAmp.SizeZ());
|
---|
| 250 | FFTWServer ffts(true);
|
---|
| 251 | ffts.setNormalize(true);
|
---|
| 252 |
|
---|
| 253 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
| 254 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) {
|
---|
| 255 | // veczin=fourAmp(Range(kx), Range(ky), Range::all());
|
---|
| 256 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) veczin(kz)=fourAmp(kx,ky,kz);
|
---|
| 257 | ffts.FFTBackward(veczin,veczout);
|
---|
| 258 | veczout /= (TF)sqrt((double)fourAmp.SizeZ());
|
---|
| 259 | // fourAmp(Range(kx), Range(ky), Range::all())=veczout;
|
---|
| 260 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) fourAmp(kx,ky,kz)=veczout(kz);
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[4028] | 264 | // if (prtlev_>2) fourAmp.Show();
|
---|
[4026] | 265 | if (prtlev_>0)
|
---|
| 266 | cout << " Four3DPk::ComputeNoiseFourierAmp(Four2DResponse& resp, double f0, double df) done ..." << endl;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[3756] | 269 | // Compute mass field from its Fourier Coefficient
|
---|
| 270 | TArray<TF> Four3DPk::ComputeMassDens()
|
---|
| 271 | {
|
---|
| 272 | TArray<TF> massdens;
|
---|
| 273 | // Backward fourier transform of the fourierAmp array
|
---|
| 274 | FFTWServer ffts(true);
|
---|
| 275 | ffts.setNormalize(true);
|
---|
| 276 | ffts.FFTBackward(fourAmp, massdens, true);
|
---|
| 277 | // cout << " Four3DPk::ComputeMassDens() done NbNeg=" << npbz << " / NPix=" << massDens.Size() << endl;
|
---|
| 278 | cout << " Four3DPk::ComputeMassDens() done NPix=" << massdens.Size() << endl;
|
---|
| 279 | return massdens;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | // Compute power spectrum as a function of wave number k
|
---|
| 283 | // cells with amp^2=re^2+im^2>s2cut are ignored
|
---|
| 284 | // Output : power spectrum (profile histogram)
|
---|
[4027] | 285 | HProf Four3DPk::ComputePk(double s2cut, int nbin, double kmin, double kmax, bool fgmodcnt)
|
---|
[3756] | 286 | {
|
---|
| 287 | // The second half of the array along Y (matrix rows) contain
|
---|
| 288 | // negative frequencies
|
---|
| 289 | // int nbh = sqrt(fourAmp.SizeX()*fourAmp.SizeX()+fourAmp.SizeY()*fourAmp.SizeY()/4.+fourAmp.SizeZ()*fourAmp.SizeY()/4.);
|
---|
| 290 | // The profile histogram will contain the mean value of FFT amplitude
|
---|
| 291 | // as a function of wave-number k = sqrt((double)(kx*kx+ky*ky))
|
---|
| 292 | // if (nbin < 1) nbin = nbh/2;
|
---|
[3769] | 293 | if ((kmax<0.)||(kmax<kmin)) {
|
---|
| 294 | kmin=0.;
|
---|
| 295 | double maxx=fourAmp.SizeX()*dkx_;
|
---|
| 296 | double maxy=fourAmp.SizeY()*dky_/2;
|
---|
| 297 | double maxz=fourAmp.SizeZ()*dkz_/2;
|
---|
| 298 | kmax=sqrt(maxx*maxx+maxy*maxy+maxz*maxz);
|
---|
| 299 | }
|
---|
[4027] | 300 | if (nbin<2) nbin=256;
|
---|
| 301 | hp_pk_p_ = new HProf(kmin, kmax, nbin);
|
---|
| 302 | hp_pk_p_->SetErrOpt(false);
|
---|
| 303 | if (fgmodcnt) {
|
---|
| 304 | hmcnt_p_ = new Histo(kmin, kmax, nbin);
|
---|
| 305 | hmcntok_p_ = new Histo(kmin, kmax, nbin);
|
---|
| 306 | }
|
---|
| 307 | s2cut_=s2cut;
|
---|
| 308 | ComputePkCumul();
|
---|
| 309 | return *hp_pk_p_;
|
---|
[3756] | 310 | }
|
---|
| 311 |
|
---|
| 312 | // Compute power spectrum as a function of wave number k
|
---|
| 313 | // Cumul dans hp - cells with amp^2=re^2+im^2>s2cut are ignored
|
---|
[4027] | 314 | void Four3DPk::ComputePkCumul()
|
---|
[3756] | 315 | {
|
---|
[3930] | 316 | uint_8 nmodeok=0;
|
---|
[3756] | 317 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 318 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 319 | double kxx, kyy, kzz;
|
---|
| 320 | // sa_size_t is large integer type
|
---|
[3783] | 321 | // We ignore 0th term in all frequency directions ...
|
---|
[4028] | 322 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
[3756] | 323 | kzz = (kz > fourAmp.SizeZ()/2) ? (double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
[4028] | 324 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
[3756] | 325 | kyy = (ky > fourAmp.SizeY()/2) ? (double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
[4028] | 326 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) { // ignore the 0th coefficient (constant term)
|
---|
| 327 | kxx=(double)kx*dkx_;
|
---|
[3756] | 328 | complex<TF> za = fourAmp(kx, ky, kz);
|
---|
[4027] | 329 | // if (za.real()>8.e19) continue;
|
---|
[3756] | 330 | double wk = sqrt(kxx*kxx+kyy*kyy+kzz*kzz);
|
---|
| 331 | double amp2 = za.real()*za.real()+za.imag()*za.imag();
|
---|
[4027] | 332 | if (hmcnt_p_) hmcnt_p_->Add(wk);
|
---|
| 333 | if ((s2cut_>1.e-9)&&(amp2>s2cut_)) continue;
|
---|
| 334 | if (hmcntok_p_) hmcntok_p_->Add(wk);
|
---|
| 335 | hp_pk_p_->Add(wk, amp2);
|
---|
[3930] | 336 | nmodeok++;
|
---|
[3756] | 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
[4027] | 340 | if ((prtlev_>1)||((prtlev_>0)&&(s2cut_>1.e-9))) {
|
---|
[3930] | 341 | cout << " Four3DPk::ComputePkCumul/Info : NModeOK=" << nmodeok << " / NMode=" << fourAmp.Size()
|
---|
| 342 | << " -> " << 100.*(double)nmodeok/(double)fourAmp.Size() << "%" << endl;
|
---|
| 343 | }
|
---|
[3756] | 344 | return;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[3947] | 347 | // Compute noise power spectrum as a function of wave number k
|
---|
| 348 | // No random generation, computes profile of noise sigma^2
|
---|
| 349 | // cells with amp^2=re^2+im^2>s2cut are ignored
|
---|
| 350 | // Output : noise power spectrum (profile histogram)
|
---|
[4026] | 351 | // angscale is a multiplicative factor converting transverse k (wave number) values to angular wave numbers
|
---|
| 352 | // typically = ComovRadialDistance
|
---|
[4027] | 353 | HProf Four3DPk::ComputeNoisePk(Four2DResponse& resp, double angscale, double s2cut, int nbin, double kmin, double kmax)
|
---|
[3947] | 354 | {
|
---|
| 355 | // The second half of the array along Y (matrix rows) contain
|
---|
| 356 | // negative frequencies
|
---|
| 357 | // int nbh = sqrt(fourAmp.SizeX()*fourAmp.SizeX()+fourAmp.SizeY()*fourAmp.SizeY()/4.+fourAmp.SizeZ()*fourAmp.SizeY()/4.);
|
---|
| 358 | // The profile histogram will contain the mean value of noise sigma^2
|
---|
| 359 | // as a function of wave-number k = sqrt((double)(kx*kx+ky*ky))
|
---|
| 360 | // if (nbin < 1) nbin = nbh/2;
|
---|
[4028] | 361 | double kmax2d=0.;
|
---|
[3947] | 362 | if ((kmax<0.)||(kmax<kmin)) {
|
---|
| 363 | kmin=0.;
|
---|
| 364 | double maxx=fourAmp.SizeX()*dkx_;
|
---|
| 365 | double maxy=fourAmp.SizeY()*dky_/2;
|
---|
| 366 | double maxz=fourAmp.SizeZ()*dkz_/2;
|
---|
| 367 | kmax=sqrt(maxx*maxx+maxy*maxy+maxz*maxz);
|
---|
[4028] | 368 | kmax2d=sqrt(maxx*maxx+maxy*maxy);
|
---|
[3947] | 369 | }
|
---|
[4027] | 370 | if (nbin<2) nbin=256;
|
---|
| 371 | hp_pk_p_ = new HProf(kmin, kmax, nbin);
|
---|
| 372 | hp_pk_p_->SetErrOpt(false);
|
---|
| 373 | hmcnt_p_ = new Histo(kmin, kmax, nbin);
|
---|
| 374 | hmcntok_p_ = new Histo(kmin, kmax, nbin);
|
---|
| 375 | s2cut_=s2cut;
|
---|
[3947] | 376 |
|
---|
| 377 | uint_8 nmodeok=0;
|
---|
| 378 | // fourAmp represent 3-D fourier transform of a real input array.
|
---|
| 379 | // The second half of the array along Y and Z contain negative frequencies
|
---|
| 380 | double kxx, kyy, kzz;
|
---|
| 381 | // sa_size_t is large integer type
|
---|
| 382 | // We ignore 0th term in all frequency directions ...
|
---|
[4028] | 383 | for(sa_size_t kz=0; kz<fourAmp.SizeZ(); kz++) {
|
---|
[3947] | 384 | kzz = (kz > fourAmp.SizeZ()/2) ? (double)(fourAmp.SizeZ()-kz)*dkz_ : (double)kz*dkz_;
|
---|
[4028] | 385 | uint_8 nmodeok_kz=0;
|
---|
| 386 | for(sa_size_t ky=0; ky<fourAmp.SizeY(); ky++) {
|
---|
[3947] | 387 | kyy = (ky > fourAmp.SizeY()/2) ? (double)(fourAmp.SizeY()-ky)*dky_ : (double)ky*dky_;
|
---|
[4028] | 388 | for(sa_size_t kx=0; kx<fourAmp.SizeX(); kx++) { // ignore the 0th coefficient (constant term)
|
---|
| 389 | kxx=(double)kx*dkx_;
|
---|
[3947] | 390 | double wk = sqrt(kxx*kxx+kyy*kyy+kzz*kzz);
|
---|
[4026] | 391 | double rep=resp(kxx*angscale, kyy*angscale);
|
---|
| 392 | double amp2 = (rep>1.e-19)?1./rep:1.e19;
|
---|
[4027] | 393 | hmcnt_p_->Add(wk);
|
---|
| 394 | if ((s2cut_>1.e-9)&&(amp2>s2cut_)) continue;
|
---|
| 395 | hmcntok_p_->Add(wk);
|
---|
| 396 | hp_pk_p_->Add(wk, amp2);
|
---|
[4028] | 397 | nmodeok++; nmodeok_kz++;
|
---|
[3947] | 398 | }
|
---|
| 399 | }
|
---|
[4028] | 400 | if (prtlev_>2)
|
---|
| 401 | cout << " Four3DPk::ComputeNoisePk(kz="<<kz<<") ModeOK_Kz=" << nmodeok_kz
|
---|
| 402 | << " FracOK=" << 100.*(double)nmodeok_kz/(double)fourAmp.SizeX()/(double)fourAmp.SizeY() << endl;
|
---|
| 403 | }
|
---|
[3947] | 404 | if ((prtlev_>1)||((prtlev_>0)&&(s2cut>1.e-9))) {
|
---|
[4028] | 405 | cout << " Four3DPk::ComputeNoisePk/Info : angscale=" << angscale
|
---|
| 406 | << " kmin,kmax=" << kmin << "," << kmax << " kmax2d_ang=" << kmax2d*angscale
|
---|
| 407 | << " \n ... NModeOK=" << nmodeok << " / NMode=" << fourAmp.Size()
|
---|
[3947] | 408 | << " -> " << 100.*(double)nmodeok/(double)fourAmp.Size() << "%" << endl;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
[4027] | 411 | return *hp_pk_p_;
|
---|
| 412 | }
|
---|
[3947] | 413 |
|
---|
[4027] | 414 | // Fills a data table from the computed P(k) profile histogram and mode count
|
---|
| 415 | Histo Four3DPk::FillPkDataTable(DataTable& dt)
|
---|
| 416 | {
|
---|
| 417 | if (hp_pk_p_==NULL) throw ParmError("Four3DPk::FillPkDataTable P(k) NOT computed");
|
---|
| 418 | if ((hmcnt_p_==NULL)||(hmcntok_p_==NULL)) throw ParmError("Four3DPk::FillPkDataTable Mode count histos NOT filled");
|
---|
| 419 | HProf& hp=(*hp_pk_p_);
|
---|
| 420 | Histo& hmcnt=(*hmcnt_p_);
|
---|
| 421 | Histo& hmcntok=(*hmcntok_p_);
|
---|
| 422 | Histo fracmodok=hmcntok/hmcnt;
|
---|
[3947] | 423 | char* nomcol[5] = {"k","pnoise","nmode","nmodok","fracmodok"};
|
---|
| 424 | dt.Clear();
|
---|
| 425 | dt.AddDoubleColumn(nomcol[0]);
|
---|
| 426 | dt.AddDoubleColumn(nomcol[1]);
|
---|
| 427 | dt.AddIntegerColumn(nomcol[2]);
|
---|
| 428 | dt.AddIntegerColumn(nomcol[3]);
|
---|
| 429 | dt.AddFloatColumn(nomcol[4]);
|
---|
| 430 | DataTableRow dtr = dt.EmptyRow();
|
---|
| 431 | for(int_4 ib=0; ib<hp.NBins(); ib++) {
|
---|
| 432 | dtr[0]=hp.BinCenter(ib);
|
---|
| 433 | dtr[1]=hp(ib);
|
---|
| 434 | dtr[2]=hmcnt(ib);
|
---|
| 435 | dtr[3]=hmcntok(ib);
|
---|
| 436 | dtr[4]=fracmodok(ib);
|
---|
| 437 | dt.AddRow(dtr);
|
---|
| 438 | }
|
---|
[4027] | 439 | return fracmodok;
|
---|
[3947] | 440 | }
|
---|
| 441 |
|
---|
[3930] | 442 | //-----------------------------------------------------
|
---|
| 443 | // -- MassDist2D class : 2D mass distribution
|
---|
| 444 | // --- PkNoiseCalculator : Classe de calcul du spectre de bruit PNoise(k)
|
---|
| 445 | // determine par une reponse 2D de l'instrument
|
---|
| 446 | //-----------------------------------------------------
|
---|
| 447 | PkNoiseCalculator::PkNoiseCalculator(Four3DPk& pk3, Four2DResponse& rep, double s2cut, int ngen,
|
---|
| 448 | const char* tit)
|
---|
[4030] | 449 | : pkn3d(pk3), frep(rep), S2CUT(s2cut), NGEN(ngen), title(tit), angscales_(pk3.SizeZ()), pnoisefac_(pk3.SizeZ())
|
---|
[3930] | 450 | {
|
---|
[4026] | 451 | SetFreqRange();
|
---|
| 452 | SetAngScaleConversion();
|
---|
[4030] | 453 | SetPNoiseFactor();
|
---|
[3930] | 454 | SetPrtLevel();
|
---|
| 455 | }
|
---|
[3756] | 456 |
|
---|
[4027] | 457 | HProf PkNoiseCalculator::Compute(int nbin, double kmin, double kmax)
|
---|
[3930] | 458 | {
|
---|
| 459 | Timer tm(title.c_str());
|
---|
| 460 | tm.Nop();
|
---|
| 461 | HProf hnd;
|
---|
[4026] | 462 | cout << "PkNoiseCalculator::Compute() " << title << " NGEN=" << NGEN << " S2CUT=" << S2CUT
|
---|
[4027] | 463 | << " Freq0=" << freq0_ << " dFreq=" << dfreq_ << " angscales=" << angscales_(0)
|
---|
| 464 | << " ... " << angscales_(angscales_.Size()-1) << endl;
|
---|
[3930] | 465 | for(int igen=0; igen<NGEN; igen++) {
|
---|
[4030] | 466 | pkn3d.ComputeNoiseFourierAmp(frep, freq0_, dfreq_, angscales_, pnoisefac_);
|
---|
[4027] | 467 | if (igen==0) hnd = pkn3d.ComputePk(S2CUT,nbin,kmin,kmax,true);
|
---|
| 468 | else pkn3d.ComputePkCumul();
|
---|
[3931] | 469 | if ((prtlev_>0)&&(igen>0)&&(((igen-1)%prtmodulo_)==0))
|
---|
[3930] | 470 | cout << " PkNoiseCalculator::Compute() - done igen=" << igen << " / MaxNGen=" << NGEN << endl;
|
---|
| 471 | }
|
---|
[4027] | 472 | return pkn3d.GetPk() ;
|
---|
[3930] | 473 | }
|
---|
[3756] | 474 |
|
---|
[3930] | 475 |
|
---|
[3756] | 476 | //-----------------------------------------------------
|
---|
| 477 | // -- MassDist2D class : 2D mass distribution
|
---|
| 478 | //-----------------------------------------------------
|
---|
| 479 | // Constructor
|
---|
| 480 | MassDist2D::MassDist2D(GenericFunc& pk, int size, double meandens)
|
---|
| 481 | : pkSpec(pk) , sizeA((size>16)?size:16) , massDens(sizeA, sizeA),
|
---|
| 482 | meanRho(meandens) , fg_fourAmp(false) , fg_massDens(false)
|
---|
| 483 | {
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | // To the computation job
|
---|
| 487 | void MassDist2D::Compute()
|
---|
| 488 | {
|
---|
| 489 | ComputeFourierAmp();
|
---|
| 490 | ComputeMassDens();
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | // Generate mass field Fourier Coefficient
|
---|
| 494 | void MassDist2D::ComputeFourierAmp()
|
---|
| 495 | {
|
---|
| 496 | if (fg_fourAmp) return; // job already done
|
---|
| 497 | // We generate a random gaussian real field
|
---|
| 498 | double sigma = 1.;
|
---|
| 499 | // The following line fills the array by gaussian random numbers
|
---|
| 500 | //--Replaced-- massDens = RandomSequence(RandomSequence::Gaussian, 0., sigma);
|
---|
| 501 | // Can be replaced by
|
---|
| 502 | DR48RandGen rg;
|
---|
| 503 | for(sa_size_t ir=0; ir<massDens.NRows(); ir++) {
|
---|
| 504 | for(sa_size_t jc=0; jc<massDens.NCols(); jc++) {
|
---|
| 505 | massDens(ir, jc) = rg.Gaussian(sigma);
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 | // --- End of random filling
|
---|
| 509 |
|
---|
| 510 | // Compute fourier transform of the random gaussian field -> white noise
|
---|
| 511 | FFTWServer ffts(true);
|
---|
| 512 | ffts.setNormalize(true);
|
---|
| 513 | ffts.FFTForward(massDens, fourAmp);
|
---|
| 514 |
|
---|
| 515 | // fourAmp represent 2-D fourier transform of a real input array.
|
---|
| 516 | // The second half of the array along Y (matrix rows) contain
|
---|
| 517 | // negative frequencies
|
---|
| 518 | // double fnorm = 1./sqrt(2.*fourAmp.Size());
|
---|
| 519 | // PUT smaller value for fnorm and check number of zeros
|
---|
| 520 | double fnorm = 1.;
|
---|
| 521 | // sa_size_t is large integer type
|
---|
| 522 | for(sa_size_t ky=0; ky<fourAmp.NRows(); ky++) {
|
---|
| 523 | double kyy = ky;
|
---|
| 524 | if (ky > fourAmp.NRows()/2) kyy = fourAmp.NRows()-ky; // negative frequencies
|
---|
| 525 | for(sa_size_t kx=0; kx<fourAmp.NCols(); kx++) {
|
---|
| 526 | double wk = sqrt((double)(kx*kx+kyy*kyy));
|
---|
| 527 | double amp = pkSpec(wk)*fnorm;
|
---|
| 528 | fourAmp(ky, kx) *= amp; // renormalize fourier coeff using
|
---|
| 529 | }
|
---|
| 530 | }
|
---|
| 531 | fg_fourAmp = true;
|
---|
| 532 | cout << " MassDist2D::ComputeFourierAmp() done ..." << endl;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | // Compute mass field from its Fourier Coefficient
|
---|
| 536 | void MassDist2D::ComputeMassDens()
|
---|
| 537 | {
|
---|
| 538 | if (fg_massDens) return; // job already done
|
---|
| 539 | if (!fg_fourAmp) ComputeFourierAmp(); // Check fourier amp generation
|
---|
| 540 |
|
---|
| 541 | // Backward fourier transform of the fourierAmp array
|
---|
| 542 | FFTWServer ffts(true);
|
---|
| 543 | ffts.setNormalize(true);
|
---|
| 544 | ffts.FFTBackward(fourAmp, massDens, true);
|
---|
| 545 | // We consider that massDens represents delta rho/rho
|
---|
| 546 | // rho = (delta rho/rho + 1) * MeanDensity
|
---|
| 547 | massDens += 1.;
|
---|
| 548 | // We remove negative values
|
---|
| 549 | sa_size_t npbz = 0;
|
---|
| 550 | for (sa_size_t i=0; i<massDens.NRows(); i++)
|
---|
| 551 | for (sa_size_t j=0; j<massDens.NCols(); j++)
|
---|
| 552 | if (massDens(i,j) < 0.) { npbz++; massDens(i,j) = 0.; }
|
---|
| 553 | massDens *= meanRho;
|
---|
| 554 | cout << " MassDist2D::ComputeMassDens() done NbNeg=" << npbz << " / NPix=" << massDens.Size() << endl;
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | // Compute power spectrum as a function of wave number k
|
---|
| 558 | // Output : power spectrum (profile histogram)
|
---|
| 559 | HProf MassDist2D::ReconstructPk(int nbin)
|
---|
| 560 | {
|
---|
| 561 | // The second half of the array along Y (matrix rows) contain
|
---|
| 562 | // negative frequencies
|
---|
| 563 | int nbh = sqrt(2.0)*fourAmp.NCols();
|
---|
| 564 | // The profile histogram will contain the mean value of FFT amplitude
|
---|
| 565 | // as a function of wave-number k = sqrt((double)(kx*kx+ky*ky))
|
---|
| 566 | if (nbin < 1) nbin = nbh/2;
|
---|
| 567 | HProf hp(-0.5, nbh-0.5, nbin);
|
---|
| 568 | hp.SetErrOpt(false);
|
---|
| 569 |
|
---|
| 570 | for(int ky=0; ky<fourAmp.NRows(); ky++) {
|
---|
| 571 | double kyy = ky;
|
---|
| 572 | if (ky > fourAmp.NRows()/2) kyy = fourAmp.NRows()-ky; // negative frequencies
|
---|
| 573 | for(int kx=0; kx<fourAmp.NCols(); kx++) {
|
---|
| 574 | double wk = sqrt((double)(kx*kx+kyy*kyy));
|
---|
| 575 | complex<r_8> za = fourAmp(ky, kx);
|
---|
| 576 | double amp = sqrt(za.real()*za.real()+za.imag()*za.imag());
|
---|
| 577 | hp.Add(wk, amp);
|
---|
| 578 | }
|
---|
| 579 | }
|
---|
| 580 | return hp;
|
---|
| 581 | }
|
---|
| 582 |
|
---|