[301] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // get_random_spectrum.cc: Functions to obtain random values. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[3] | 28 | #include <iostream> |
---|
| 29 | #include <math.h> |
---|
| 30 | |
---|
| 31 | float getNormalRV() |
---|
| 32 | { |
---|
| 33 | float v1,v2,s; |
---|
[146] | 34 | // simulate a standard normal RV via polar method |
---|
[3] | 35 | do{ |
---|
[518] | 36 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
| 37 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
[3] | 38 | s = v1*v1+v2*v2; |
---|
| 39 | }while(s>1); |
---|
| 40 | return sqrt(-2.*log(s)/s)*v1; |
---|
| 41 | |
---|
| 42 | } |
---|
| 43 | |
---|
[70] | 44 | float getNormalRVtrunc() |
---|
| 45 | { |
---|
| 46 | float v1,v2,s; |
---|
[146] | 47 | // simulate a standard normal RV via polar method |
---|
[70] | 48 | do{ |
---|
[518] | 49 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
| 50 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
[70] | 51 | s = v1*v1+v2*v2; |
---|
[125] | 52 | }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2))); |
---|
[70] | 53 | return sqrt(-2.*log(s)/s)*v1; |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
[3] | 57 | float getNormalRV(float mean, float sigma) |
---|
| 58 | { |
---|
[146] | 59 | // simulate a standard normal RV via polar method |
---|
[3] | 60 | float v1,v2,s; |
---|
| 61 | do{ |
---|
[518] | 62 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
| 63 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
[3] | 64 | s = v1*v1+v2*v2; |
---|
| 65 | }while(s>1); |
---|
| 66 | float z=sqrt(-2.*log(s)/s)*v1; |
---|
| 67 | return z*sigma + mean; |
---|
| 68 | } |
---|
| 69 | |
---|
[201] | 70 | void getRandomSpectrum(int length, float *x, float *y) |
---|
| 71 | { |
---|
| 72 | srandom(time(0)); |
---|
| 73 | rand(); |
---|
| 74 | for(int i=0;i<length;i++){ |
---|
| 75 | x[i] = (float)i; |
---|
| 76 | // simulate a standard normal RV via polar method |
---|
| 77 | double v1,v2,s; |
---|
| 78 | do{ |
---|
[518] | 79 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
| 80 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
[201] | 81 | s = v1*v1+v2*v2; |
---|
| 82 | }while(s>1); |
---|
| 83 | y[i] = sqrt(-2.*log(s)/s)*v1; |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[3] | 87 | void getRandomSpectrum(int length, float *x, double *y) |
---|
| 88 | { |
---|
[518] | 89 | srandomdev(); |
---|
[3] | 90 | rand(); |
---|
| 91 | for(int i=0;i<length;i++){ |
---|
| 92 | x[i] = (float)i; |
---|
| 93 | // simulate a standard normal RV via polar method |
---|
| 94 | double v1,v2,s; |
---|
| 95 | do{ |
---|
[518] | 96 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
| 97 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
[3] | 98 | s = v1*v1+v2*v2; |
---|
| 99 | }while(s>1); |
---|
| 100 | y[i] = sqrt(-2.*log(s)/s)*v1; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
[146] | 105 | void getRandomSpectrum(int length, float mean, float sigma, |
---|
| 106 | float *x, double *y) |
---|
[3] | 107 | { |
---|
[518] | 108 | srandomdev(); |
---|
[3] | 109 | rand(); |
---|
| 110 | for(int i=0;i<length;i++){ |
---|
| 111 | x[i] = (float)i; |
---|
| 112 | // simulate a standard normal RV via polar method |
---|
| 113 | double v1,v2,s; |
---|
| 114 | do{ |
---|
[518] | 115 | v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
| 116 | v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.; |
---|
[3] | 117 | s = v1*v1+v2*v2; |
---|
| 118 | }while(s>1); |
---|
| 119 | float z = sqrt(-2.*log(s)/s)*v1; |
---|
| 120 | y[i] = z * sigma + mean; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|