title: WAV-file analysis C (libsndfile, fftw3)
body: <p>I'm trying to develop a simple C application that can give a value from 0-100 at a certain frequency range at a given timestamp in a WAV-file.</p>
  
  <p>Example: I have frequency range of 44.1kHz (typical MP3 file) and I want to split that range into n amount of ranges (starting from 0). I then need to get the amplitude of each range, being from 0 to 100. </p>
  
  <p><strong>What I've managed so far:</strong></p>
  
  <p>Using libsndfile I'm now able to read the data of a WAV-file. </p>
  
  <pre><code>infile = sf_open(argv [1], SFM_READ, &amp;sfinfo);
  
  float samples[sfinfo.frames];
  
  sf_read_float(infile, samples, 1);
  </code></pre>
  
  <p>However, my understanding of FFT is rather limited. But I know it's required inorder to get the amplitudes at the ranges I need. But how do I move on from here? I found the library FFTW-3, which seems to be suited for the purpose.</p>
  
  <p>I found some help here: <a href="https://stackoverflow.com/a/4371627/1141483">https://stackoverflow.com/a/4371627/1141483</a></p>
  
  <p>and looked at the FFTW tutorial here: <a href="http://www.fftw.org/fftw2_doc/fftw_2.html" rel="nofollow noreferrer">http://www.fftw.org/fftw2_doc/fftw_2.html</a></p>
  
  <p>But as I'm unsure about the behaviour of the FFTW, I don't know to progress from here.</p>
  
  <p>And another question, assuming you use libsndfile: If you force the reading to be single channeled (with a stereo file) and then read the samples. Will you then actually only be reading half of the samples of the total file? As half of them being from channel 1, or does automaticly filter those out?</p>
  
  <p>Thanks a ton for your help.</p>
  
  <p>EDIT: My code can be seen here:</p>
  
  <pre><code>double blackman_harris(int n, int N){
  double a0, a1, a2, a3, seg1, seg2, seg3, w_n;
  a0 = 0.35875;
  a1 = 0.48829;
  a2 = 0.14128;
  a3 = 0.01168;
  
  seg1 = a1 * (double) cos( ((double) 2 * (double) M_PI * (double) n) / ((double) N - (double) 1) );
  seg2 = a2 * (double) cos( ((double) 4 * (double) M_PI * (double) n) / ((double) N - (double) 1) );
  seg3 = a3 * (double) cos( ((double) 6 * (double) M_PI * (double) n) / ((double) N - (double) 1) );
  
  w_n = a0 - seg1 + seg2 - seg3;
  return w_n;
  }
  
  int main (int argc, char * argv [])
  {   char        *infilename ;
  SNDFILE     *infile = NULL ;
  FILE        *outfile = NULL ;
  SF_INFO     sfinfo ;
  
  
  infile = sf_open(argv [1], SFM_READ, &amp;sfinfo);
  
  int N = pow(2, 10);
  
  fftw_complex results[N/2 +1];
  double samples[N];
  
  sf_read_double(infile, samples, 1);
  
  
  double normalizer;
  int k;
  for(k = 0; k &lt; N;k++){
      if(k == 0){
  
          normalizer = blackman_harris(k, N);
  
      } else {
          normalizer = blackman_harris(k, N);
      }
  
  }
  
  normalizer = normalizer * (double) N/2;
  
  
  
  fftw_plan p = fftw_plan_dft_r2c_1d(N, samples, results, FFTW_ESTIMATE);
  
  fftw_execute(p);
  
  
  int i;
  for(i = 0; i &lt; N/2 +1; i++){
      double value = ((double) sqrtf(creal(results[i])*creal(results[i])+cimag(results[i])*cimag(results[i]))/normalizer);
      printf("%f\n", value);
  
  }
  
  
  
  sf_close (infile) ;
  
  return 0 ;
  } /* main */
  </code></pre>