[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 *********/
|
---|
| 18 | /*! Createur. f is a probability density function (PDF).
|
---|
| 19 | Le tirage aleatoire est fait sur un histogramme
|
---|
| 20 | Histo(xMin,xMax,nBin) (voir convention dans Histo).
|
---|
| 21 | Chaque bin de l'histogramme contient la valeur de la PDF
|
---|
| 22 | au centre du bin.
|
---|
| 23 | Les valeurs retournees sont les valeurs du centre des bins.
|
---|
| 24 | Si binw est la largeur du bin, les valeurs retournees
|
---|
| 25 | vont de xmin+binw/2 a xmax-binw/2.
|
---|
| 26 | */
|
---|
[1092] | 27 | FunRan::FunRan(FunRan::Func f, r_8 xMin, r_8 xMax, int_4 nBin)
|
---|
[2840] | 28 | : Histo(xMin,xMax,nBin)
|
---|
[220] | 29 | {
|
---|
[2840] | 30 | if(nBin<0)
|
---|
| 31 | throw RangeCheckError("FunRan::FunRan less than 2 bins requested");
|
---|
| 32 |
|
---|
| 33 | // On cree la fonction de distribution a partir de la PDF
|
---|
| 34 | (*this)(0) = f(BinCenter(0));
|
---|
| 35 | for(int_4 i=1;i<nBin;i++) (*this)(i) = (*this)(i-1) + f(BinCenter(i));
|
---|
| 36 |
|
---|
| 37 | if((*this)(nBin-1)<=0.)
|
---|
| 38 | throw RangeCheckError("FunRan::FunRan not a distribution function last bin is <=0");
|
---|
| 39 |
|
---|
| 40 | for(int_4 i=0;i<nBin;i++) (*this)(i) /= (*this)(nBin-1);
|
---|
[220] | 41 | }
|
---|
| 42 |
|
---|
[3075] | 43 | /********* Methode *********/
|
---|
| 44 | /*! Createur. tab is a probability density function.
|
---|
| 45 | The return random values will be between 0 and nBin-1.
|
---|
| 46 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 47 | */
|
---|
[1092] | 48 | FunRan::FunRan(r_8 *tab, int_4 nBin)
|
---|
[2840] | 49 | : Histo(-0.5,nBin-0.5,nBin)
|
---|
[220] | 50 | {
|
---|
[2840] | 51 | if(nBin<=0)
|
---|
| 52 | throw RangeCheckError("FunRan::FunRan no bin in Histo");
|
---|
[220] | 53 |
|
---|
[2840] | 54 | (*this)(0) = tab[0];
|
---|
| 55 | for(int_4 i=1;i<nBin;i++) (*this)(i) = (*this)(i-1) + tab[i];
|
---|
[220] | 56 |
|
---|
[2840] | 57 | if((*this)(nBin-1)<=0.)
|
---|
| 58 | throw RangeCheckError("FunRan::FunRan not a distribution function last bin is <=0");
|
---|
| 59 |
|
---|
| 60 | for(int_4 i=0;i<nBin;i++) (*this)(i) /= (*this)(nBin-1);
|
---|
[220] | 61 | }
|
---|
| 62 |
|
---|
[3075] | 63 | /********* Methode *********/
|
---|
| 64 | /*! Createur. tab is a probability density function
|
---|
| 65 | The content of tab is identified has the content of
|
---|
| 66 | an Histogram define by Histo(xMin,xMax,nBin).
|
---|
| 67 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 68 | */
|
---|
[1092] | 69 | FunRan::FunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax)
|
---|
[2840] | 70 | : Histo(xMin,xMax,nBin)
|
---|
[220] | 71 | {
|
---|
[2840] | 72 | if(nBin<=0)
|
---|
| 73 | throw RangeCheckError("FunRan::FunRan no bin in Histo");
|
---|
[220] | 74 |
|
---|
[2840] | 75 | (*this)(0) = tab[0];
|
---|
| 76 | for(int_4 i=1;i<nBin;i++) (*this)(i) = (*this)(i-1) + tab[i];
|
---|
[220] | 77 |
|
---|
[2840] | 78 | if((*this)(nBin-1)<=0.)
|
---|
| 79 | throw RangeCheckError("FunRan::FunRan not a distribution function last bin is <=0");
|
---|
| 80 |
|
---|
| 81 | for(int_4 i=0;i<nBin;i++) (*this)(i) /= (*this)(nBin-1);
|
---|
[220] | 82 | }
|
---|
| 83 |
|
---|
[3075] | 84 | /********* Methode *********/
|
---|
| 85 | /*! Createur.
|
---|
| 86 | If pdf=true, h is a probability density fonction.
|
---|
| 87 | If pdf=false, h is a distribution function (not necessarly normalized to 1).
|
---|
| 88 | See FunRan::FunRan(FunRan::Func...) for further comments.
|
---|
| 89 | */
|
---|
[2840] | 90 | FunRan::FunRan(Histo &h, bool pdf)
|
---|
[2838] | 91 | : Histo(h)
|
---|
| 92 | {
|
---|
[2840] | 93 | if(mBins<=0)
|
---|
| 94 | throw RangeCheckError("FunRan::FunRan(Histo) no bin in Histo");
|
---|
| 95 |
|
---|
| 96 | // On cree l'histo integre
|
---|
| 97 | if(pdf)
|
---|
| 98 | for(int_4 i=1;i<mBins;i++) (*this)(i) += (*this)(i-1);
|
---|
| 99 |
|
---|
| 100 | if((*this)(mBins-1)<=0.)
|
---|
| 101 | throw RangeCheckError("FunRan::FunRan(Histo) not a distribution function last bin is <=0");
|
---|
| 102 |
|
---|
| 103 | for(int_4 i=0;i<mBins;i++) (*this)(i) /= (*this)(mBins-1);
|
---|
[2838] | 104 | }
|
---|
| 105 |
|
---|
[3075] | 106 | /********* Methode *********/
|
---|
| 107 | /*! Tirage avec retour du numero de bin entre 0 et mBins-1.
|
---|
| 108 | It returns the first bin whose content is greater or equal
|
---|
| 109 | to the random uniform number (in [0,1])
|
---|
| 110 | */
|
---|
[1092] | 111 | int_4 FunRan::BinRandom()
|
---|
[220] | 112 | {
|
---|
[2840] | 113 | // recherche du premier bin plus grand ou egal a z
|
---|
| 114 | r_8 z=drand01();
|
---|
| 115 | for(int_4 i=0;i<mBins;i++) if(z<(*this)(i)) return i;
|
---|
| 116 | return mBins-1;
|
---|
[220] | 117 | }
|
---|
| 118 |
|
---|
[3075] | 119 | /********* Methode *********/
|
---|
| 120 | /*! Tirage avec retour abscisse du bin non interpole. */
|
---|
[1092] | 121 | r_8 FunRan::Random()
|
---|
[220] | 122 | {
|
---|
[2840] | 123 | r_8 z=drand01();
|
---|
| 124 | int ibin = mBins-1;
|
---|
| 125 | for(int_4 i=0;i<mBins;i++)
|
---|
| 126 | if(z<(*this)(i)) {ibin=i; break;}
|
---|
[220] | 127 |
|
---|
[2840] | 128 | return BinCenter(ibin);
|
---|
[220] | 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | ////////////////////////////////////////////////////////////////////////////
|
---|
[3075] | 134 | /*!
|
---|
| 135 | \class SOPHYA::FunRan2D
|
---|
| 136 | \ingroup NTools
|
---|
| 137 | Classe for generating random variables from 2D function
|
---|
| 138 | */
|
---|
[220] | 139 |
|
---|
[3075] | 140 | /********* Methode *********/
|
---|
| 141 | /*! Creator for random from a table */
|
---|
[1092] | 142 | FunRan2D::FunRan2D(r_8 *tab, int_4 nBinX, int_4 nBinY)
|
---|
[220] | 143 | {
|
---|
| 144 | // Tirage en X, somme sur les Y.
|
---|
[1092] | 145 | r_8* tabX = new r_8[nBinX];
|
---|
| 146 | for (int_4 i=0; i<nBinX; i++) {
|
---|
[220] | 147 | tabX[i] = 0;
|
---|
[1092] | 148 | for (int_4 j=0; j<nBinY; j++) {
|
---|
[220] | 149 | tabX[i] += tab[i*nBinY +j];
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | ranX = new FunRan(tabX, nBinX);
|
---|
| 153 | delete[] tabX;
|
---|
| 154 |
|
---|
[2870] | 155 | ranY = new FunRan* [nBinX];
|
---|
[220] | 156 |
|
---|
[1092] | 157 | for (int_4 k=0; k<nBinX; k++)
|
---|
[220] | 158 | ranY[k] = new FunRan(tab + nBinY*k, nBinY);
|
---|
| 159 |
|
---|
| 160 | nx = nBinX;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[3075] | 163 | /********* Methode *********/
|
---|
| 164 | /*! Creator for random from a table */
|
---|
[1092] | 165 | FunRan2D::FunRan2D(r_8 **tab, int_4 nBinX, int_4 nBinY)
|
---|
[220] | 166 | {
|
---|
| 167 | // Tirage en X, somme sur les Y.
|
---|
[1092] | 168 | r_8* tabX = new r_8[nBinX];
|
---|
| 169 | for (int_4 i=0; i<nBinX; i++) {
|
---|
[220] | 170 | tabX[i] = 0;
|
---|
[1092] | 171 | for (int_4 j=0; j<nBinY; j++) {
|
---|
[220] | 172 | tabX[i] += tab[i][j];
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | ranX = new FunRan(tabX, nBinX);
|
---|
| 176 |
|
---|
[2870] | 177 | ranY = new FunRan* [nBinX];
|
---|
[220] | 178 |
|
---|
[1092] | 179 | for (int_4 k=0; k<nBinX; k++)
|
---|
[220] | 180 | if (tabX[k] != 0)
|
---|
| 181 | ranY[k] = new FunRan(tab[k], nBinY);
|
---|
| 182 | else ranY[k] = NULL;
|
---|
| 183 |
|
---|
| 184 | delete[] tabX;
|
---|
| 185 | nx = nBinX;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[3075] | 188 | /********* Methode *********/
|
---|
| 189 | /*! Destructor */
|
---|
[220] | 190 | FunRan2D::~FunRan2D()
|
---|
| 191 | {
|
---|
[1092] | 192 | for (int_4 i=nx-1; i>=0; i--)
|
---|
[220] | 193 | delete ranY[i];
|
---|
| 194 |
|
---|
| 195 | delete[] ranY;
|
---|
| 196 |
|
---|
| 197 | delete ranX;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[3075] | 200 | /********* Methode *********/
|
---|
| 201 | /*! Tirage avec retour du numeros de bin. */
|
---|
[1092] | 202 | void FunRan2D::BinRandom(int_4& x, int_4& y)
|
---|
[220] | 203 | {
|
---|
| 204 | x = ranX->BinRandom();
|
---|
| 205 | y = ranY[x]->BinRandom();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[3075] | 208 | /********* Methode *********/
|
---|
| 209 | /*! Tirage avec retour abscisse et ordonnee du bin interpole. */
|
---|
[1092] | 210 | void FunRan2D::Random(r_8& x, r_8& y)
|
---|
[220] | 211 | {
|
---|
| 212 | x = ranX->Random();
|
---|
[1092] | 213 | int_4 i = int_4(ceil(x));
|
---|
[220] | 214 | y = ranY[i]->Random();
|
---|
| 215 | }
|
---|
[3075] | 216 |
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | /////////////////////////////////////////////////////////////////
|
---|
| 220 | /*
|
---|
| 221 | --- Remarque sur complex< r_8 > ComplexGaussRan(double sig)
|
---|
| 222 | x = r cos(t) tire gaussien: pdf f(x) = 1/(sqrt(2Pi) Sx) exp(-(x-Mx)^2/(2 Sx^2))
|
---|
| 223 | y = r sin(t) tire gaussien: pdf f(y) = 1/(sqrt(2Pi) Sy) exp(-(y-My)^2/(2 Sy^2))
|
---|
| 224 | x,y independants --> pdf f(x,y) = f(x) f(y)
|
---|
| 225 | - On cherche la pdf g(r,t) du module et de la phase
|
---|
| 226 | (r=sqrt(x^2+y^2,t=atan2(y,x)) --> (x,y): le Jacobien = r
|
---|
| 227 | g(r,t) = r f(x,y) = r f(x) f(y)
|
---|
| 228 | = r/(2Pi Sx Sy) exp(-(x-Mx)^2/(2 Sx^2)) exp(-(y-My)^2/(2 Sy^2))
|
---|
| 229 | - Le cas general est complique
|
---|
| 230 | (cf D.Pelat cours DEA "bruits et signaux" section 4.5)
|
---|
| 231 | - Cas ou "Mx = My = 0" et "Sx = Sy = S"
|
---|
| 232 | g(r,t) = r/(2Pi S^2) exp(-r^2/(2 S^2))
|
---|
| 233 | La distribution de "r" est donc:
|
---|
| 234 | g(r) = Integrate[g(r,t),{t,0,2Pi}]
|
---|
| 235 | = r/S^2 exp(-r^2/(2 S^2))
|
---|
| 236 | La distribution de "t" est donc:
|
---|
| 237 | g(t) = Integrate[g(r,t),{r,0,Infinity}]
|
---|
| 238 | = 1 / 2Pi (distribution uniforme sur [0,2Pi[)
|
---|
| 239 | Les variables aleatoires r,t sont independantes:
|
---|
| 240 | g(r,t) = g(r) g(t)
|
---|
[3097] | 241 | - Attention:
|
---|
| 242 | La variable complexe "c=x+iy" ainsi definie verifie:
|
---|
| 243 | <|c|^2> = <c c*> = <x^2+y^2> = <r^2> = 2 sig^2
|
---|
| 244 | Si on veut generer une variable complexe gaussienne telle que
|
---|
| 245 | <c c*> = s^2 alors il faut sig = s/sqrt(2) comme argument
|
---|
[3075] | 246 | */
|
---|