Troubleshooting FFT code with MATLAB

  1. ok I wrote my own FFT class and I now have MatLab on my computer. I am comparing my code to mat-lab.

    so my test i used it this :

    Code (Text):
     for (int y = 0; y < 100; y++)
                {
                    double temp8 = (2D * (Math.Sin(2D * Math.PI * 4D * (double)y / 100D)));
                    buffer[y] = (float)temp8;      
                }
     
    which in MatLab is this:
    Code (Text):

    fo = 4;
     Fs = 100;
    Ts = 1/Fs;
     0:Ts;1-Ts;
    t = 0:Ts:1-Ts;
    y = 2 * sin(2*pi*fo*t);
     
    so that is working great, I am getting the same values in MatLab as in my program.

    now the problem is the fft itself:

    my code gets:

    Code (Text):

    Real[ 0] =15.6438784003258
     Img[ 0] =0
     Real[ 1] =8.46128487984206
     Img[ 1] =1.40568933318413
     Real[ 2] =0
     Img[ 2] =0
     Real[ 3] =22.2310419428763
     Img[ 3] =5.40923846876197
     Real[ 4] =-1.80056660225914
     Img[ 4] =5.74956074821605
     Real[ 5] =0
     Img[ 5] =0
     Real[ 6] =-5.11379679263678
     Img[ 6] =-0.688387862482321
     Real[ 7] =-10.4312474863214
     Img[ 7] =-8.80658407162755
     Real[ 8] =0
     Img[ 8] =0
     Real[ 9] =7.79337236310158
     Img[ 9] =9.18587953709857
     Real[ 10] =-3.99240649537681
     Img[ 10] =-2.95715001320984
     Real[ 11] =0
     Img[ 11] =0
     Real[ 12] =-0.725325897426671
     Img[ 12] =0.363923889449184
     Real[ 13] =-3.48947495495046
     Img[ 13] =-3.17979218818627
     Real[ 14] =0
     Img[ 14] =0
     Real[ 15] =1.91405696078796
     Img[ 15] =1.41155752908299
     Real[ 16] =-4.65431128431017
     Img[ 16] =-2.55948892517805
     Real[ 17] =0
     Img[ 17] =0
     Real[ 18] =-2.66530083281111
     Img[ 18] =-3.2978120014703
     Real[ 19] =-2.18544418167612
     Img[ 19] =-3.16506874433514
     Real[ 20] =0
     Img[ 20] =0
     Real[ 21] =2.5183300759435
     Img[ 21] =3.27070622595888
     Real[ 22] =-1.91673308587846
     Img[ 22] =-4.28146090355363
     Real[ 23] =0
     Img[ 23] =0
     Real[ 24] =0
     Img[ 24] =0
     Real[ 25] =-0.127357929944991
     Img[ 25] =0.380064934492111
     Real[ 26] =-1.77076956563049
     Img[ 26] =-1.16238391836151
     Real[ 27] =0
     Img[ 27] =0
     Real[ 28] =1.33827428539545
     Img[ 28] =1.82705801865022
     Real[ 29] =-2.74387526172086
     Img[ 29] =1.6918766374459
     Real[ 30] =0
     Img[ 30] =0
     Real[ 31] =-1.23531162328276
     Img[ 31] =-0.344879676029618
     Real[ 32] =-0.490950246033762
     Img[ 32] =-2.50634463327099
     Real[ 33] =0
     Img[ 33] =0
     Real[ 34] =0.0616589288850338
     Img[ 34] =1.41281177829685
     Real[ 35] =-1.11021525078896
     Img[ 35] =-1.6384014200336
     Real[ 36] =0
     Img[ 36] =0
     Real[ 37] =-0.581764223242864
     Img[ 37] =-0.565565392364546
     Real[ 38] =-0.800557052119878
     Img[ 38] =0.24862692145541
     Real[ 39] =0
     Img[ 39] =0
     Real[ 40] =-0.155778218052648
     Img[ 40] =-4.08014192142325
     Real[ 41] =-1.59543284819159
     Img[ 41] =3.38035787631867
     Real[ 42] =0
     Img[ 42] =0
     Real[ 43] =-1.42509628490074
     Img[ 43] =-0.690852268756354
     Real[ 44] =-1.01489289175587
     Img[ 44] =1.30057672045934
     Real[ 45] =0
     Img[ 45] =0
     Real[ 46] =1.74167365638883
     Img[ 46] =-1.85709851794845
     Real[ 47] =-1.54097745212813
     Img[ 47] =-5.30373068481949
     Real[ 48] =0
     Img[ 48] =0
     Real[ 49] =0
     Img[ 49] =0
     Real[ 50] =0.124337077140807
     Img[ 50] =0
     Real[ 51] =-1.70945722097089
     Img[ 51] =0.765800478188799
     Real[ 52] =0
     Img[ 52] =0
     Real[ 53] =1.85429156562877
     Img[ 53] =1.52216544624097
     Real[ 54] =-3.37426422070059
     Img[ 54] =5.34550387538567
     Real[ 55] =0
     Img[ 55] =0
     Real[ 56] =-1.58324273924713
     Img[ 56] =0.709457373828966
     Real[ 57] =-0.509551690154068
     Img[ 57] =-4.13778842089745
     Real[ 58] =0
     Img[ 58] =0
     Real[ 59] =-0.111356595280117
     Img[ 59] =4.16938599825349
     Real[ 60] =-1.27006205100977
     Img[ 60] =-0.979250998498057
     Real[ 61] =0
     Img[ 61] =0
     Real[ 62] =-0.607477279265961
     Img[ 62] =0.474591108126309
     Real[ 63] =-0.789693160521237
     Img[ 63] =-0.304816584641161
     Real[ 64] =0
     Img[ 64] =0
     Real[ 65] =0.000684665428978715
     Img[ 65] =-1.22197350537954
     Real[ 66] =-1.43379200328076
     Img[ 66] =2.51523743989183
     Real[ 67] =0
     Img[ 67] =0
     Real[ 68] =-1.13493927974621
     Img[ 68] =-0.045628157166532
     Real[ 69] =-0.833622074048892
     Img[ 69] =0.249244154952255
     Real[ 70] =0
     Img[ 70] =0
     Real[ 71] =1.28236043172393
     Img[ 71] =-1.54307771477549
     Real[ 72] =-1.51791564293372
     Img[ 72] =-2.19078665228718
     Real[ 73] =0
     Img[ 73] =0
     Real[ 74] =0
     Img[ 74] =0
     Real[ 75] =-0.127357929944991
     Img[ 75] =-0.380064934492111
     Real[ 76] =-1.97025325016812
     Img[ 76] =2.00831845114145
     Real[ 77] =0
     Img[ 77] =0
     Real[ 78] =2.26040036086525
     Img[ 78] =-3.00689615389389
     Real[ 79] =-4.44019352391471
     Img[ 79] =8.29860006881245
     Real[ 80] =0
     Img[ 80] =0
     Real[ 81] =-2.58596181495194
     Img[ 81] =3.06647330142348
     Real[ 82] =-1.72270785043888
     Img[ 82] =0.11127351919887
     Real[ 83] =0
     Img[ 83] =0
     Real[ 84] =1.55682887842896
     Img[ 84] =-0.943199069986429
     Real[ 85] =-3.06412282603289
     Img[ 85] =1.05092164080382
     Real[ 86] =0
     Img[ 86] =0
     Real[ 87] =-0.856548737569082
     Img[ 87] =-0.272949605210947
     Real[ 88] =-3.91301515540254
     Img[ 88] =3.17141962717599
     Real[ 89] =0
     Img[ 89] =0
     Real[ 90] =6.30989470522789
     Img[ 90] =-8.7777282723593
     Real[ 91] =-10.2918483227761
     Img[ 91] =8.89927095595156
     Real[ 92] =0
     Img[ 92] =0
     Real[ 93] =-5.63808404993432
     Img[ 93] =1.29162929065268
     Real[ 94] =-12.8829923493748
     Img[ 94] =5.99948970692771
     Real[ 95] =0
     Img[ 95] =0
     Real[ 96] =20.9099985289662
     Img[ 96] =-6.77868784657761
     Real[ 97] =17.9336785221511
     Img[ 97] =-9.01871999661001
     Real[ 98] =0
     Img[ 98] =0
     Real[ 99] =0
     Img[ 99] =0
     
    and this is what Mat Lab has

    Code (Text):

    YfreqDomain =

       1.0e+02 *

      Columns 1 through 2

       0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 3 through 4

      -0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 5 through 6

      -0.0000 - 1.0000i   0.0000 - 0.0000i

      Columns 7 through 8

       0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 9 through 10

       0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 11 through 12

      -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 13 through 14

       0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 15 through 16

       0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 17 through 18

      -0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 19 through 20

      -0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 21 through 22

      -0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 23 through 24

       0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 25 through 26

      -0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 27 through 28

       0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 29 through 30

       0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 31 through 32

      -0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 33 through 34

      -0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 35 through 36

       0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 37 through 38

       0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 39 through 40

      -0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 41 through 42

       0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 43 through 44

       0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 45 through 46

      -0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 47 through 48

      -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 49 through 50

      -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 51 through 52

       0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 53 through 54

      -0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 55 through 56

      -0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 57 through 58

      -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 59 through 60

       0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 61 through 62

       0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 63 through 64

      -0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 65 through 66

       0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 67 through 68

       0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 69 through 70

      -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 71 through 72

      -0.0000 - 0.0000i  -0.0000 - 0.0000i

      Columns 73 through 74

       0.0000 - 0.0000i  -0.0000 + 0.0000i

      Columns 75 through 76

       0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 77 through 78

      -0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 79 through 80

       0.0000 + 0.0000i  -0.0000 - 0.0000i

      Columns 81 through 82

      -0.0000 - 0.0000i   0.0000 + 0.0000i

      Columns 83 through 84

      -0.0000 - 0.0000i   0.0000 - 0.0000i

      Columns 85 through 86

      -0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 87 through 88

       0.0000 + 0.0000i  -0.0000 + 0.0000i

      Columns 89 through 90

       0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 91 through 92

      -0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 93 through 94

       0.0000 + 0.0000i   0.0000 - 0.0000i

      Columns 95 through 96

       0.0000 + 0.0000i   0.0000 + 0.0000i

      Columns 97 through 98

      -0.0000 + 1.0000i   0.0000 - 0.0000i

      Columns 99 through 100

      -0.0000 + 0.0000i  -0.0000 - 0.0000i
     
    so how should I go about troubleshooting my c# fft code?

    is there a way to get Matlab to print out the steps it is during where it does the FFT?

    is there a better way of troubleshooting my ftt code ?
     
  2. jcsd
  3. DrClaude

    DrClaude 2,283
    Science Advisor
    Homework Helper
    Gold Member

    Start with very short sequences, such as y[0]=1, y[1]=0 or y[0]=i, y[1]=0, and build up from there.

    No. Even if it were possible, I don't see how you could get useful information from that.
     
  4. As DrClaude said, test with an impulse and then figure out why you get the wrong answer. With a short impulse you can go step by step by hand. Do you have a standalone c program that you started from that works? Can you compare intermediate results with that?
     
  5. but I do not know what the outcome of that sequence should be...
     
  6. DrClaude

    DrClaude 2,283
    Science Advisor
    Homework Helper
    Gold Member

    You can calculate it by hand or use Matlab.
     
  7. ok i did

    y = 1,0,i,0

    math lab got :
    Code (Text):
    YfreqDomain =

      Columns 1 through 3

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

      Column 4

       1.0000 - 1.0000i
     
    by hand I got the same as math lab
    and my code got:
    Code (Text):

     Real[ 0] =1
     Img[ 0] =0
     Real[ 1] =1
     Img[ 1] =0
     Real[ 2] =0
     Img[ 2] =0
     Real[ 3] =1
     Img[ 3] =0
     
    i walk in the code and I think the problem is here
    Code (Text):

                Y_Even = FFT(even);
               Y_Odd = FFT(odd);
               polar1 temp4;
               for (int k = 0; k < (N2 / 2); k++)
               {
                   temp4 = Complex1(k, N2);
                   X[k].real = Y_Even[k].real + (Y_Odd[k].real * temp4.real);
                   X[k + (N2 / 2)].real = Y_Even[k].real - (Y_Odd[k].real * temp4.real);  
                   X[k].img = Y_Even[k].img + (Y_Odd[k].real * temp4.img);
                    X[k + (N2 / 2)].img = Y_Even[k].img - (Y_Odd[k].real * temp4.img);
                   }
    return x;

     
    I am not sure how to fix it.
    that is after I get a0 and a2 alone and add then and subject them .
    the 1st time we get to that code it should be
    x(0) = a0 + a2 = 1 + i
    x(1)= a0 - a2 = 1 - i
    right?
    but i am getting
    x(0) = 1
    x(0) = 1
    I think it is because the temp4 is wrong..
    the 1st Complex1 is called is returns 1

    but I do not know how to fix it
    here is the function
    Code (Text):

      public polar1 Complex1(int K, int N3)
            {
               polar1 temp;
                double temp1;
                temp1 = (2D * Math.PI *K) / N3;
                        temp.real =  Math.Cos(temp1);
                        temp.img =  Math.Sin(temp1);
                return temp;

            }
     
    but I am not sure
    can you help
    I know it is something wrong with that part of the code
     
  8. in fft

    does WKN = (e-i (2π/N))K

    where K is the x[K] or the index of that point
    and N is the over all number of samples taking
    I am new to FFT and I am not sure if I am right
     
  9. can someone please tell me how to get WKn?

    when you are doing a 8 or a 4 point

    say you have x = [ 2 , 1, -1, -3, 0, 1 ,2 , 1]

    x[0] 2 2
    butterfly diagram
    x[4] 0 2



    x[2] -1 1
    butterfly diagram
    x[6] 2 -3

    ok now how do you get 1 and -3 ?
    (-1 + 6 W68) right ? and that should be a i?
    can someone please help me
    I am so lost right now
     
  10. I would focus on the DC component. Your code is producing a DC component of 15.64. The FFT is indicating 0 DC. The DC component is just the mean value of the waveform samples. Walk through your code and see if you can see why that is happening.
     
  11. I need to know how Wkn
    is pick or set..
    my code works but only for 4 points and I can only do 4 points by hand
    i need to know
    how k and n or know for the W
    i can see it if i look up a picture of a butterflies diagram on Google
    but I do not know how they pick/know what k and n is along the butterflies diagram
    can someone please help me??
    can someone please explain that to me?
     
  12. Mark44

    Staff: Mentor

    I didn't realize you had another thread on this.
    How are you seeing these as the same?
    Matlab results (1 through 4)
    1.0000 + 1.0000i
    1.0000 - 1.0000i
    1.0000 + 1.0000i
    1.0000 - 1.0000i

    Your results (1 through 4)
    1 + 1i OK
    1 + 0i Different
    0 + 0i Different
    1 + 0i Different

    It might be easier to spot the differences if you format the two sets of results in the same way.
     
Know someone interested in this topic? Share a link to this question via email, Google+, Twitter, or Facebook

Have something to add?

0
Draft saved Draft deleted