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