Help with FFT Code Troubleshooting in Matlab

  • MATLAB
  • Thread starter btb4198
  • Start date
  • Tags
    Code Fft
In summary: FFT1() { F = FFT(x); } public Complex[] FFT(Complex[] x) { int N2 = x.Length; Complex[] X = new Complex[N2]; if (N2 == 1)
  • #1
btb4198
572
10
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:
       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;
       }
 
Physics news on Phys.org
  • #2
btb4198 said:
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:
       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;
       }
Start with a very small array of numbers and compare the results that your code gives with what Matlab gives.
 
  • #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
 
  • #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:
 Real[ 0] =463
 Img[ 0] =0
 Real[ 1] =189.806028517011
 Img[ 1] =-363.373692360352
 Real[ 2] =-119.592114050925
 Img[ 2] =-213.870851532612
 Real[ 3] =-129.238549778491
 Img[ 3] =6.13776532946331
 Real[ 4] =-7.10143747633832
 Img[ 4] =13.0767560210555
 Real[ 5] =2.07896892406892
 Img[ 5] =-2.82704714513847
 Real[ 6] =26.8328407119951
 Img[ 6] =-0.587063423354897
 Real[ 7] =21.7618899670057
 Img[ 7] =-44.0292027147991
 Real[ 8] =-7.65766734182345
 Img[ 8] =-9.90949761767934
 Real[ 9] =-2.42695679736481
 Img[ 9] =2.39648218385744
 Real[ 10] =-0.501396750545102
 Img[ 10] =0.738119463035542
 Real[ 11] =4.29733295744174
 Img[ 11] =5.0700269535738
 Real[ 12] =7.57806318083925
 Img[ 12] =-0.926256373945376
 Real[ 13] =-3.3556513475385
 Img[ 13] =8.6219518021768
 Real[ 14] =21.6574627083539
 Img[ 14] =20.7737631979399
 Real[ 15] =52.2643940557698
 Img[ 15] =-13.9875687777826
 Real[ 16] =0
 Img[ 16] =0
 Real[ 17] =-1
 Img[ 17] =0
 Real[ 18] =0.840069369292564
 Img[ 18] =0.778621819483817
 Real[ 19] =-2.20993174795267
 Img[ 19] =0.0650503497332267
 Real[ 20] =-0.210101572499369
 Img[ 20] =8.56877776421676
 Real[ 21] =7.62514372492549
 Img[ 21] =2.8549907996651
 Real[ 22] =-3.04438486295254
 Img[ 22] =1.00280717411786
 Real[ 23] =1.65806419765245
 Img[ 23] =12.7079550576014
 Real[ 24] =17.8428540012147
 Img[ 24] =7.00158144758914
 Real[ 25] =-10.3423326581766
 Img[ 25] =9.90949761767934
 Real[ 26] =5.0012050215938
 Img[ 26] =20.63509649597
 Real[ 27] =10.3034425494224
 Img[ 27] =1.06768171984357
 Real[ 28] =-4.06902771698462
 Img[ 28] =8.65993809170566
 Real[ 29] =-4.10176942942642
 Img[ 29] =-15.0054904467752
 Real[ 30] =-124.899278824111
 Img[ 30] =-35.2342199701158
 Real[ 31] =-162.148367618001
 Img[ 31] =179.105345167814
 Real[ 32] =101.351208086543
 Img[ 32] =390.578681906033
 Real[ 33] =0
 Img[ 33] =0
 Real[ 34] =0
 Img[ 34] =0
 Real[ 0] =463
 Img[ 0] =0
 Real[ 1] =189.806028517011
 Img[ 1] =-363.373692360352
 Real[ 2] =-119.592114050925
 Img[ 2] =-213.870851532612
 Real[ 3] =-129.238549778491
 Img[ 3] =6.13776532946331
 Real[ 4] =-7.10143747633832
 Img[ 4] =13.0767560210555
 Real[ 5] =2.07896892406892
 Img[ 5] =-2.82704714513847
 Real[ 6] =26.8328407119951
 Img[ 6] =-0.587063423354897
 Real[ 7] =21.7618899670057
 Img[ 7] =-44.0292027147991
 Real[ 8] =-7.65766734182345
 Img[ 8] =-9.90949761767934
 Real[ 9] =-2.42695679736481
 Img[ 9] =2.39648218385744
 Real[ 10] =-0.501396750545102
 Img[ 10] =0.738119463035542
 Real[ 11] =4.29733295744174
 Img[ 11] =5.0700269535738
 Real[ 12] =7.57806318083925
 Img[ 12] =-0.926256373945376
 Real[ 13] =-3.3556513475385
 Img[ 13] =8.6219518021768
 Real[ 14] =21.6574627083539
 Img[ 14] =20.7737631979399
 Real[ 15] =52.2643940557698
 Img[ 15] =-13.9875687777826
 Real[ 16] =0
 Img[ 16] =0
 Real[ 17] =-1
 Img[ 17] =0
 Real[ 18] =0.840069369292564
 Img[ 18] =0.778621819483817
 Real[ 19] =-2.20993174795267
 Img[ 19] =0.0650503497332267
 Real[ 20] =-0.210101572499369
 Img[ 20] =8.56877776421676
 Real[ 21] =7.62514372492549
 Img[ 21] =2.8549907996651
 Real[ 22] =-3.04438486295254
 Img[ 22] =1.00280717411786
 Real[ 23] =1.65806419765245
 Img[ 23] =12.7079550576014
 Real[ 24] =17.8428540012147
 Img[ 24] =7.00158144758914
 Real[ 25] =-10.3423326581766
 Img[ 25] =9.90949761767934
 Real[ 26] =5.0012050215938
 Img[ 26] =20.63509649597
 Real[ 27] =10.3034425494224
 Img[ 27] =1.06768171984357
 Real[ 28] =-4.06902771698462
 Img[ 28] =8.65993809170566
 Real[ 29] =-4.10176942942642
 Img[ 29] =-15.0054904467752
 Real[ 30] =-124.899278824111
 Img[ 30] =-35.2342199701158
 Real[ 31] =-162.148367618001
 Img[ 31] =179.105345167814
 Real[ 32] =101.351208086543
 Img[ 32] =390.578681906033
 Real[ 33] =0
 Img[ 33] =0
 Real[ 34] =0
 Img[ 34] =0

