2015-05-22 63 views
2

我正在用fftw3库编写一个简单的复杂到DFT的C代码的代码。 我写了一个文件与输入数组双数据,所以我可以与matlab fft函数进行比较。 我尝试从变换数组中执行反向变换,但结果和第一个输入数组是不同的。这是我的结果:fftw3逆变换不起作用

FFTW3 TRANSFORM WELCOME <<<<< 

enter the number (integer) N of samples (Bit: 64) (preferably power of 2):8 

SAMPLE INPUT 
in[0][0] = -216448918.015237  in[0][1] = 0.000000 
in[1][0] = 948904790.062151   in[1][1] = 0.000000 
in[2][0] = 826811206.185300   in[2][1] = 0.000000 
in[3][0] = 1868763250.342451  in[3][1] = 0.000000 
in[4][0] = 703135606.077152   in[4][1] = 0.000000 
in[5][0] = -1989016445.622210  in[5][1] = 0.000000 
in[6][0] = 1912963650.704585  in[6][1] = 0.000000 
in[7][0] = 811527262.805480   in[7][1] = 0.000000 

Hit enter to continue ... 


FORWARD TRANSFORM COEFFICIENTS 

out[0][0] = 4866640402.539672  out[0][1] = 0.000000 
out[1][0] = 410260768.150135  out[1][1] = -1738850319.926936 
out[2][0] = -2253088168.827970  out[2][1] = 3720402168.707990 
out[3][0] = -2249429816.334913  out[3][1] = -3911155208.965507 
out[4][0] = 1586282687.363928  out[4][1] = 0.000000 
out[5][0] = -2249429816.334913  out[5][1] = 3911155208.965507 
out[6][0] = -2253088168.827970  out[6][1] = -3720402168.707990 
out[7][0] = 410260768.150135  out[7][1] = 1738850319.926936 
do you want to calculate the inverse-transform? (y/n) 
y 


INVERSE TRANSFORM COEFFICIENTS 
rev[0][0] = -1731591344.121896  rev[0][1] = 0.000000 
rev[1][0] = 7591238320.497208  rev[1][1] = 0.000000 
rev[2][0] = 6614489649.482399  rev[2][1] = 0.000000 
rev[3][0] = 14950106002.739609  rev[3][1] = 0.000000 
rev[4][0] = 5625084848.617215  rev[4][1] = 0.000000 
rev[5][0] = -15912131564.977680  rev[5][1] = 0.000000 
rev[6][0] = 15303709205.636681  rev[6][1] = 0.000000 
rev[7][0] = 6492218102.443840  rev[7][1] = 0.000000 

正如你看到'in'和'rev'数组是不同的但直接转换是正确的。我将它与matlab进行了比较,结果相同。 当我用matlab执行逆变换时,我获得输入数组。 我能做什么?

这是我的C代码:

#include <fftw3.h> 
#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define PI 3.141592653589 

int main() 
{ 

    fftw_complex *in, *out, *rev; 
    int i,f0,A,N; 
    char no; 
    FILE *fp; 
    fftw_plan p; 

    printf("\n\n>>>>> FFTW3 TRANSFORM WELCOME <<<<<"); 
    printf("\n\n enter the number (integer) N of samples (bit: %ld) (preferably power of 2):",(sizeof(fftw_complex)/2)*8); 
    scanf("%d",&N); 

    //f0 = 50; 
    //A = 1; 


    //allocating memory for input & output arrays 
    in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N); 
    out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N); 
    rev = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*N); 



    //Opening the data file 
    if((fp=fopen("lista_numeri_double.dat","rb"))==NULL) 
    { 
    printf("\nError reading file\n"); 
    exit(1); 
    } 


    printf("\nSAMPLE INPUT"); 
    //assigning samples read from the file 
    for(i=0;i<N;i++) 
    { 
    //in[i][0] = A * cos(2*PI*f0*i/N); 
    fread(&in[i][0],sizeof(double),1,fp); 
    in[i][1]=0; 

    printf("\nin[%d][0] = %f \t\tin[%d][1] = %f",i,in[i][0],i,in[i][1]); 
    } 


    //plan and execute transform 
    p = fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_ESTIMATE); 
    fftw_execute(p); 

    printf("\n\n Hit enter to continue ... \n"); 
    scanf("%c",&no); 


    //print output values 
    printf("\n\nFORWARD TRANSFORM COEFFICIENTS\n"); 
    for(i=0;i<N;i++) 
    { 
    printf("\nout[%d][0] = %f \t\tout[%d][1] = %f",i,out[i][0],i,out[i][1]); 
    } 

    fftw_destroy_plan(p); 

    printf("\n do you want to calculate the inverse-transform? (y/n) \n"); 
    scanf ("%c",&no); 

    if(no=='y') 
    { 

    //plan and execute inverse transform 
    p = fftw_plan_dft_1d(N,out,rev,FFTW_BACKWARD,FFTW_ESTIMATE); 
    fftw_execute(p); 

    printf("\n\nINVERSE TRANSFORM COEFFICIENTS\n"); 
    for(i=0;i<N;i++) 
    { 
     printf("rev[%d][0] = %f \t\trev[%d][1] = %f\n",i,rev[i][0],i,rev[i][1]); 
    } 

    fftw_destroy_plan(p);   
    } 

    return 0; 
} 

回答

2

Matlab和FFTW的区别自带应用于变换的缩放因子。

虽然Matlab's FFT被归一化,但由FFTW使用的算法如in FFTW's documentation所述,未被归一化。换句话说,使用FFTW的全圆变换(前向后跟后向)将结果缩放为因子N

相应地,比较inrev阵列示出了rev是由8一致因子(大小在您的示例的变换中使用的N)缩放。

+0

我认为这些天matlab偷走'fftw'并将其合并为自己的。 [3] FFTW(http://www.fftw.org) [4] Frigo,M.和SG Johnson,“FFTW:一种适用于FFT的自适应软件结构”,Proceedings of the International Conference on Acoustics,Speech,和Signal Processing,Vol。 3,1998,pp。1381-1384。 –

+0

@GAlexander afaik matlab使用的是fftw的修改版本,为了向后兼容,为了方便进行缩放。 – SleuthEye

+0

在pdf天fftw会粉碎matlab fft这么多,我没有人知道使用matlabs实现。 –