[2615] | 1 | #include "sopnamsp.h"
|
---|
[244] | 2 | #include "machdefs.h"
|
---|
| 3 | #include "pexceptions.h"
|
---|
[220] | 4 | #include "perandom.h"
|
---|
| 5 | #include "pemath.h"
|
---|
[2322] | 6 | #include <iostream>
|
---|
[220] | 7 |
|
---|
[3075] | 8 |
|
---|
[220] | 9 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3075] | 10 | /*!
|
---|
| 11 | \class SOPHYA::FunRan
|
---|
| 12 | \ingroup NTools
|
---|
| 13 | Classe for generating random variables from 1D function
|
---|
| 14 | */
|
---|
[220] | 15 |
|
---|
[3075] | 16 |
|
---|
| 17 | /********* Methode *********/
|
---|
[3109] | 18 | /*! Creator from a function.
|
---|
| 19 | \verbatim
|
---|
| 20 | - if pdf==true: f est une densite de probabilite (PDF)
|
---|
| 21 | non necessairement normalisee
|
---|
| 22 | if pdf==false: f est une fonction de distribution (DF).
|
---|
| 23 | non necessairement normalisee
|
---|
| 24 | - Le tirage aleatoire est fait sur un histogramme
|
---|
| 25 | Histo(xMin,xMax,nBin) (voir convention dans Histo).
|
---|
| 26 | - Chaque bin de l'histogramme contient la valeur de la PDF
|
---|
| 27 | (ou de la DF) au centre du bin: h(i)=f(BinCenter(i))
|
---|
| 28 | - Les valeurs retournees sont les valeurs du centre des bins
|
---|
| 29 | pour le tirage non interpole et toutes les valeurs
|
---|
| 30 | entre [xmin,xmax] pour le tirage interpole
|
---|
| 31 | - La pdf doit etre interpretee comme etant nulle
|
---|
| 32 | pour des x<=xmin et x>=xmax
|
---|
| 33 | - Dans le bin "i" entre [x1,x2[ et de centre x0, h(i)=pdf(x0).
|
---|
| 34 | Pour le tirage interpole, la DF est approximee par un segment
|
---|
| 35 | et pdf(x0) est l'exces de proba entre x1 et x2:
|
---|
| 36 | bin 0 entre [xmin,BinHighEdge(0)[ :
|
---|
| 37 | la pdf va de 0 a pdf(BinCenter(0))
|
---|
| 38 | bin 1 entre [BinLowEdge(1),BinHighEdge(1)[:
|
---|
| 39 | la pdf va de pdf(BinCenter(0)) a pdf(BinCenter(1))
|
---|
| 40 | ...
|
---|
| 41 | bin n-1 entre [BinLowEdge(n-1),xmax[:
|
---|
| 42 | la pdf va de pdf(BinCenter(n-2)) a pdf(BinCenter(n-1))
|
---|
| 43 | \endverbatim
|
---|
[3075] | 44 | */
|
---|
[3109] | 45 | FunRan::FunRan(FunRan::Func f, r_8 xMin, r_8 xMax, int_4 nBin, bool pdf)
|
---|
| 46 | : Histo(xMin,xMax,nBin)
|
---|
[220] | 47 | {
|
---|
[3109] | 48 | if(nBin<=1)
|
---|
[2840] | 49 | throw RangeCheckError("FunRan::FunRan less than 2 bins requested");
|
---|
[3109] | 50 | for(int_4 i=0;i<nBin;i++) (*this)(i) = f(BinCenter(i));
|
---|
| 51 | create_DF(pdf);
|
---|
| 52 | }
|
---|
[2840] | 53 |
|
---|
[3109] | 54 | /********* Methode *********/
|
---|
| 55 | /*! Creator from a ClassFunc
|
---|
| 56 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 57 | */
|
---|
| 58 | FunRan::FunRan(ClassFunc& f, r_8 xMin, r_8 xMax, int_4 nBin, bool pdf)
|
---|
| 59 | : Histo(xMin,xMax,nBin)
|
---|
| 60 | {
|
---|
| 61 | if(nBin<=1)
|
---|
| 62 | throw RangeCheckError("FunRan::FunRan less than 2 bins requested");
|
---|
| 63 | for(int_4 i=0;i<nBin;i++) (*this)(i) = f(BinCenter(i));
|
---|
| 64 | create_DF(pdf);
|
---|
[220] | 65 | }
|
---|
| 66 |
|
---|
[3075] | 67 | /********* Methode *********/
|
---|
[3109] | 68 | /*! Creator from an table.
|
---|
| 69 | If pdf=true, tab is a probability density fonction (not necessary normalised).
|
---|
| 70 | If pdf=false, tab is a distribution function (not necessarly normalized to 1).
|
---|
[3075] | 71 | The return random values will be between 0 and nBin-1.
|
---|
| 72 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 73 | */
|
---|
[3109] | 74 | FunRan::FunRan(r_8 *tab, int_4 nBin, bool pdf)
|
---|
[2840] | 75 | : Histo(-0.5,nBin-0.5,nBin)
|
---|
[220] | 76 | {
|
---|
[3109] | 77 | if(nBin<=1)
|
---|
| 78 | throw RangeCheckError("FunRan::FunRan less than 2 bins requested");
|
---|
| 79 | for(int_4 i=0;i<nBin;i++) (*this)(i) = tab[i];
|
---|
| 80 | create_DF(pdf);
|
---|
[220] | 81 | }
|
---|
| 82 |
|
---|
[3075] | 83 | /********* Methode *********/
|
---|
[3109] | 84 | /*! Creator from an table.
|
---|
| 85 | If pdf=true, tab is a probability density fonction (not necessary normalised).
|
---|
| 86 | If pdf=false, tab is a distribution function (not necessarly normalized to 1).
|
---|
[3075] | 87 | The content of tab is identified has the content of
|
---|
| 88 | an Histogram define by Histo(xMin,xMax,nBin).
|
---|
| 89 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 90 | */
|
---|
[3109] | 91 | FunRan::FunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax, bool pdf)
|
---|
[2840] | 92 | : Histo(xMin,xMax,nBin)
|
---|
[220] | 93 | {
|
---|
[3109] | 94 | if(nBin<=1)
|
---|
| 95 | throw RangeCheckError("FunRan::FunRan less than 2 bins requested");
|
---|
| 96 | for(int_4 i=0;i<nBin;i++) (*this)(i) = tab[i];
|
---|
| 97 | create_DF(pdf);
|
---|
[220] | 98 | }
|
---|
| 99 |
|
---|
[3075] | 100 | /********* Methode *********/
|
---|
[3109] | 101 | /*! Creator from an histogram
|
---|
| 102 | If pdf=true, h is a probability density fonction (not necessary normalised).
|
---|
[3075] | 103 | If pdf=false, h is a distribution function (not necessarly normalized to 1).
|
---|
| 104 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 105 | */
|
---|
[2840] | 106 | FunRan::FunRan(Histo &h, bool pdf)
|
---|
[3109] | 107 | : Histo(h)
|
---|
[2838] | 108 | {
|
---|
[3109] | 109 | if(mBins<=1)
|
---|
| 110 | throw RangeCheckError("FunRan::FunRan less than 2 bins requested");
|
---|
| 111 | create_DF(pdf);
|
---|
| 112 | }
|
---|
[2840] | 113 |
|
---|
[3109] | 114 | /********* Methode *********/
|
---|
[3321] | 115 | /*! Creator by copy */
|
---|
| 116 | FunRan::FunRan(const FunRan& fh)
|
---|
| 117 | : Histo(fh)
|
---|
| 118 | {
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /********* Methode *********/
|
---|
| 122 | /*! Creator by default */
|
---|
| 123 | FunRan::FunRan(void)
|
---|
| 124 | {
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /********* Methode *********/
|
---|
[3109] | 128 | void FunRan::create_DF(bool pdf)
|
---|
| 129 | // Creation (si necessaire) et normalisation de la DF
|
---|
| 130 | {
|
---|
| 131 | // On fabrique la FD si necessaire
|
---|
| 132 | if(pdf)
|
---|
| 133 | for(int_4 i=1;i<mBins;i++) (*this)(i) += (*this)(i-1);
|
---|
[2840] | 134 |
|
---|
[3109] | 135 | // On normalise la FD
|
---|
[3195] | 136 | if((*this)(mBins-1)<=0.) {
|
---|
| 137 | cout<<"FunRan::FunRan(Histo) not a distribution function last bin is <=0"<<endl;
|
---|
| 138 | throw RangeCheckError("FunRan::FunRan(Histo) not a distribution function last bin is <=0");
|
---|
| 139 | }
|
---|
[2840] | 140 | for(int_4 i=0;i<mBins;i++) (*this)(i) /= (*this)(mBins-1);
|
---|
[2838] | 141 | }
|
---|
| 142 |
|
---|
[3075] | 143 | /********* Methode *********/
|
---|
| 144 | /*! Tirage avec retour du numero de bin entre 0 et mBins-1.
|
---|
| 145 | It returns the first bin whose content is greater or equal
|
---|
[3109] | 146 | to the random uniform number (in [0,1[)
|
---|
[3075] | 147 | */
|
---|
[1092] | 148 | int_4 FunRan::BinRandom()
|
---|
[220] | 149 | {
|
---|
[2840] | 150 | // recherche du premier bin plus grand ou egal a z
|
---|
| 151 | r_8 z=drand01();
|
---|
| 152 | for(int_4 i=0;i<mBins;i++) if(z<(*this)(i)) return i;
|
---|
| 153 | return mBins-1;
|
---|
[220] | 154 | }
|
---|
| 155 |
|
---|
[3075] | 156 | /********* Methode *********/
|
---|
| 157 | /*! Tirage avec retour abscisse du bin non interpole. */
|
---|
[1092] | 158 | r_8 FunRan::Random()
|
---|
[220] | 159 | {
|
---|
[2840] | 160 | r_8 z=drand01();
|
---|
| 161 | int ibin = mBins-1;
|
---|
[3109] | 162 | for(int_4 i=0;i<mBins;i++) if(z<(*this)(i)) {ibin=i; break;}
|
---|
[2840] | 163 | return BinCenter(ibin);
|
---|
[220] | 164 | }
|
---|
| 165 |
|
---|
[3109] | 166 | /********* Methode *********/
|
---|
| 167 | /*! Tirage avec retour abscisse du bin interpole. */
|
---|
| 168 | r_8 FunRan::RandomInterp(void)
|
---|
| 169 | {
|
---|
| 170 | r_8 z=drand01();
|
---|
| 171 | int ibin = mBins-1;
|
---|
| 172 | r_8 z1=0., z2;
|
---|
| 173 | for(int_4 i=0;i<mBins;i++) {
|
---|
| 174 | z2 = (*this)(i);
|
---|
| 175 | if(z<z2) {ibin=i; break;}
|
---|
| 176 | z1 = z2;
|
---|
| 177 | }
|
---|
[220] | 178 |
|
---|
[3109] | 179 | // l'algorithme garanti que "z2-z1 != 0" et "z1<z2"
|
---|
| 180 | return BinLowEdge(ibin) + (z-z1)/(z2-z1)*binWidth;
|
---|
| 181 |
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[220] | 184 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3075] | 185 | /*!
|
---|
| 186 | \class SOPHYA::FunRan2D
|
---|
| 187 | \ingroup NTools
|
---|
| 188 | Classe for generating random variables from 2D function
|
---|
| 189 | */
|
---|
[220] | 190 |
|
---|
[3075] | 191 | /********* Methode *********/
|
---|
| 192 | /*! Creator for random from a table */
|
---|
[1092] | 193 | FunRan2D::FunRan2D(r_8 *tab, int_4 nBinX, int_4 nBinY)
|
---|
[220] | 194 | {
|
---|
| 195 | // Tirage en X, somme sur les Y.
|
---|
[1092] | 196 | r_8* tabX = new r_8[nBinX];
|
---|
| 197 | for (int_4 i=0; i<nBinX; i++) {
|
---|
[220] | 198 | tabX[i] = 0;
|
---|
[1092] | 199 | for (int_4 j=0; j<nBinY; j++) {
|
---|
[220] | 200 | tabX[i] += tab[i*nBinY +j];
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | ranX = new FunRan(tabX, nBinX);
|
---|
| 204 | delete[] tabX;
|
---|
| 205 |
|
---|
[2870] | 206 | ranY = new FunRan* [nBinX];
|
---|
[220] | 207 |
|
---|
[1092] | 208 | for (int_4 k=0; k<nBinX; k++)
|
---|
[220] | 209 | ranY[k] = new FunRan(tab + nBinY*k, nBinY);
|
---|
| 210 |
|
---|
| 211 | nx = nBinX;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[3075] | 214 | /********* Methode *********/
|
---|
| 215 | /*! Creator for random from a table */
|
---|
[1092] | 216 | FunRan2D::FunRan2D(r_8 **tab, int_4 nBinX, int_4 nBinY)
|
---|
[220] | 217 | {
|
---|
| 218 | // Tirage en X, somme sur les Y.
|
---|
[1092] | 219 | r_8* tabX = new r_8[nBinX];
|
---|
| 220 | for (int_4 i=0; i<nBinX; i++) {
|
---|
[220] | 221 | tabX[i] = 0;
|
---|
[1092] | 222 | for (int_4 j=0; j<nBinY; j++) {
|
---|
[220] | 223 | tabX[i] += tab[i][j];
|
---|
| 224 | }
|
---|
| 225 | }
|
---|
| 226 | ranX = new FunRan(tabX, nBinX);
|
---|
| 227 |
|
---|
[2870] | 228 | ranY = new FunRan* [nBinX];
|
---|
[220] | 229 |
|
---|
[1092] | 230 | for (int_4 k=0; k<nBinX; k++)
|
---|
[220] | 231 | if (tabX[k] != 0)
|
---|
| 232 | ranY[k] = new FunRan(tab[k], nBinY);
|
---|
| 233 | else ranY[k] = NULL;
|
---|
| 234 |
|
---|
| 235 | delete[] tabX;
|
---|
| 236 | nx = nBinX;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[3075] | 239 | /********* Methode *********/
|
---|
| 240 | /*! Destructor */
|
---|
[220] | 241 | FunRan2D::~FunRan2D()
|
---|
| 242 | {
|
---|
[1092] | 243 | for (int_4 i=nx-1; i>=0; i--)
|
---|
[220] | 244 | delete ranY[i];
|
---|
| 245 |
|
---|
| 246 | delete[] ranY;
|
---|
| 247 |
|
---|
| 248 | delete ranX;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[3075] | 251 | /********* Methode *********/
|
---|
| 252 | /*! Tirage avec retour du numeros de bin. */
|
---|
[1092] | 253 | void FunRan2D::BinRandom(int_4& x, int_4& y)
|
---|
[220] | 254 | {
|
---|
| 255 | x = ranX->BinRandom();
|
---|
| 256 | y = ranY[x]->BinRandom();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[3075] | 259 | /********* Methode *********/
|
---|
| 260 | /*! Tirage avec retour abscisse et ordonnee du bin interpole. */
|
---|
[1092] | 261 | void FunRan2D::Random(r_8& x, r_8& y)
|
---|
[220] | 262 | {
|
---|
| 263 | x = ranX->Random();
|
---|
[1092] | 264 | int_4 i = int_4(ceil(x));
|
---|
[220] | 265 | y = ranY[i]->Random();
|
---|
| 266 | }
|
---|
[3075] | 267 |
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 | /////////////////////////////////////////////////////////////////
|
---|
| 271 | /*
|
---|
[3109] | 272 | **** Remarques sur complex< r_8 > ComplexGaussRan(double sig) ****
|
---|
| 273 |
|
---|
| 274 | --- variables gaussiennes x,y independantes
|
---|
| 275 | x gaussien: pdf f(x) = 1/(sqrt(2Pi) Sx) exp(-(x-Mx)^2/(2 Sx^2))
|
---|
| 276 | y gaussien: pdf f(y) = 1/(sqrt(2Pi) Sy) exp(-(y-My)^2/(2 Sy^2))
|
---|
[3075] | 277 | x,y independants --> pdf f(x,y) = f(x) f(y)
|
---|
[3109] | 278 | On a:
|
---|
| 279 | <x> = Integrate[x*f(x)] = Mx
|
---|
| 280 | <x^2> = Integrate[x^2*f(x)] = Mx^2 + Sx^2
|
---|
| 281 |
|
---|
| 282 | --- On cherche la pdf g(r,t) du module et de la phase
|
---|
| 283 | x = r cos(t) , y = r sin(t)
|
---|
| 284 | r=sqrt(x^2+y^2 , t=atan2(y,x)
|
---|
| 285 | (r,t) --> (x,y): le Jacobien = r
|
---|
| 286 |
|
---|
[3075] | 287 | g(r,t) = r f(x,y) = r f(x) f(y)
|
---|
| 288 | = r/(2Pi Sx Sy) exp(-(x-Mx)^2/(2 Sx^2)) exp(-(y-My)^2/(2 Sy^2))
|
---|
[3109] | 289 |
|
---|
[3075] | 290 | - Le cas general est complique
|
---|
| 291 | (cf D.Pelat cours DEA "bruits et signaux" section 4.5)
|
---|
[3109] | 292 |
|
---|
[3075] | 293 | - Cas ou "Mx = My = 0" et "Sx = Sy = S"
|
---|
[3109] | 294 | c'est la pdf du module et de la phase d'un nombre complexe
|
---|
| 295 | dont les parties reelles et imaginaires sont independantes
|
---|
| 296 | et sont distribuees selon des gaussiennes de variance S^2
|
---|
[3075] | 297 | g(r,t) = r/(2Pi S^2) exp(-r^2/(2 S^2))
|
---|
| 298 | La distribution de "r" est donc:
|
---|
| 299 | g(r) = Integrate[g(r,t),{t,0,2Pi}]
|
---|
| 300 | = r/S^2 exp(-r^2/(2 S^2))
|
---|
| 301 | La distribution de "t" est donc:
|
---|
| 302 | g(t) = Integrate[g(r,t),{r,0,Infinity}]
|
---|
| 303 | = 1 / 2Pi (distribution uniforme sur [0,2Pi[)
|
---|
| 304 | Les variables aleatoires r,t sont independantes:
|
---|
[3109] | 305 | g(r,t) = g(r) g(t)
|
---|
| 306 | On a:
|
---|
| 307 | <r> = Integrate[r*g(r)] = sqrt(PI/2)*S
|
---|
| 308 | <r^2> = Integrate[r^2*g(r)] = 2*S^2
|
---|
| 309 | <r^3> = Integrate[r^3*g(r)] = 3*sqrt(PI/2)*S^3
|
---|
| 310 | <r^4> = Integrate[r^4*g(r)] = 8*S^4
|
---|
| 311 |
|
---|
[3097] | 312 | - Attention:
|
---|
[3109] | 313 | La variable complexe "c = x+iy = r*exp(i*t)" ainsi definie verifie:
|
---|
| 314 | <|c|^2> = <c c*> = <x^2+y^2> = <r^2> = 2 S^2
|
---|
[3097] | 315 | Si on veut generer une variable complexe gaussienne telle que
|
---|
[3109] | 316 | <c c*> = s^2 alors il faut prendre S = s/sqrt(2) comme argument
|
---|
| 317 |
|
---|
[3075] | 318 | */
|
---|