I do not know why
can you help ?
 
  • #5
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.
 
  • #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:
Real[ 0] =463
 Img[ 0] =0
 Real[ 1] =212.847759177161
 Img[ 1] =-349.9824201751
 Real[ 2] =-80.5976671383587
 Img[ 2] =-232.117970349427
 Real[ 3] =-120.612159321677
 Img[ 3] =-54.7294734194975
 Real[ 4] =-7.78358540940624
 Img[ 4] =12.7975251074921
 Real[ 5] =-6.67533076881305
 Img[ 5] =8.04925413302722
 Real[ 6] =35.8271496945542
 Img[ 6] =19.7654999209716
 Real[ 7] =55.5260143248544
 Img[ 7] =-47.1023151916819
 Real[ 8] =0
 Img[ 8] =0
 Real[ 9] =-9
 Img[ 9] =-9.99999999999999
 Real[ 10] =-6.40811850040841
 Img[ 10] =5.03115158742256
 Real[ 11] =2.91349860149827
 Img[ 11] =6.13341388770061
 Real[ 12] =15.2583302491977
 Img[ 12] =0.0551362713291255
 Real[ 13] =9.04029931229872
 Img[ 13] =-1.48406203602687
 Real[ 14] =-5.91409764285586
 Img[ 14] =0.551984395537549
 Real[ 15] =6.2151066330519
 Img[ 15] =16.0403254304358
 Real[ 16] =32.5718670306463
 Img[ 16] =1.08527575361319
 Real[ 17] =0
 Img[ 17] =0
 Real[ 18] =-1
 Img[ 18] =0
 Real[ 19] =-0.869917327200227
 Img[ 19] =1.77502656676992
 Real[ 20] =-0.610559394091162
 Img[ 20] =4.91429582838553
 Real[ 21] =10.9852262806237
 Img[ 21] =11.3090023534363
 Real[ 22] =7.00521456673536
 Img[ 22] =2.95939894070325
 Real[ 23] =-9.28138777456806
 Img[ 23] =-0.867883631780604
 Real[ 24] =-5.71197154979619
 Img[ 24] =21.429014435113
 Real[ 25] =27.3341419854172
 Img[ 25] =25.3912170486824
 Real[ 26] =0
 Img[ 26] =0
 Real[ 27] =-9
 Img[ 27] =9.99999999999999
 Real[ 28] =0.595382347844904
 Img[ 28] =18.2559952235983
 Real[ 29] =3.10814159634083
 Img[ 29] =12.2910169947527
 Real[ 30] =10.9045011767181
 Img[ 30] =35.3756443470179
 Real[ 31] =-4.26192846962784
 Img[ 31] =-14.2728620121685
 Real[ 32] =-120.29428951116
 Img[ 32] =-50.8131080994753
 Real[ 33] =-185.143698443199
 Img[ 33] =151.544403852068
 Real[ 34] =32.0320782742197
 Img[ 34] =396.615512837101
 Real[ 35] =0
 Img[ 35] =0
 Real[ 0] =463
 Img[ 0] =0
 Real[ 1] =212.847759177161
 Img[ 1] =-349.9824201751
 Real[ 2] =-80.5976671383587
 Img[ 2] =-232.117970349427
 Real[ 3] =-120.612159321677
 Img[ 3] =-54.7294734194975
 Real[ 4] =-7.78358540940624
 Img[ 4] =12.7975251074921
 Real[ 5] =-6.67533076881305
 Img[ 5] =8.04925413302722
 Real[ 6] =35.8271496945542
 Img[ 6] =19.7654999209716
 Real[ 7] =55.5260143248544
 Img[ 7] =-47.1023151916819
 Real[ 8] =0
 Img[ 8] =0
 Real[ 9] =-9
 Img[ 9] =-9.99999999999999
 Real[ 10] =-6.40811850040841
 Img[ 10] =5.03115158742256
 Real[ 11] =2.91349860149827
 Img[ 11] =6.13341388770061
 Real[ 12] =15.2583302491977
 Img[ 12] =0.0551362713291255
 Real[ 13] =9.04029931229872
 Img[ 13] =-1.48406203602687
 Real[ 14] =-5.91409764285586
 Img[ 14] =0.551984395537549
 Real[ 15] =6.2151066330519
 Img[ 15] =16.0403254304358
 Real[ 16] =32.5718670306463
 Img[ 16] =1.08527575361319
 Real[ 17] =0
 Img[ 17] =0
 Real[ 18] =-1
 Img[ 18] =0
 Real[ 19] =-0.869917327200227
 Img[ 19] =1.77502656676992
 Real[ 20] =-0.610559394091162
 Img[ 20] =4.91429582838553
 Real[ 21] =10.9852262806237
 Img[ 21] =11.3090023534363
 Real[ 22] =7.00521456673536
 Img[ 22] =2.95939894070325
 Real[ 23] =-9.28138777456806
 Img[ 23] =-0.867883631780604
 Real[ 24] =-5.71197154979619
 Img[ 24] =21.429014435113
 Real[ 25] =27.3341419854172
 Img[ 25] =25.3912170486824
 Real[ 26] =0
 Img[ 26] =0
 Real[ 27] =-9
 Img[ 27] =9.99999999999999
 Real[ 28] =0.595382347844904
 Img[ 28] =18.2559952235983
 Real[ 29] =3.10814159634083
 Img[ 29] =12.2910169947527
 Real[ 30] =10.9045011767181
 Img[ 30] =35.3756443470179
 Real[ 31] =-4.26192846962784
 Img[ 31] =-14.2728620121685
 Real[ 32] =-120.29428951116
 Img[ 32] =-50.8131080994753
 Real[ 33] =-185.143698443199
 Img[ 33] =151.544403852068
 Real[ 34] =32.0320782742197
 Img[ 34] =396.615512837101
 Real[ 35] =0
 Img[ 35] =0

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
 
  • #7
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.
 
  • #8
