source: tags/release-0.9/Utils/get_random_spectrum.cc

Last change on this file was 3, checked in by Matthew Whiting, 18 years ago

This is the first full import of all working code to
the Duchamp repository.
Made three directories at top level:

branches/ tags/ trunk/

and trunk/ has the full set of code:
ATrous/ Cubes/ Detection/ InputComplete? InputExample? README Utils/ docs/ mainDuchamp.cc param.cc param.hh

File size: 2.3 KB
Line 
1#include <iostream>
2#include <stdlib.h>
3#include <time.h>
4#include <math.h>
5
6float getNormalRV()
7{
8  float v1,v2,s;
9  do{
10    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
11    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
12    s = v1*v1+v2*v2;
13  }while(s>1);
14  return sqrt(-2.*log(s)/s)*v1;
15
16}
17
18float getNormalRV(float mean, float sigma)
19{
20  float v1,v2,s;
21  do{
22    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
23    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
24    s = v1*v1+v2*v2;
25  }while(s>1);
26  float z=sqrt(-2.*log(s)/s)*v1;
27  return z*sigma + mean;
28}
29
30void getRandomSpectrum(int length, float *x, float *y)
31{
32  srandom(time(0));
33  rand();
34
35  for(int i=0;i<length;i++){
36    x[i] = i;
37    // simulate a standard normal RV via polar method
38    float v1,v2,s;
39    do{
40      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
41      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
42      s = v1*v1+v2*v2;
43    }while(s>1);
44    y[i] = sqrt(-2.*log(s)/s)*v1;
45  }
46}
47
48void getRandomSpectrum(int length, float *x, double *y)
49{
50  srandom(time(0));
51  rand();
52  for(int i=0;i<length;i++){
53    x[i] = (float)i;
54    // simulate a standard normal RV via polar method
55    double v1,v2,s;
56    do{
57      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
58      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
59      s = v1*v1+v2*v2;
60    }while(s>1);
61    y[i] = sqrt(-2.*log(s)/s)*v1;
62//     cerr<< x[i]<<" " << y[i]<<endl;
63  }
64}
65
66
67void getRandomSpectrum(int length, float mean, float sigma, float *x, double *y)
68{
69  srandom(time(0));
70  rand();
71  for(int i=0;i<length;i++){
72    x[i] = (float)i;
73    // simulate a standard normal RV via polar method
74    double v1,v2,s;
75    do{
76      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
77      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
78      s = v1*v1+v2*v2;
79    }while(s>1);
80    float z = sqrt(-2.*log(s)/s)*v1;
81    y[i] = z * sigma + mean;
82//     cerr<< x[i]<<" " << y[i]<<endl;
83  }
84}
85
86void getRandomSpectrum(int length, float mean, float sigma, float *x, float *y)
87{
88  srandom(time(0));
89  rand();
90  for(int i=0;i<length;i++){
91    x[i] = (float)i;
92    // simulate a standard normal RV via polar method
93    float v1,v2,s;
94    do{
95      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
96      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
97      s = v1*v1+v2*v2;
98    }while(s>1);
99    float z = sqrt(-2.*log(s)/s)*v1;
100    y[i] = z * sigma + mean;
101//     cerr<< x[i]<<" " << y[i]<<endl;
102  }
103}
104
Note: See TracBrowser for help on using the repository browser.