Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [C++] Fast Fourier Transform algorithm does not work properly

[C++] Fast Fourier Transform algorithm does not work properly

Scheduled Pinned Locked Moved C / C++ / MFC
c++comalgorithmstutorialquestion
6 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14168701
    wrote on last edited by
    #1

    Hello, there is a pseudocode for recursive radix 2 DIT Fast Fourier Transform(page 11,12) at this link. I have written a code in c++ which does the same, but it doesn't work properly. For example when I take 1024 datapoints of sin(0.1*x) where x={0,1,...1023}, I get this. Reverse transform inserted for this gives initial function multiplied by 1024, so it works. What could be wrong? I tried with value dir and dir/2 inside "Shift" function too. Here is the code: Notation: E2-even part, E3-odd part I work without complex numbers library, so there are both "_re" and "_im" tables.

    void FFT(double *tab_re, double *tab_im, double *results_re, double *results_im, int n, int dir) //n-data size
    {
    if(n==1)
    {results_re[0]=tab_re[0]; //real values
    results_im[0]=tab_im[0]; //imaginary
    return;}

    int nh=n/2;

    double *E2_re=new double [nh];
    double *E3_re=new double [nh];
    double *E2_im=new double [nh];
    double *E3_im=new double [nh];

    double *F2_re=new double [nh];
    double *F3_re=new double [nh];
    double *F2_im=new double [nh];
    double *F3_im=new double [nh];

    for(int k=0;k

    D L M 3 Replies Last reply
    0
    • M Member_14168701

      Hello, there is a pseudocode for recursive radix 2 DIT Fast Fourier Transform(page 11,12) at this link. I have written a code in c++ which does the same, but it doesn't work properly. For example when I take 1024 datapoints of sin(0.1*x) where x={0,1,...1023}, I get this. Reverse transform inserted for this gives initial function multiplied by 1024, so it works. What could be wrong? I tried with value dir and dir/2 inside "Shift" function too. Here is the code: Notation: E2-even part, E3-odd part I work without complex numbers library, so there are both "_re" and "_im" tables.

      void FFT(double *tab_re, double *tab_im, double *results_re, double *results_im, int n, int dir) //n-data size
      {
      if(n==1)
      {results_re[0]=tab_re[0]; //real values
      results_im[0]=tab_im[0]; //imaginary
      return;}

      int nh=n/2;

      double *E2_re=new double [nh];
      double *E3_re=new double [nh];
      double *E2_im=new double [nh];
      double *E3_im=new double [nh];

      double *F2_re=new double [nh];
      double *F3_re=new double [nh];
      double *F2_im=new double [nh];
      double *F3_im=new double [nh];

      for(int k=0;k

      D Offline
      D Offline
      Daniel Pfeffer
      wrote on last edited by
      #2

      If you get the original function back after the reverse, you have probably coded the transform correctly. The problem is likely to be that your sampling rate is not a multiple of the basic frequency of sin(0.1*x). This means that you get a large FFT component close to the frequency, but you also get "noise" throughout the range, due to the fact that the residues do not cancel out. I would try taking an FFT of sin(x), sin(2*x), etc., and see if these functions give the expected values.

      Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

      M 1 Reply Last reply
      0
      • D Daniel Pfeffer

        If you get the original function back after the reverse, you have probably coded the transform correctly. The problem is likely to be that your sampling rate is not a multiple of the basic frequency of sin(0.1*x). This means that you get a large FFT component close to the frequency, but you also get "noise" throughout the range, due to the fact that the residues do not cancel out. I would try taking an FFT of sin(x), sin(2*x), etc., and see if these functions give the expected values.

        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

        M Offline
        M Offline
        Member_14168701
        wrote on last edited by
        #3

        Thank You for the hint. I have tried with sin(nx), sin(2pi*x/n) and sin(2pi*x*n) too, but results were similar. The most promising result was for the option with sin(2pi*x*n): here is a result with n=10. Values for x>270 are 0. I used decimation in frequency FFT instead of decimation in time version, but it still doesn't show good values.

        1 Reply Last reply
        0
        • M Member_14168701

          Hello, there is a pseudocode for recursive radix 2 DIT Fast Fourier Transform(page 11,12) at this link. I have written a code in c++ which does the same, but it doesn't work properly. For example when I take 1024 datapoints of sin(0.1*x) where x={0,1,...1023}, I get this. Reverse transform inserted for this gives initial function multiplied by 1024, so it works. What could be wrong? I tried with value dir and dir/2 inside "Shift" function too. Here is the code: Notation: E2-even part, E3-odd part I work without complex numbers library, so there are both "_re" and "_im" tables.

          void FFT(double *tab_re, double *tab_im, double *results_re, double *results_im, int n, int dir) //n-data size
          {
          if(n==1)
          {results_re[0]=tab_re[0]; //real values
          results_im[0]=tab_im[0]; //imaginary
          return;}

          int nh=n/2;

          double *E2_re=new double [nh];
          double *E3_re=new double [nh];
          double *E2_im=new double [nh];
          double *E3_im=new double [nh];

          double *F2_re=new double [nh];
          double *F3_re=new double [nh];
          double *F2_im=new double [nh];
          double *F3_im=new double [nh];

          for(int k=0;k

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          You lost me with your translation of the code .. page 11 & 12 have the exp function which doesn't appear in your code?

          In vino veritas

          M 1 Reply Last reply
          0
          • L leon de boer

            You lost me with your translation of the code .. page 11 & 12 have the exp function which doesn't appear in your code?

            In vino veritas

            M Offline
            M Offline
            Member_14168701
            wrote on last edited by
            #5

            The exp function is inside function called "Shift". As I wrote earlier, I don't use complex numbers library, so there are both tables for real and imagery parts. I used this relation: exp(i*phi)=cos(phi)+i*sin(phi).

            1 Reply Last reply
            0
            • M Member_14168701

              Hello, there is a pseudocode for recursive radix 2 DIT Fast Fourier Transform(page 11,12) at this link. I have written a code in c++ which does the same, but it doesn't work properly. For example when I take 1024 datapoints of sin(0.1*x) where x={0,1,...1023}, I get this. Reverse transform inserted for this gives initial function multiplied by 1024, so it works. What could be wrong? I tried with value dir and dir/2 inside "Shift" function too. Here is the code: Notation: E2-even part, E3-odd part I work without complex numbers library, so there are both "_re" and "_im" tables.

              void FFT(double *tab_re, double *tab_im, double *results_re, double *results_im, int n, int dir) //n-data size
              {
              if(n==1)
              {results_re[0]=tab_re[0]; //real values
              results_im[0]=tab_im[0]; //imaginary
              return;}

              int nh=n/2;

              double *E2_re=new double [nh];
              double *E3_re=new double [nh];
              double *E2_im=new double [nh];
              double *E3_im=new double [nh];

              double *F2_re=new double [nh];
              double *F3_re=new double [nh];
              double *F2_im=new double [nh];
              double *F3_im=new double [nh];

              for(int k=0;k

              M Offline
              M Offline
              Member 14331766
              wrote on last edited by
              #6

              Bonjour, //hi essayez avec : //try with arg=v*pi*i/n; k++); //two time i++);

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups