/* * Copyright (c) 2011, Norma Kuehn * All rights reserved. * */ // Pseudo random number generator 'ran1' used in the stimulation program. #include #include #include #include #define IA 16807 #define IM 2147483647 #define AM (1.0/IM) #define IQ 127773 #define IR 2836 #define NTAB 32 #define NDIV (1+(IM-1)/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) int iset = 0; double gset = 0; // function declarations double gauss(long &idum); void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if (nrhs < 3) mexErrMsgTxt("Input arguments are stixel width, stixel height, number of frames [and seed]."); if (nrhs > 4) mexErrMsgTxt("Input arguments are stixel width, stixel height, number of frames [and seed]."); if (nlhs != 1) mexErrMsgTxt("Wrong number of output arguments."); // required input arguments int xstixels, ystixels, nframes; long seed = -1000; float meanintensity = 0.5; float contrast = 1; // 0.3; //get input data if (mxIsDouble(prhs[0])) xstixels = (int)mxGetScalar(prhs[0]); else mexErrMsgTxt("Invalid class of input argument 'xpixels'."); if (mxIsDouble(prhs[1])) ystixels = (int)mxGetScalar(prhs[1]); else mexErrMsgTxt("Invalid class of input argument 'ypixels'."); if (mxIsDouble(prhs[2])) nframes = (int)mxGetScalar(prhs[2]); else mexErrMsgTxt("Invalid class of input argument 'nframes'."); if (nrhs == 4) { if (mxIsDouble(prhs[3])) { seed = (int)mxGetScalar(prhs[3]); if (seed > 0) seed = -seed; } else mexErrMsgTxt("Invalid class of input argument 'nframes'."); } // create pointer array double *stimulus = new double[xstixels*ystixels*nframes]; // generate array of random numbers for (int n=0; n=0;j--) { k=(idum)/IQ; idum=IA*(idum-k*IQ)-IR*k; if (idum < 0) idum += IM; if (j < NTAB) iv[j] = idum; } iy=iv[0]; } k=(idum)/IQ; idum=IA*(idum-k*IQ)-IR*k; if (idum < 0) idum += IM; j=iy/NDIV; iy=iv[j]; iv[j] = idum; if ((temp=AM*iy) > RNMX) return RNMX; else return temp; } double gauss(long &idum) { double fac,rsq,v1,v2; if (idum < 0) iset=0; if (iset == 0) { do { v1=2.0*ran1(idum)-1.0; v2=2.0*ran1(idum)-1.0; rsq=v1*v1+v2*v2; } while (rsq >= 1.0 || rsq == 0.0); fac=sqrt(-2.0*log(rsq)/rsq); gset=v1*fac; iset=1; return v2*fac; } else { iset=0; return gset; } }