Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

FFT code help please

  1. Jan 17, 2014 #1
    ok I have been troubleshooting this code left and right... but I am still not getting what Mat lab is getting...

    can someone please look at this for me ?

    Code (Text):

           private float Fs;
           private int N;
           private Complex [] F;
           private int R;
           private Complex[] x;
     public DSPclass(float[] DSP1,int f1)
            {
               N = DSP1.Length;
               R = DSP1.Length;
               F = new Complex[N];
               Fs = (float)f1;
               x = new Complex[DSP1.Length];
           
              for (int v = 0; v < N; v++)
               {
                   x[v] = DSP1[v];        
              }
            }

       public void FFT1()
           {
              F = FFT(x);
            }

           public Complex[] FFT(Complex[] x)
           {
               int N2 = x.Length;
               Complex[] X = new Complex[N2];
               if (N2 == 1)
               {
                   return x;
               }
               Complex[] odd = new Complex[N2 / 2];
               Complex[] even = new Complex[N2 / 2];
               Complex[] Y_Odd = new Complex[N2 / 2];
               Complex[] Y_Even = new Complex[N2 / 2];


               for (int t = 0; t < N2 / 2; t++)
               {
                   even[t] = x[t * 2];    
                   odd[t] = x[(t * 2) + 1];
               }
               Y_Even = FFT(even);
               Y_Odd = FFT(odd);
               Complex temp4;

               for (int k = 0; k < (N2 / 2); k++)
               {
                   temp4 = Complex1(k, N);
                   X[k] = Y_Even[k] + (Y_Odd[k] * temp4);
                   X[k + (N2 / 2)] = Y_Even[k] - (Y_Odd[k] * temp4);  
                   }

               return X;
           }
     
  2. jcsd
  3. Jan 17, 2014 #2

    Mark44

    Staff: Mentor

    Start with a very small array of numbers and compare the results that your code gives with what Matlab gives.
     
  4. Jan 17, 2014 #3
    I did
    y = [ 0, 1, 2, 3, 3, 2, 1, 0, -1,-2, -3, -3, -2, -1, 0,1 ];

    for both math lab and my code

    I did get the same values as mat lab

    it seems to work for lower samples
     
  5. Jan 17, 2014 #4
    I just tried
    y = {11,22,33,44,55,66,66,55,44,33,22,11, 0, 1, 2, 3, 3, 2, 1, 0, -1,-2, -3, -3, -2, -1, 0,1,1,0,-1,0,-1,0,-1 };

    and it is way off
    mat lab
    has
    PHP:


    Trial>> y=[11,22,33,44,55,66,66,55,44,33,22,11, 0, 1, 2, 3, 3, 2, 1, 0, -1,-2, -3, -3, -2, -1, 0,1,1,0,-1,0,-1,0,-1 ];
    Trial>> ffty = fft(y)

    ffty =

       1.0e+02 *

      Columns 1 through 3

       4.6100 + 0.0000i   2.2170 - 3.5715i  -1.0620 - 2.4977i

      Columns 4 through 6

      -1.5877 - 0.4164i  -0.2102 + 0.3300i   0.0212 + 0.0054i

      Columns 7 through 9

      -0.0214 + 0.0261i   0.1521 - 0.1178i  -0.0251 - 0.2398i

      Columns 10 through 12

      -0.0924 - 0.0777i   0.0128 + 0.0035i   0.0015 - 0.0272i

      Columns 13 through 15

       0.0073 + 0.0093i   0.0761 - 0.0279i   0.0179 - 0.0859i

      Columns 16 through 18

       0.0060 - 0.0176i   0.0689 - 0.0205i   0.0379 - 0.0336i

      Columns 19 through 21

       0.0379 + 0.0336i   0.0689 + 0.0205i   0.0060 + 0.0176i

      Columns 22 through 24

       0.0179 + 0.0859i   0.0761 + 0.0279i   0.0073 - 0.0093i

      Columns 25 through 27

       0.0015 + 0.0272i   0.0128 - 0.0035i  -0.0924 + 0.0777i

      Columns 28 through 30

      -0.0251 + 0.2398i   0.1521 + 0.1178i  -0.0214 - 0.0261i

      Columns 31 through 33

       0.0212 - 0.0054i  -0.2102 - 0.3300i  -1.5877 + 0.4164i

      Columns 34 through 35

      -1.0620 + 2.4977i   2.2170 + 3.5715i


     
    and my code had:
    PHP:
     
    I do not know why
    can you help ?
     
  6. Jan 17, 2014 #5

    Mark44

    Staff: Mentor

    It looks to me to be a little off, but not way off. Matlab is scaling your answers with a multiplication factor of 100. If I take this into account and compare the first couple of Matlab results with yours, I see
    Matlab
    461 + 0i
    221.70 - 357.15i
    Yours (rounded)
    463 + 0i
    189.80 - 363.37i

    One problem I see in your code is numerous occurrences of N2/2. If N2 is odd, then N2/2 won't give the exact result. For example, 8/2 = 4, which you'd expect, by 9/2 = 4 as well, which you might not expect.

    In your first example, your array had an even number of elements, but in your second example, your array had an odd number of elements. I think this might be what's giving you incorrect results. You can prevent problems by making sure that the array you pass has an even number of elements.

    Another thing is that your routines are using floats, while Matlab's are using doubles, which have lots more precision.

    It's possible that there is something else causing problems that I didn't notice. Your code is a little hard to read, as it uses variables named X and x, plus a lot of others that have one-character names.
     
  7. Jan 17, 2014 #6
    ok I made it even and I change my code to use doubles and not float.
    how do I fix the numerous occurrences of N2/2?
    PHP:

               for (int k = 0; k < (N2 / 2); k++)
               {
                   temp4 = Complex1(k, N2);
                   X[k] = Y_Even[k] + (Y_Odd[k] * temp4);
                   X[k + (N2 / 2)] = Y_Even[k] - (Y_Odd[k] * temp4);  
                   }
     
    I


    understand what you are saying, just not how to fix it.


    [11,22,33,44,55,66,66,55,44,33,22,11, 0, 1, 2, 3, 3, 2, 1, 0, -1,-2, -3, -3, -2, -1, 0,1,1,0,-1,0,-1,0,-1,0];

    this is what my code has
    PHP:
     
    and this is what mat Lab has:
    PHP:

    Columns 1 through 3

       4.6100 + 0.0000i   2.3385 - 3.5323i  -0.9706 - 2.6230i

      Columns 4 through 6

      -1.6154 - 0.5994i  -0.3104 + 0.3972i   0.0090 + 0.0394i

      Columns 7 through 9

      -0.0450 - 0.0087i   0.1610 - 0.0710i   0.0440 - 0.2416i

      Columns 10 through 12

      -0.0900 - 0.1000i  -0.0283 + 0.0202i   0.0081 - 0.0335i

      Columns 13 through 15

       0.0050 - 0.0087i   0.0755 + 0.0111i   0.0689 - 0.0588i

      Columns 16 through 18

      -0.0046 - 0.0106i   0.0264 + 0.0367i   0.0178 - 0.0128i

      Columns 19 through 21

      -0.0300 + 0.0000i   0.0178 + 0.0128i   0.0264 - 0.0367i

      Columns 22 through 24

      -0.0046 + 0.0106i   0.0689 + 0.0588i   0.0755 - 0.0111i

      Columns 25 through 27

       0.0050 + 0.0087i   0.0081 + 0.0335i  -0.0283 - 0.0202i

      Columns 28 through 30

      -0.0900 + 0.1000i   0.0440 + 0.2416i   0.1610 + 0.0710i

      Columns 31 through 33

      -0.0450 + 0.0087i   0.0090 - 0.0394i  -0.3104 - 0.3972i

      Columns 34 through 36

      -1.6154 + 0.5994i  -0.9706 + 2.6230i   2.3385 + 3.5323i
     
     
  8. Jan 17, 2014 #7

    Mark44

    Staff: Mentor

    In addition to the above, your code should be cleaned up to make it more comprehensible. For example, you have at least two variables that aren't being used, Fs and R. You also reference a method (constructor?), Complex1, that you don't show, and I don't see the FFT1 method being called anywhere.
     
  9. Jan 17, 2014 #8

    Mark44

    Staff: Mentor

    Make sure that your example arrays have an even number of elements. See if that makes a difference.
     
  10. Jan 17, 2014 #9

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    In fact, make sure the length of your input array is a power of 2, not just an even number.

    Note, the Matlab FFT code will probably work correctly for any length of array, and work efficiently if the length can be factorized into small integers (e.g. 60 = 2 x 2 x 3 x 5), but your algorithm only works when the length is an exact power of 2.
     
  11. Jan 17, 2014 #10
    here the code:
    Code (Text):

    public class DSPclass
        {
           
           private double Fs;
           private int N;
           private Complex [] F;
           private int R;
           private Complex[] x;
           public DSPclass(double[] DSP1, int f1)
            {
               N = DSP1.Length;
               R = DSP1.Length;
               F = new Complex[N];
               Fs = (double)f1;
               x = new Complex[DSP1.Length];
           
              for (int v = 0; v < N; v++)
               {
                   x[v] = DSP1[v];        
              }
            }

           public void FFT1()
           {
              F = FFT(x);
            }

           public Complex[] FFT(Complex[] x)
           {
               int N2 = x.Length;
               Complex[] X = new Complex[N2];
               if (N2 == 1)
               {
                   return x;
               }
               Complex[] odd = new Complex[N2 / 2];
               Complex[] even = new Complex[N2 / 2];
               Complex[] Y_Odd = new Complex[N2 / 2];
               Complex[] Y_Even = new Complex[N2 / 2];


               for (int t = 0; t < N2 / 2; t++)
               {
                   even[t] = x[t * 2];    
                   odd[t] = x[(t * 2) + 1];
               }
               Y_Even = FFT(even);
               Y_Odd = FFT(odd);
               Complex temp4;

               for (int k = 0; k < (N2 / 2); k++)
               {
                   temp4 = Complex1(k, N2);
                   X[k] = Y_Even[k] + (Y_Odd[k] * temp4);
                   X[k + (N2 / 2)] = Y_Even[k] - (Y_Odd[k] * temp4);  
                   }

               return X;
           }
            public Complex Complex1(int K, int N3)
            {
                Complex W = Complex.Pow((Complex.Exp(-1 * Complex.ImaginaryOne * (2.0 * Math.PI / N3))), K);
                return W;

            }
           
        public int Apm(double[] img, double[] real)
            {
             
                for (int i = 0; i < R; i++)
                {
                img[i] = F[i].Imaginary;
                real[i] = F[i].Real;
                }

                return R;
            }


     

    I have a button set up my buffer and calls
    Code (Text):

             
                DSPclass ojd = new DSPclass(buffer, 100);
                ojd.FFT1();
               int temp3 = ojd.Apm(img, real);

                for (int r = 0; r < temp3; r++)
                {
                    richTextBox1.Text = richTextBox1.Text + " Real" +  "[ " + r +"] =" + real[r] + "\n";
                    richTextBox1.Text = richTextBox1.Text + " Img" + "[ " + r + "] =" + img[r] + "\n";
                 
                }

     
    then I print out my bins
     
  12. Jan 17, 2014 #11
    i just tried :
    here is what my code had:
    Code (Text):

     Real[ 0] =15.6438784003258
     Img[ 0] =0
     Real[ 1] =10.2983759109041
     Img[ 1] =6.05658364171625
     Real[ 2] =0
     Img[ 2] =0
     Real[ 3] =39.5433000439413
     Img[ 3] =6.96499187098068
     Real[ 4] =-30.9925575146078
     Img[ 4] =-30.1235064400932
     Real[ 5] =0
     Img[ 5] =0
     Real[ 6] =-9.95154977268631
     Img[ 6] =-3.69298083212547
     Real[ 7] =2.20759416859427
     Img[ 7] =1.6364292073018
     Real[ 8] =0
     Img[ 8] =0
     Real[ 9] =-4.18537793409244
     Img[ 9] =-2.16632836056903
     Real[ 10] =0.984062631336309
     Img[ 10] =0.92015116446298
     Real[ 11] =0
     Img[ 11] =0
     Real[ 12] =-1.37746832626377
     Img[ 12] =-1.0583850406474
     Real[ 13] =1.29803649847418
     Img[ 13] =1.5932800860084
     Real[ 14] =0
     Img[ 14] =0
     Real[ 15] =-0.99267691465516
     Img[ 15] =-1.01387078555409
     Real[ 16] =1.32590704041747
     Img[ 16] =2.41181584479389
     Real[ 17] =0
     Img[ 17] =0
     Real[ 18] =0.0957663301677241
     Img[ 18] =-0.256307027503596
     Real[ 19] =1.17968985041151
     Img[ 19] =2.79622165457918
     Real[ 20] =0
     Img[ 20] =0
     Real[ 21] =-1.25101015817972
     Img[ 21] =-1.51221152695445
     Real[ 22] =0.198413304660074
     Img[ 22] =0.517555189108845
     Real[ 23] =0
     Img[ 23] =0
     Real[ 24] =0
     Img[ 24] =0
     Real[ 25] =-0.127357929944991
     Img[ 25] =-0.38006493449211
     Real[ 26] =0.432648411359066
     Img[ 26] =1.30601072491968
     Real[ 27] =0
     Img[ 27] =0
     Real[ 28] =-0.145111851696628
     Img[ 28] =-0.474632632036994
     Real[ 29] =0.268022851654261
     Img[ 29] =1.69222987953967
     Real[ 30] =0
     Img[ 30] =0
     Real[ 31] =0.145239524019237
     Img[ 31] =-0.0488161335578057
     Real[ 32] =0.090915224139609
     Img[ 32] =1.65530281470198
     Real[ 33] =0
     Img[ 33] =0
     Real[ 34] =-0.125240060553626
     Img[ 34] =-0.572456104735653
     Real[ 35] =-0.0470832695668921
     Img[ 35] =0.91145842848119
     Real[ 36] =0
     Img[ 36] =0
     Real[ 37] =0.11269692795535
     Img[ 37] =-0.086577036472551
     Real[ 38] =-0.162121778644659
     Img[ 38] =1.2548487295934
     Real[ 39] =0
     Img[ 39] =0
     Real[ 40] =0.109882212075364
     Img[ 40] =-0.172262461711166
     Real[ 41] =-0.456958927964437
     Img[ 41] =1.57286268745902
     Real[ 42] =0
     Img[ 42] =0
     Real[ 43] =0.171800801222045
     Img[ 43] =0.322030781507433
     Real[ 44] =-0.808197480781608
     Img[ 44] =1.94875403070976
     Real[ 45] =0
     Img[ 45] =0
     Real[ 46] =0.0970680510168265
     Img[ 46] =-1.02687351123835
     Real[ 47] =-0.220540093017641
     Img[ 47] =0.441370280774122
     Real[ 48] =0
     Img[ 48] =0
     Real[ 49] =0
     Img[ 49] =0
     Real[ 50] =0.124337077140807
     Img[ 50] =0
     Real[ 51] =-0.548705747310702
     Img[ 51] =1.06601119759587
     Real[ 52] =0
     Img[ 52] =0
     Real[ 53] =0.14632713828345
     Img[ 53] =-0.0894459071096332
     Real[ 54] =-0.894940646983329
     Img[ 54] =1.23178002539574
     Real[ 55] =0
     Img[ 55] =0
     Real[ 56] =0.0611496063425658
     Img[ 56] =0.271327637928145
     Real[ 57] =-1.10866863770287
     Img[ 57] =1.22456507700049
     Real[ 58] =0
     Img[ 58] =0
     Real[ 59] =0.222635256999182
     Img[ 59] =-0.292108065498561
     Real[ 60] =-0.756648423770216
     Img[ 60] =0.66846475324246
     Real[ 61] =0
     Img[ 61] =0
     Real[ 62] =0.044665149571137
     Img[ 62] =0.219870043071908
     Real[ 63] =-1.1812933063625
     Img[ 63] =0.899064577770995
     Real[ 64] =0
     Img[ 64] =0
     Real[ 65] =0.0741861147176598
     Img[ 65] =0.171632161452021
     Real[ 66] =-1.75228658636836
     Img[ 66] =0.963327979778321
     Real[ 67] =0
     Img[ 67] =0
     Real[ 68] =-0.301620261700824
     Img[ 68] =0.626212335587953
     Real[ 69] =-2.65927275157048
     Img[ 69] =1.08986555955693
     Real[ 70] =0
     Img[ 70] =0
     Real[ 71] =0.894319310624528
     Img[ 71] =-0.739845250993178
     Real[ 72] =-0.767312790410855
     Img[ 72] =0.238597430946073
     Real[ 73] =0
     Img[ 73] =0
     Real[ 74] =0
     Img[ 74] =0
     Real[ 75] =-0.127357929944991
     Img[ 75] =0.38006493449211
     Real[ 76] =-2.01871076621567
     Img[ 76] =0.630514981223546
     Real[ 77] =0
     Img[ 77] =0
     Real[ 78] =-0.113537195681781
     Img[ 78] =0.356773368734805
     Real[ 79] =-2.86140433259531
     Img[ 79] =0.453201859551713
     Real[ 80] =0
     Img[ 80] =0
     Real[ 81] =-0.7731523277941
     Img[ 81] =0.727806191014623
     Real[ 82] =-3.80065814836602
     Img[ 82] =0.166456472803514
     Real[ 83] =0
     Img[ 83] =0
     Real[ 84] =0.166319636812731
     Img[ 84] =0.144341843142572
     Real[ 85] =-2.92731873985638
     Img[ 85] =-0.219216121035203
     Real[ 86] =0
     Img[ 86] =0
     Real[ 87] =-1.5510098887673
     Img[ 87] =0.925092034048044
     Real[ 88] =-5.46265859833295
     Img[ 88] =-0.798576636330173
     Real[ 89] =0
     Img[ 89] =0
     Real[ 90] =-2.86950327882629
     Img[ 90] =1.17353477556421
     Real[ 91] =-9.93066537316123
     Img[ 91] =-2.88512440947682
     Real[ 92] =0
     Img[ 92] =0
     Real[ 93] =-10.8293673170813
     Img[ 93] =2.05072704714871
     Real[ 94] =-33.8103170983207
     Img[ 94] =-14.5229924705155
     Real[ 95] =0
     Img[ 95] =0
     Real[ 96] =39.9341521655305
     Img[ 96] =-0.751239413473173
     Real[ 97] =22.0182725210828
     Img[ 97] =13.2026917984351
     Real[ 98] =0
     Img[ 98] =0
     Real[ 99] =0
     Img[ 99] =0

     
    and here is what math lab had:
    Code (Text):

    yft =

       1.0e+02 *

      Columns 1 through 3

       0.0000 + 0.0000i  -0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 4 through 6

       0.0000 + 0.0000i  -0.0000 - 1.0000i   0.0000 - 0.0000i

      Columns 7 through 9

       0.0000 - 0.0000i   0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 10 through 12

       0.0000 + 0.0000i  -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 13 through 15

       0.0000 - 0.0000i  -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 16 through 18

       0.0000 + 0.0000i  -0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 19 through 21

      -0.0000 + 0.0000i   0.0000 - 0.0000i  -0.0000 + 0.0000i

      Columns 22 through 24

      -0.0000 + 0.0000i   0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 25 through 27

      -0.0000 - 0.0000i   0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 28 through 30

      -0.0000 - 0.0000i   0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 31 through 33

      -0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 34 through 36

      -0.0000 - 0.0000i   0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 37 through 39

       0.0000 + 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 40 through 42

       0.0000 + 0.0000i   0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 43 through 45

       0.0000 - 0.0000i   0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 46 through 48

      -0.0000 + 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 49 through 51

      -0.0000 - 0.0000i   0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 52 through 54

       0.0000 + 0.0000i  -0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 55 through 57

      -0.0000 + 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 58 through 60

       0.0000 - 0.0000i   0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 61 through 63

       0.0000 - 0.0000i   0.0000 - 0.0000i  -0.0000 + 0.0000i

      Columns 64 through 66

      -0.0000 + 0.0000i   0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 67 through 69

       0.0000 + 0.0000i  -0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 70 through 72

       0.0000 - 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 73 through 75

       0.0000 - 0.0000i  -0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 76 through 78

       0.0000 - 0.0000i  -0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 79 through 81

       0.0000 + 0.0000i  -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 82 through 84

       0.0000 + 0.0000i  -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 85 through 87

      -0.0000 + 0.0000i   0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 88 through 90

      -0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 91 through 93

      -0.0000 + 0.0000i   0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 94 through 96

       0.0000 - 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 97 through 99

      -0.0000 + 1.0000i   0.0000 - 0.0000i  -0.0000 + 0.0000i

      Column 100

      -0.0000 - 0.0000i

     
    and I used 100 samples
    and I made sure that mat lab and my code had the same input samples
     
  13. Jan 17, 2014 #12
    ok update:

    I did
    for (int y = 0; y < 1024; y++)
    {
    double temp8 = 2D * (Math.Sin(2D * Math.PI * 4D * (double)y / 1024));
    buffer[y] = (float)temp8;
    }

    in both mathlab and my code
    I got a lot on 0s in both but here are the non 0
    math lab:

    PHP:

      Columns 4 through 6

       0.0000 + 0.0000i  -0.0000 - 1.0240i  -0.0000 - 0.0000i

    Columns 1021 through 1023

      -0.0000 + 1.0240i   0.0000 - 0.0000i   0.0000 - 0.0000i

     
    my code:
    Code (Text):

    Real[ 1020] =-2.97383645486704E-13
     Img[ 1020] =1023.99999791559
    Real[ 4] =-4.31816041257527E-14
     Img[ 4] =-1023.99999791562
     
    all the other ones are 0 in both my code and mat lab
    Mark44
    could you give me a better test to do ?
    I think my input values are not good for testing.
     
  14. Jan 17, 2014 #13
    ok I just tried
    for (int y = 0; y < 1024; y++)
    {
    double temp8 = 10D * (Math.Sin(2D * Math.PI * 500D * (double)y / 1024));
    buffer[y] = (float)temp8;
    }

    in both math lab
    and my code

    I got a lot of 0s in both
    but i got

    this in math lab :
    Columns 499 through 501

    0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 - 5.1200i

    Columns 523 through 525

    0.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 5.1200i

    and this is my code
    Real[ 500] =-9.0169911468041E-12
    Img[ 500] =-5119.99998089389
    Real[ 524] =-9.63785475038836E-12
    Img[ 524] =5119.99998089403


    it seem MatLab is moving there decimal point but beside that I get the some as them..

    why do I only get the same if I do small sample sizes or 2^number?

    I do know fft only works for 2^number, but why it is working for mat lab ?
     
  15. Jan 17, 2014 #14
    Good new my code seem to be working and I am getting the same values as matlab!!
    i just did

    for (int y = 0; y < 1024; y++)
    {
    double temp8 = 20D * (Math.Sin(2D * Math.PI * 100D *(double)y / 1024)) + 10D*(Math.Sin(2D * Math.PI * 500D * (double)y / 1024));
    buffer[y] = temp8;
    }


    and I got the
    100
    500
    524
    924

    and math lab
    got

    ok question
    I am going to use this class to do a fft on a realtime mic

    at 44100Hz sampling rate
    and I am going to since the fft
    should that be good ?
    and 8192 sampler at a time
    is that good ?
     
  16. Jan 17, 2014 #15
    also when I run my fft on input from a mic
    should I use the Magnitude or the Real or the Imaginary to get the frequencies?
    right now I can use both Magnitude and Imaginary because Real seems to all way be 0.
    I think that is because i am using a Sin for my input
    but I am not sure
     
  17. Jan 17, 2014 #16

    Mark44

    Staff: Mentor

    You're losing a lot of precision by casting temp8 to a float in the second line in your loop. Why don't you declare your buffer array to be of type double?
    These don't match as far as I can tell. For element 500, matlab got 0 + 0i. In your code Real[500] is close to zero, but Imag[500] is almost 5120.

    Got what?
    You're going to do what?
     
  18. Jan 18, 2014 #17
    Mark44
    sorry I meant mat lab got the same values and I got

    oh and my code does real[0]... and img[0]... so my bin 500 is really bin 501.
    so mat lab got bin 501 = -0.0000 - 5.1200i
    and I got bin 501 = -9.0169911468041E-12 , -5119.99998089389i
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: FFT code help please
Loading...