btb4198 said:
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?
Make sure that your example arrays have an even number of elements. See if that makes a difference.
 
  • #9
Mark44 said:
Make sure that your example arrays have an even number of elements. See if that makes a difference.

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.
 
  • #10
here the code:
Code:
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:
            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
 
  • #11
i just tried :
for (int y = 0; y < 100; y++)
{
double temp8 = 2D * (Math.Sin(2D * Math.PI * 4D * (double)y / 100));
buffer[y] = (float)temp8;
}

here is what my code had:
Code:
 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:
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
 
  • #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:
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.
 
  • #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 ?
 
  • #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 ?
 
  • #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
 
  • #16
btb4198 said:
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
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?
btb4198 said:
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
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.

btb4198 said:
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 ?

btb4198 said:
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
Got what?
btb4198 said:
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
You're going to do what?
btb4198 said:
should that be good ?
and 8192 sampler at a time
is that good ?

btb4198 said:
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
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
 

1. How do I determine if my FFT code is working correctly?

The simplest way to check the functionality of your FFT code is to compare the results with a known solution. You can do this by using a test signal with a known FFT output, such as a sinusoidal signal with a single frequency. If the output of your code matches the known solution, it is likely working correctly. Another way to test your code is to use the built-in FFT functions in Matlab and compare the results.

2. Why am I getting incorrect results when using my FFT code?

There are several potential reasons for incorrect results when using FFT code. It could be due to errors in the code itself, such as using incorrect parameters or not properly handling the data. It could also be due to issues with the input data, such as incorrect formatting or missing values. Additionally, the FFT algorithm can be sensitive to certain types of signals, so it is important to ensure that your input signal is properly pre-processed before applying the FFT.

3. How do I troubleshoot errors in my FFT code?

If you encounter errors while running your FFT code, the first step is to carefully review your code for any syntax or logical errors. You can also try using the debugging tools in Matlab to step through your code and identify any issues. Another helpful technique is to print out intermediate steps and results to better understand where the error may be occurring.

4. Can I optimize my FFT code for faster computation?

Yes, there are several ways to optimize your FFT code for faster computation. One approach is to use built-in functions or libraries, which are often optimized for speed. You can also use techniques such as vectorization and pre-allocation of variables to improve performance. Additionally, choosing the appropriate FFT algorithm for your specific application can also lead to faster computation.

5. Are there any common mistakes to avoid when writing FFT code in Matlab?

Some common mistakes to avoid when writing FFT code in Matlab include not properly understanding the input data format, using incorrect parameters, and using the wrong FFT algorithm for your specific application. It is also important to carefully test and validate your code before using it for any critical applications. Finally, it is helpful to familiarize yourself with the built-in FFT functions in Matlab and use them whenever possible for more efficient and accurate results.

Similar threads

  • Electrical Engineering
3
Replies
85
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
898
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top