0

I tried to apply FFT from random audio file (will input by user) by using FFTW.
Here is my code:

 void window(double in[], double out[], int N){
      for (int i = 0; i < N; i++) {
          double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N - 1)));
          out[i] = multiplier * in[i];
      }
  }

 int main (int argc, char * argv []) {
     char        *infilename ;
     SNDFILE     *infile = NULL ;
     FILE        *outfile = NULL ;
     SF_INFO     sfinfo ;
     infile = sf_open("test.wav", SFM_READ, &sfinfo);

     int N = pow(2, 10); // FFT size
     double samples[N];
     double samples_windows[N];

     // read into samples
     sf_read_double(infile, samples, N);

     fftw_complex out[N];
     fftw_plan p;

     // apply hann Windows function
     window(samples, samples_windows, N);

     // create 1D plan
     p = fftw_plan_dft_r2c_1d(N, samples_windows, out, FFTW_ESTIMATE);

    // excute FFT
    fftw_execute(p);

    fftw_destroy_plan(p); 

    printf("Real         Img\n\n");
    for (int i=0; i<N; i++) {
         printf("%f   %f\n", out[i][0], out[i][1]);
    }

    double magnitude[N];
    for (int i=0; i<N; i++) {             
         printf("%i %f\n", i, sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]));
         magnitude[i] = sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]);
     }


    double max = max_array(magnitude, N); // find max value in array
    printf("Most occurring frequencies = %f", max);
    sf_close (infile) ;

    return 0 ;
  }
  1. By this way, I can get the most occurring frequencies. But how can I get the frequencies and time intervals of each value instead of magnitude?
  2. How can I know the sample rate of input audio? According to this answer , sample rate is required for getting frequency.
Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
  • The SF_INFO struct should have the sample rate in it. – Adam Finley Jun 23 '15 at 15:49
  • @AdamFinley Thank you so much, I got it. Do you have any idea for calculating time intervals? – ductran Jun 23 '15 at 16:00
  • 2
    There are no "time intervals" in a single FFT - you need to produce a spectrogram if you want to see time-varying information. A spectrogram is a sequence of (usually overlapping) power spectra - you've generated one spectrum above, you need to generate more than one in order to represent the time-varying information. – Paul R Jun 23 '15 at 16:35
  • @PaulR Do you have any example or document for it? As I'm just a newbie in audio processing, I don't know where should I start. Thank you. – ductran Jun 24 '15 at 03:37
  • I suggest you start with the [Wikipedia entry for spectrogram](https://en.wikipedia.org/wiki/Spectrogram) - that explains in more detail what I outlined in my comment above. If you have further questions about the theory you can also try reading and asking questions on http://dsp.stackexchange.com – Paul R Jun 24 '15 at 05:42

0 Answers0