2014-06-10 60 views
0

我正在讀取一個原始聲音文件,我試圖在它上面運行fft,目標是在最後得到PSD,但是我在開始時獲得一個錯誤,我無法理解,希望及彼一些幫助,代碼:關於真實數據序列的FFTW

#include <stdio.h> 
#include <fftw3.h> 

int main(){ 
    char* fileName = "sound.raw"; 
    FILE* inp = NULL; 
double* data = NULL; 
int index = 0; 
fftw_plan plan; 
fftw_complex* out; 
double r,i; 
int N = 8192; 

//Allocating the memory for the input data 

data = (double*) fftw_malloc(sizeof(double)*N); 
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N); 
plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_FORWARD); 
// opening the file for reading 
inp = fopen(fileName,"rb"); 
if(inp== NULL){ 
    printf(" couldn't open the file \n "); 
    return -1; 
} 
while(!feof(inp)){ 
    printf(" index %d",index); // just get where the program crashs 
     if(index < N){ 
      fread(&data[index],sizeof(short),1,inp); 
      index = index +1; 
     } 
     else{ 
      index = 0; 
      fftw_execute(plan); 
      printf("New Plan \n"); 
      printf(" Real \t imag \t Magn \t \n"); 
      for(index = 0 ; index<N; index++){ 
       r=out[index][0]; 
       i =out[index][1]; 
       printf("%lf \t %lf \t %lf \t \n",r,i,index); 
      } 
      index = 0 ; 
     } 
} 
return 0 ; 
} 

程序崩潰時index = 8106我敢肯定,該文件包含更多的數據。我得到的錯誤是:

Segmentation fault (core dumped) 

我知道,錯誤的指針試圖訪問,這是不允許的內存相關的,我的問題是如何解決這個問題!

UPDATE

我再次檢查程序和錯誤是完全一致:

fftw_execute(plan) ; 

我希望幫助更多!

在此先感謝!

+0

你的N有多大? – mvp

+0

int N = 8192; ? – Engine

+0

你似乎試圖將'short'數據讀入'double'的緩衝區? –

回答

0

找到它,誤差是在給定的計劃功能放慢參數:

plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_FORWARD); // 

應該代替

plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_MEASURE); 

plan = fftw_plan_dft_r2c_1d(N,data,out,FFTW_ESTIMATE); 

由於變換的方向隱含在函數的名字中!

謝謝!

+0

帶回家信息:在任何可能失敗的通話之後,請務必檢查錯誤! –