home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / turbo_c / fft.zip / GEN.C < prev    next >
C/C++ Source or Header  |  1988-08-07  |  1KB  |  63 lines

  1. /*
  2.  *    gen.c
  3.  *
  4.  *    C Version 1.0 by Steve Sampson, Public Domain
  5.  *
  6.  *    This program is used to generate time domain sinewave data
  7.  *    for fft.c.  If you want an opening target - negate the test frequency
  8.  *
  9.  *    Usage: gen samples output
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <alloc.h>
  14. #include <math.h>
  15.  
  16. #define    PI2    ((double)2.0 * M_PI)
  17.  
  18. main(argc, argv)
  19. int    argc;
  20. char    *argv[];
  21. {
  22.     FILE    *fp;
  23.     double    sample, freq, time, *real, *imag;
  24.     int    loop, samples;
  25.  
  26.     if (argc != 3)  {
  27.         printf("Usage: gen samples output_file\n");
  28.         printf("Where samples is a power of 2\n");
  29.         exit(-1);
  30.     }
  31.  
  32.     if ((fp = fopen(argv[2], "wb")) == (FILE *)NULL)  {
  33.         printf("Unable to create write file\n");
  34.         exit(-1);
  35.     }
  36.  
  37.     samples = abs(atoi(argv[1]));
  38.  
  39.     real = (double *)malloc(samples * sizeof(double));
  40.     imag = (double *)malloc(samples * sizeof(double));
  41.  
  42.     printf("Input The Test Frequency (Hz) ? ");
  43.     scanf("%lf", &freq);
  44.     printf("Input The Sampling Frequency (Hz) ? ");
  45.     scanf("%lf", &sample);
  46.     sample = (double)1.0 / sample;
  47.  
  48.     time = (double)0.0;
  49.     for (loop = 0; loop < samples; loop++)  {
  50.         real[loop] =  sin(PI2 * freq * time);
  51.         imag[loop] = -cos(PI2 * freq * time);
  52.         time += sample;
  53.     }
  54.  
  55.     fwrite(real, sizeof(double), samples, fp);
  56.     fwrite(imag, sizeof(double), samples, fp);
  57.  
  58.     fclose(fp);
  59.     putchar('\n');
  60. }
  61.  
  62. /* EOF */
  63.