Verifying correctness of FFT algorithm

前端 未结 2 2004
心在旅途
心在旅途 2020-12-19 21:48

Today I wrote an algorithm to compute the Fast Fourier Transform from a given array of points representing a discrete function. Now I\'m trying to test it to see if it is w

相关标签:
2条回答
  • 2020-12-19 22:19
    1. Validation

      look here: slow DFT,iDFT at the end is mine slow implementation of DFT and iDFT they are tested and correct. I also used them for fast implementations validation in the past.

    2. Your code

      stop recursion is wrong (you forget to set the return element) mine looks like this:

      if (n<=1) { if (n==1) { dst[0]=src[0]*2.0; dst[1]=src[1]*2.0; } return; }
      

      so when your N==1 set the output element to Re=2.0*real[0], Im=2.0*imaginary[0] before return. Also I am a bit lost in your complex math (t,t1,t2) and to lazy to analyze.

    Just to be sure here is mine fast implementation. It need too much things from class hierarchy so it will not be of another use for you then visual comparison to your code.

    My Fast implementation (cc means complex output and input):

    //---------------------------------------------------------------------------
    void transform::DFFTcc(double *dst,double *src,int n)
        {
        if (n>N) init(n);
        if (n<=1) { if (n==1) { dst[0]=src[0]*2.0; dst[1]=src[1]*2.0; } return; }
        int i,j,n2=n>>1,q,dq=+N/n,mq=N-1;
        // reorder even,odd (buterfly)
        for (j=0,i=0;i<n+n;) { dst[j]=src[i]; i++; j++; dst[j]=src[i]; i+=3; j++; }
        for (    i=2;i<n+n;) { dst[j]=src[i]; i++; j++; dst[j]=src[i]; i+=3; j++; }
        // recursion
        DFFTcc(src  ,dst  ,n2); // even
        DFFTcc(src+n,dst+n,n2); // odd
        // reorder and weight back (buterfly)
        double a0,a1,b0,b1,a,b;
        for (q=0,i=0,j=n;i<n;i+=2,j+=2,q=(q+dq)&mq)
            {
            a0=src[j  ]; a1=+_cos[q];
            b0=src[j+1]; b1=+_sin[q];
            a=(a0*a1)-(b0*b1);
            b=(a0*b1)+(a1*b0);
            a0=src[i  ]; a1=a;
            b0=src[i+1]; b1=b;
            dst[i  ]=(a0+a1)*0.5;
            dst[i+1]=(b0+b1)*0.5;
            dst[j  ]=(a0-a1)*0.5;
            dst[j+1]=(b0-b1)*0.5;
            }
        }
    //---------------------------------------------------------------------------
    


    dst[] and src[] are not overlapping !!! so you cannot transform array to itself .
    _cos and _sin are precomputed tables of cos and sin values (computed by init() function like this:

        double a,da; int i;
        da=2.0*M_PI/double(N);
        for (a=0.0,i=0;i<N;i++,a+=da) { _cos[i]=cos(a); _sin[i]=sin(a); }
    


    N is power of 2 (zero padded size of data set) (last n from init(n) call)

    Just to be complete here is mine complex to complex slow version:

    //---------------------------------------------------------------------------
    void transform::DFTcc(double *dst,double *src,int n)
        {
        int i,j;
        double a,b,a0,a1,_n,b0,b1,q,qq,dq;
        dq=+2.0*M_PI/double(n); _n=2.0/double(n);
        for (q=0.0,j=0;j<n;j++,q+=dq)
            {
            a=0.0; b=0.0;
            for (qq=0.0,i=0;i<n;i++,qq+=q)
                {
                a0=src[i+i  ]; a1=+cos(qq);
                b0=src[i+i+1]; b1=+sin(qq);
                a+=(a0*a1)-(b0*b1);
                b+=(a0*b1)+(a1*b0);
                }
            dst[j+j  ]=a*_n;
            dst[j+j+1]=b*_n;
            }
        }
    //---------------------------------------------------------------------------
    
    0 讨论(0)
  • 2020-12-19 22:24

    I'd use something authoritative like Wolfram Alpha to verify.

    If I evalute cos(i/2) for 0 <= i < 32, I get this array:

    [1,0.878,0.540,0.071,-0.416,-0.801,-0.990,-0.936,-0.654,-0.211,0.284,0.709,0.960,0.977,0.754,0.347,-0.146,-0.602,-0.911,-0.997,-0.839,-0.476,0.004,0.483,0.844,0.998,0.907,0.595,0.137,-0.355,-0.760,-0.978]
    

    If I give that as input to Wolfram Alpha's FFT function I get this result.

    The plot that I get looks symmetric, which makes sense. The plot looks nothing like any of the ones you supplied.

    0 讨论(0)
提交回复
热议问题