Advertisement
eXFq7GJ1cC

Untitled

Mar 1st, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <fftw3.h>
  5. #define N 1764                  //size of array input to FFT
  6.  
  7. int main()
  8. {
  9.     double *data = (double *) malloc(sizeof(double) * N);       //input data of size n
  10.     fftw_complex *fft = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * N); //fftw data of size n
  11.     double x, cc, ts;           //x is a dummy variable, cc is the output*complex conjugate of the FFT output, ts is the time of each sample
  12.     fftw_plan plan;
  13.     FILE *voice;
  14.     FILE *out;
  15.     FILE *ccout;
  16.     FILE *time;
  17.     int i, j, p;
  18.     int ch;
  19.     long nxl = 0;
  20.     FILE *fp;
  21.     double a, mx;
  22.  
  23.     ts = N / 44100;             //number of samples taken/number of samples per second
  24.  
  25.     if ((fp = fopen("sinwav.dat", "r")) == NULL) {        //open the file for reading
  26.         printf("Can`t open sinwav.dat\n");
  27.         exit(1);
  28.     }
  29.  
  30.  
  31.     while ((ch = getc(fp)) != EOF) {     //count the number of line
  32.         if (ch == '\n')
  33.             nxl++;
  34.     }
  35.  
  36.     fclose(fp);                 //close this
  37.  
  38.     printf("File sinwav.dat has %ld lines\n", nxl);     //the number of lines in the file
  39.     time = fopen("time.dat", "w");      //create the file for writing to
  40.     if ((voice = fopen("anna.dat", "r")) == NULL) {
  41.         printf("Cannot open %s\n", "anna.dat");
  42.         exit(EXIT_FAILURE);
  43.     }
  44.  
  45.     for (p = 0; p < nxl / N; p++) {       //repeat the following for the number of lines in file divided by the number of files per input to FFT
  46.  
  47.         for (i = 0; i < N; i++) {       //input lines from file into data
  48.             fscanf(voice, "%lf %lf", &x, &data[i]);     //I think I'm right in saying fscanf carries on scanning from the file stream, so will pick up where it left off
  49.         }
  50.  
  51.         plan = fftw_plan_dft_r2c_1d(N, data, fft, FFTW_ESTIMATE);       //plan the FFT
  52.         fftw_execute(plan);     //execute it
  53.  
  54.         for (j = 0; j < N; j++) {       //find the maximum value for a given FFT
  55.  
  56.             cc = fft[0][j] * fft[0][j] + fft[1][j] * fft[1][j];
  57.  
  58.             if (j = 0) {
  59.                 mx = (j * 44100) / (2 * N);
  60.                 a = cc;
  61.             } else {
  62.  
  63.                 if (cc > a) {
  64.                     mx = (j * 44100) / (2 * N);
  65.                     a = cc;
  66.                 }
  67.             }
  68.         }
  69.         fprintf(time, "%lf\t%lf\t%lf\n", p * ts, mx, a);        //print this to file
  70.     }
  71.     fclose(out);
  72.     fclose(ccout);
  73.     fclose(time);
  74.     exit(0);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement