FFT结果的额外时间,频率和采样率

时间:2015-06-23 15:31:04

标签: c++ audio signal-processing fft fftw

我尝试使用FFTW从随机音频文件(将由用户输入)应用FFT 这是我的代码:

 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. 通过这种方式,我可以获得最频繁的频率。但是,我怎样才能得到每个值的频率和时间间隔而不是幅度?
  2. 我如何知道输入音频的采样率?根据{{​​3}},获取频率需要采样率。

0 个答案:

没有答案