/***************************************************************** Program: randstp.c Generates random steps from a series of random numbers. *****************************************************************/ #include #include #include #include #include #include #define IBSIZ 1200000 #define OBSIZ 1000 int main(argc, argv) int argc; char *argv[]; { char ifn[100], ofn[100]; FILE *ifd, *ofd; char ibuf[IBSIZ]; /* Input Data Buffer */ float obuf[OBSIZ]; /* Output Data Buffer */ double ampli, period, sampint, srate; float ratio; int tfac, i, j, cnt1, n, N; int incnt, outcnt, npad, datasize; char *pc; short *ps; long *pl; pc = ibuf; ps = (short *) ibuf; pl = (long *) ibuf; printf("%s", "Enter name of file containing the random numbers: "); scanf("%s", ifn); datasize = 2; ifd = fopen(ifn,"r"); n = 150372; fread(ibuf, datasize, n, ifd); for (i = 0; i < n; i++) { if (datasize == 1 && *pc++ == 0) break; else if (datasize == 2 && *ps++ == 0) break; else if (datasize == 4 && *pl++ == 0) break; } n = i; printf(" '%s' contains %d values; enter number of ouput steps ", ifn, n); scanf("%d", &incnt); if (incnt > n) { fprintf(stderr,"randstp: bad input value\n"); exit(1); } ampli = 1.0; printf("%s", "Enter clock period, that is, the minimum duration between steps (secs, integer) "); scanf("%lf", &period); printf("%s", "Enter sample rate of output series (Hz) "); scanf("%lf", &srate); sampint = 1. / srate; if (sampint == 0) { fprintf(stderr,"randstp: bad sample interval\n"); exit(1); } ratio = (period / sampint); tfac = (int)(period / sampint); if ((ratio - (float) tfac) != 0.0) { fprintf(stderr,"randstp: sampint must be a multiple of clock period. Exiting.\n"); exit(1); } printf("%s", "Enter name of output file (floats) "); scanf("%s", ofn); ofd = fopen(ofn, "w"); /* zeroes padded at beg */ printf("%s", "Enter number of zeroes to pad at the beginning: "); scanf("%d", &npad); for (i = 0; i < OBSIZ; i++) obuf[i] = 0; outcnt = 0; N = npad / OBSIZ; for (i = 0; i < N; i++) fwrite(obuf, sizeof(float), OBSIZ, ofd); N = npad % OBSIZ; fwrite(obuf, sizeof(float), N, ofd); outcnt += npad; /* now make steps */ cnt1 = 1; ampli = -ampli; i = j = 0; pc = ibuf; ps = (short *) ibuf; pl = (long *) ibuf; while (1) { if (--cnt1 <= 0) { if (datasize == 1) cnt1 = *pc++ * tfac; else if (datasize == 2) cnt1 = *ps++ * tfac; else if (datasize == 4) cnt1 = *pl++ * tfac; ampli = -ampli; ++j; } if ( j > incnt ) { fwrite(obuf, sizeof(float), i, ofd); break; } ++outcnt; obuf[i] = ampli; if ( ++i >= OBSIZ ) { fwrite(obuf, sizeof(float), OBSIZ, ofd); i = 0; } } /* zeroes padded at end */ printf("Enter number of zeroes to pad at the end: "); scanf("%d", &npad); for (i = 0; i < OBSIZ; i++) obuf[i] = 0; N = npad / OBSIZ; for (i = 0; i < N; i++) fwrite(obuf, sizeof(float), OBSIZ, ofd); N = npad % OBSIZ; fwrite(obuf, sizeof(float), N, ofd); outcnt += npad; printf(" file '%s' written, %d points.\n", ofn, outcnt); fclose(ofd); exit(0); }