CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2015
    Posts
    18

    Discrete Fourier Transform Test Results

    Hello. I have been testing my Discrete Fourier Transform program for 1 dimensional data (written in using C++).

    For the following test input data(X), the corresponding DFT(X) output is expected. The first number in each separate column is the REAL and the second number is the IMAGINARY.

    Code:
              
    
    X                      DFT X 
    ( 1.000, 0.000) ( 4.000, 0.000)
    ( 1.000, 0.000) ( 1.000,-2.414)
    ( 1.000, 0.000) ( 0.000, 0.000)
    ( 1.000, 0.000) ( 1.000,-0.414)
    ( 0.000, 0.000) ( 0.000, 0.000)
    ( 0.000, 0.000) ( 1.000, 0.414)
    ( 0.000, 0.000) ( 0.000, 0.000)
    ( 0.000, 0.000) ( 1.000, 2.414)
    My program produces the output below for the same input test data.
    Code:
    (0.5, 0)
    (0.125,  -0.302)
    (-2.3e-017, -2.78e-017)
    (0.125, -0.0518)
    (0, -3.06e-017)
    (0.125, 0.0518)
    (4.11e-017, -4.16e-017)
    (0.125, 0.302)

    The program code is below. I would appreciate to learn where the error lies and how to fix this, thank you!

    Code:
    bool inverse = false;
    int n = 8;
    double gRe[8] = {0,1,0,0,0,0,0,0};
    double gIm[8] = {0,0,0,0,0,0,0,0};
    double GRe[8] = {0,0,0,0,0,0,0,0};
    double GIm[8] = {0,0,0,0,0,0,0,0};
     
    
    for(int w = 0; w < n; w++)
    {
      GRe[w] = GIm[w] = 0;
      for(int x = 0; x < n; x++)
      {
        double a = -2 * pi * w * x / float(n);
        if(inverse) a = -a;
        double ca = cos(a);
        double sa = sin(a);
        GRe[w] += gRe[x] * ca - gIm[x] * sa;
        GIm[w] += gRe[x] * sa + gIm[x] * ca;
      }
      if(!inverse)
      {
        GRe[w] /= n;
        GIm[w] /= n;
      }
    }
    
    fstream DFTdata;
    DFTdata.open("DFT_Bdata.txt");
    for (int i = 0; i<n; i++) // width  
    {
       DFTdata <<"Input: "<< testReal[i]
    	             <<" Real: "<< setprecision(3)<<FReal[i] <<" Imag:  "<<setprecision(3)<<FIma[i]<< endl;
    }
    	
    DFTdata.close();
    Last edited by DavyS; October 19th, 2015 at 05:45 AM.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Discrete Fourier Transform Test Results

    1) I don't see how you get the output you posted with the code you posted.

    2) The main difference the two outputs seems to be that you divide by the number of points.

  3. #3
    Join Date
    Oct 2015
    Posts
    18

    Re: Discrete Fourier Transform Test Results

    Quote Originally Posted by Philip Nicoletti View Post
    1) I don't see how you get the output you posted with the code you posted.

    2) The main difference the two outputs seems to be that you divide by the number of points.
    Thanks for your feedback. My program is working now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured