source: trunk/src/Utils/get_random_spectrum.cc

Last change on this file was 1347, checked in by MatthewWhiting, 10 years ago

Changing the verification cube so that the FWHM are done properly. Also moving the random spectrum code out of Devel into Utils so that the released code can run createTestImage.

File size: 3.5 KB
Line 
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// -----------------------------------------------------------------------
28#include <iostream>
29#include <math.h>
30#include <stdlib.h>
31
32float getNormalRV()
33{
34  float v1,v2,s;
35  // simulate a standard normal RV via polar method
36  do{
37    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
38    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
39    s = v1*v1+v2*v2;
40  }while(s>1);
41  return sqrt(-2.*log(s)/s)*v1;
42
43}
44
45float getNormalRVtrunc()
46{
47  float v1,v2,s;
48  // simulate a standard normal RV via polar method
49  do{
50    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
51    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
52    s = v1*v1+v2*v2;
53  }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2)));
54  return sqrt(-2.*log(s)/s)*v1;
55
56}
57
58float getNormalRV(float mean, float sigma)
59{
60  // simulate a standard normal RV via polar method
61  float v1,v2,s;
62  do{
63    v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
64    v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
65    s = v1*v1+v2*v2;
66  }while(s>1);
67  float z=sqrt(-2.*log(s)/s)*v1;
68  return z*sigma + mean;
69}
70
71void getRandomSpectrum(int length, float *x, float *y)
72{
73  // srandom(time(0));
74  rand();
75  for(int i=0;i<length;i++){
76    x[i] = (float)i;
77    // simulate a standard normal RV via polar method
78    double v1,v2,s;
79    do{
80      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
81      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
82      s = v1*v1+v2*v2;
83    }while(s>1);
84    y[i] = sqrt(-2.*log(s)/s)*v1;
85  }
86}
87
88void getRandomSpectrum(int length, float *x, double *y)
89{
90//   srandomdev();
91  srandom(time(0));
92  rand();
93  for(int i=0;i<length;i++){
94    x[i] = (float)i;
95    // simulate a standard normal RV via polar method
96    double v1,v2,s;
97    do{
98      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
99      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
100      s = v1*v1+v2*v2;
101    }while(s>1);
102    y[i] = sqrt(-2.*log(s)/s)*v1;
103  }
104}
105
106
107void getRandomSpectrum(int length, float mean, float sigma,
108                       float *x, double *y)
109{
110//   srandomdev();
111  srandom(time(0));
112  rand();
113  for(int i=0;i<length;i++){
114    x[i] = (float)i;
115    // simulate a standard normal RV via polar method
116    double v1,v2,s;
117    do{
118      v1 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
119      v2 = 2.*(1.*random())/(RAND_MAX+1.0) - 1.;
120      s = v1*v1+v2*v2;
121    }while(s>1);
122    float z = sqrt(-2.*log(s)/s)*v1;
123    y[i] = z * sigma + mean;
124  }
125}
126
Note: See TracBrowser for help on using the repository browser.