source: trunk/src/Utils/get_random_spectrum.cc @ 201

Last change on this file since 201 was 201, checked in by Matthew Whiting, 18 years ago
  • New functionality added: ability to smooth a cube (in each individual spectrum) using a hanning filter before searching.
  • This comes with associated input parameters: flagSmooth and hanningWidth.
  • Hanning smoothing implemented via a new class that does the smoothing
  • Other minor changes:
    • changed the way getIAUName is called.
    • new colour names in mycpgplot
    • a << operator for the StatsContainer? class
    • more robust ProgressBar? functions
    • updated the Makefile.in
File size: 2.2 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  // simulate a standard normal RV via polar method
10  do{
11    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
12    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
13    s = v1*v1+v2*v2;
14  }while(s>1);
15  return sqrt(-2.*log(s)/s)*v1;
16
17}
18
19float getNormalRVtrunc()
20{
21  float v1,v2,s;
22  // simulate a standard normal RV via polar method
23  do{
24    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
25    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
26    s = v1*v1+v2*v2;
27  }while((s>1)||(fabs(sqrt(-2.*log(s)/s)*v1)>sqrt(2*M_LN2)));
28  return sqrt(-2.*log(s)/s)*v1;
29
30}
31
32float getNormalRV(float mean, float sigma)
33{
34  // simulate a standard normal RV via polar method
35  float v1,v2,s;
36  do{
37    v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
38    v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
39    s = v1*v1+v2*v2;
40  }while(s>1);
41  float z=sqrt(-2.*log(s)/s)*v1;
42  return z*sigma + mean;
43}
44
45void getRandomSpectrum(int length, float *x, float *y)
46{
47  srandom(time(0));
48  rand();
49  for(int i=0;i<length;i++){
50    x[i] = (float)i;
51    // simulate a standard normal RV via polar method
52    double v1,v2,s;
53    do{
54      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
55      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
56      s = v1*v1+v2*v2;
57    }while(s>1);
58    y[i] = sqrt(-2.*log(s)/s)*v1;
59  }
60}
61
62void getRandomSpectrum(int length, float *x, double *y)
63{
64  srandom(time(0));
65  rand();
66  for(int i=0;i<length;i++){
67    x[i] = (float)i;
68    // simulate a standard normal RV via polar method
69    double v1,v2,s;
70    do{
71      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
72      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
73      s = v1*v1+v2*v2;
74    }while(s>1);
75    y[i] = sqrt(-2.*log(s)/s)*v1;
76  }
77}
78
79
80void getRandomSpectrum(int length, float mean, float sigma,
81                       float *x, double *y)
82{
83  srandom(time(0));
84  rand();
85  for(int i=0;i<length;i++){
86    x[i] = (float)i;
87    // simulate a standard normal RV via polar method
88    double v1,v2,s;
89    do{
90      v1 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
91      v2 = 2.*(1.*rand())/(RAND_MAX+1.0) - 1.;
92      s = v1*v1+v2*v2;
93    }while(s>1);
94    float z = sqrt(-2.*log(s)/s)*v1;
95    y[i] = z * sigma + mean;
96  }
97}
98
Note: See TracBrowser for help on using the repository browser.