00001 #include "sadie.h"
00002
00003 #define NTIMES 12
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 void
00017 RANDOM (short nlin,
00018 short npix,
00019 short option,
00020
00021
00022 double sigma,
00023 IMAGE ** out
00024
00025 )
00026 {
00027 register short j, k, l;
00028 double noise, factor, bias;
00029 char msg[SLEN];
00030
00031 if (TIMES)
00032 TIMING (T_RANDOM);
00033 if (NAMES)
00034 {
00035 MESSAGE ('I', "");
00036 MESSAGE ('I', "RANDOM");
00037 MESSAGE ('I', "");
00038 sprintf (msg, " Image size: lines: %d", nlin);
00039 MESSAGE ('I', msg);
00040 sprintf (msg, " pixels: %d", npix);
00041 MESSAGE ('I', msg);
00042 if (option == UNIFORM)
00043 {
00044 sprintf (msg, " Uniform distribution, range: %12.4e", sigma);
00045 }
00046 else if (option == GAUSSIAN)
00047 {
00048 sprintf (msg, " Gaussian distribution, standard deviation: %12.4e",
00049 sigma);
00050 }
00051 MESSAGE ('I', msg);
00052 MESSAGE ('I', " ...............");
00053 }
00054
00055
00056 if (nlin <= 0)
00057 {
00058 MESSAGE ('E', " Number of lines must be greater than zero.");
00059 goto the_end;
00060 }
00061 else if (npix <= 0)
00062 {
00063 MESSAGE ('E', " Number of pixels/line must be greater than zero.");
00064 goto the_end;
00065 }
00066 else if (option != UNIFORM && option != GAUSSIAN)
00067 {
00068 MESSAGE ('E', " Option must be one of UNIFORM or GAUSSIAN.");
00069 goto the_end;
00070 }
00071 else if (sigma <= 0.0)
00072 {
00073 MESSAGE ('E', " Noise variance must be greater than zero.");
00074 goto the_end;
00075 }
00076
00077
00078 if (!CHECKIMG (*out))
00079 GETMEM (1, nlin, npix, out);
00080 if (!*out)
00081 goto the_end;
00082
00083 factor = (double) RAND_MAX;
00084 if (option == UNIFORM)
00085 {
00086 bias = 0.5;
00087 for (j = 0; j < nlin; j++)
00088 {
00089 for (k = 0; k < npix; k++)
00090 {
00091
00092
00093 noise = (double) rand () / factor;
00094
00095
00096 (*out)->data[0][j][k] = (PIXEL) (sigma * (noise - bias));
00097 }
00098 }
00099 }
00100 else
00101 {
00102 bias = (double) NTIMES / 2.0;
00103 for (j = 0; j < nlin; j++)
00104 {
00105 for (k = 0; k < npix; k++)
00106 {
00107
00108
00109 for (noise = 0.0, l = 0; l < NTIMES; l++)
00110 {
00111 noise += (double) rand ();
00112 }
00113 noise /= factor;
00114
00115
00116 (*out)->data[0][j][k] = (PIXEL) (sigma * (noise - bias));
00117 }
00118 }
00119 }
00120
00121 the_end:
00122 if (TIMES)
00123 TIMING (T_EXIT);
00124 }