Bsoft  2.1.0
Bernard's software package
Bimage_noise.cpp
Go to the documentation of this file.
1 
9 #include "Bimage.h"
10 #include "random_numbers.h"
11 #include "utilities.h"
12 
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15 
34 int Bimage::noise_uniform(double rmin, double rmax)
35 {
36  if ( rmin < dtmin ) rmin = dtmin;
37  if ( rmax > dtmax ) rmax = dtmax;
38 
39  if ( verbose & VERB_PROCESS ) {
40  cout << "Generating a random image with a uniform distribution:" << endl;
41  cout << "Minimum and maximum: " << rmin << " " << rmax << endl << endl;
42  }
43 
44  long i;
45  double range = (rmax - rmin)/get_rand_max();
46 
47  random_seed();
48 
49  for ( i=0; i<datasize; i++ ) add(i, random()*range + rmin);
50 
51  statistics();
52 
53  return 0;
54 }
55 
72 int Bimage::noise_gaussian(double ravg, double rstd)
73 {
74  if ( rstd <= 0 ) {
75  error_show("Error in Bimage::noise_gaussian: The standard deviation for a Gaussian distribution must be > 0!", __FILE__, __LINE__);
76  return -1;
77  }
78 
79  if ( verbose & VERB_PROCESS ) {
80  cout << "Generating a random image with a gaussian distribution:" << endl;
81  cout << "Average and standard deviation: " << ravg << " " << rstd << endl << endl;
82  }
83 
84  long i;
85 
86  random_seed();
87 
88  for ( i=0; i<datasize; i++ )
89  add(i, random_gaussian(ravg, rstd));
90 
91  statistics();
92 
93  return 0;
94 }
95 
115 int Bimage::noise_poisson(double ravg)
116 {
117  if ( ravg <= 0 ) {
118  error_show("Error in Bimage::noise_poisson: The average for a poisson distribution must be > 0!", __FILE__, __LINE__);
119  return -1;
120  }
121 
122  if ( verbose & VERB_PROCESS ) {
123  cout << "Generating a random image with a poisson distribution:" << endl;
124  cout << "Average: " << ravg << endl << endl;
125  }
126 
127  long i;
128 
129  random_seed();
130 
131  for ( i=0; i<datasize; i++ )
132  add(i, random_poisson(ravg));
133 
134  statistics();
135 
136  return 0;
137 }
138 
156 int Bimage::noise_logistical(double ravg, double rstd)
157 {
158  if ( verbose & VERB_PROCESS ) {
159  cout << "Generating a random image with a logistical differential distribution:" << endl;
160  cout << "Average and standard deviation: " << ravg << " " << rstd << endl << endl;
161  }
162 
163  long i;
164 
165  random_seed();
166 
167  for ( i=0; i<datasize; i++ )
168  add(i, random_logistical(ravg, rstd));
169 
170  statistics();
171 
172  return 0;
173 }
174 
184 int Bimage::noise_spectral(double alpha)
185 {
186  long i, j, nn, xx, yy, zz, cc, imgsize(x*y*z);
187  double x2, y2, z2, s2, w, sum;
188  double fac(-alpha/4); // Factor of 2 because we work with s2, and another 2 for amplitude scaling
189  double range(2.0/get_rand_max());
190 
191  if ( verbose & VERB_PROCESS ) {
192  cout << "Generating a spectral noise image:" << endl;
193  cout << "Alpha: " << alpha << endl << endl;
194  }
195 
196  for ( i=nn=0; nn<n; nn++ ) {
197  sum = 0;
198  for ( zz=0; zz<z; zz++ ) {
199  z2 = zz*1.0L/z;
200  if ( z2 >= 0.5 ) z2 -= 1;
201  z2 *= z2;
202  for ( yy=0; yy<y; yy++ ) {
203  y2 = yy*1.0L/y;
204  if ( y2 >= 0.5 ) y2 -= 1;
205  y2 *= y2;
206  for ( xx=0; xx<x; xx++, i++ ) {
207  x2 = xx*1.0L/x;
208  if ( x2 >= 0.5 ) x2 -= 1;
209  x2 *= x2;
210  s2 = x2 + y2 + z2;
211  if ( s2 ) {
212  w = pow(s2, fac);
213  for ( cc=0, j=i*c; cc<c; cc++, j++ )
214  set(j, w*(random()*range-1));
215  sum += w*w;
216  }
217  }
218  }
219  }
220  sum = sqrt(sum/(3*imgsize));
221  for ( j=nn*imgsize; j<(nn+1)*imgsize; j++ ) set(j, complex(j)/sum);
222  }
223 
224  fft_back();
225 
226  statistics();
227 
228  return 0;
229 }
Bimage::set
void set(long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:1118
random_logistical
double random_logistical(double avg, double std)
Generates a value with a logistical random distribution.
Definition: random_numbers.cpp:338
random_numbers.h
Header file for functions for creating random images.
Bimage::sum
void sum(long m, Bimage **p)
Sums an array of images with their FOM blocks.
Definition: Bimage_combine.cpp:23
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
random_gaussian
double random_gaussian(double avg, double std)
Generates a value with a gaussian random distribution.
Definition: random_numbers.cpp:123
Bimage::fft_back
int fft_back()
Definition: Bimage.h:831
Bimage::noise_uniform
int noise_uniform(double rmin, double rmax)
Generates an image with a uniform random distribution of densities.
Definition: Bimage_noise.cpp:34
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
random_poisson
double random_poisson(double avg)
Generates a value deviating from the average based on a poisson distribution.
Definition: random_numbers.cpp:221
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
Bimage::noise_logistical
int noise_logistical(double ravg, double rstd)
Generates an image with a gaussian random distribution of densities.
Definition: Bimage_noise.cpp:156
Bimage::noise_gaussian
int noise_gaussian(double ravg=0, double rstd=1)
Generates an image with a gaussian random distribution of densities.
Definition: Bimage_noise.cpp:72
Bimage::complex
Complex< double > complex(long j)
Returns a complex value at the given index.
Definition: Bimage.cpp:1027
Bimage.h
Header file for image class.
utilities.h
Header file for general utilities.
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Bimage::noise_poisson
int noise_poisson(double ravg)
Generates an image with a poisson random distribution of densities.
Definition: Bimage_noise.cpp:115
get_rand_max
long get_rand_max()
Finds the maximum random number for a system.
Definition: random_numbers.cpp:22
verbose
int verbose
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::noise_spectral
int noise_spectral(double alpha)
Generates a noise map with a defined spectral decay.
Definition: Bimage_noise.cpp:184