This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

FFT Peak and Magnitude Problem

Other Parts Discussed in Thread: ADS7881

Hi,

I am trying to implement FFT algorithm for Real Input disucssed on wiki page given below.

http://processors.wiki.ti.com/index.php/Efficient_FFT_Computation_of_Real_Input

But I am not getting the right result, it has 3 peaks which can be observed in the figure given below.

Moreover, FFT magnitude is not showing the right amplitude as of input signal which is 1.25V. I will really appreciate if some one help me out here.

Regards.

Tariq

  • Will some one from TI reply to this post ?

    I have been waiting for a week now for some one to consider my problem..

     

  • Tariq,

    Your screenshot looks "almost" correct for a sinusoid input signal.  The biggest problem is the spike in the middle of the buffer.  I have not seen an artifact like this in my tests.  Are you using the source code example from the real FFT wiki page?

    Based on the screenshot, I am guessing that you have a 1024-sample data buffer and calculate a 512-point FFT using the DSPLIB and the technique described on the wiki.  Is that correct?

  • Joe,

    Thanks for your help.

    Yes I am using the source code example from the real FFT wiki page. I am using ADC to sample analog signal, Input buffer has 1024 samples and calculating N/2 which is 512-point real FFT. Following is the routine I am running on C6713.

    #define N 1024

    float pRFFT_In[N];
    float pRFFT_Out[2 * N];
    float pTemp[N];

    //we can generate twidle factors using tw_gen();
    float w[N];
    float A[N];
    float B[N];

    tw_gen (w, N / 2);

    split_gen (A, B, N / 2);
       
    twiddle = (float *) w;
       
    // Forward FFT Calculation using N/2 complex FFT..
    DSPF_sp_fftSPxSP (N / 2, pRFFT_In, twiddle, pTemp, brev, rad, 0, N / 2);

    // FFT Split call to get complex FFT out of length N..
    FFT_Split (N / 2, pTemp, A, B, pRFFT_Out);

    // FFT Magnitude calculation..

        for(j=0; j < N; j++)

            {
                fft_mag[j] = magcalculation((double)pRFFT_Out[2*j], (double)pRFFT_Out[2*j+1]);
            }

     

  • Tariq,

    I have one comment about the above code.  After you calculate a N / 2 point FFT, you should only end up with N / 2 complex values in your output buffer.  This means you should only attempt to calculate N / 2 magnitude values.  In your code, you appear to be calculating a full N magnitude values using data from beyond the end of your data buffer.  I recommend computing only the first N / 2 magnitude values; the later values do not accurately reflect the FFT of your input signal.

    Hope this helps.

  • Joe,

    Thanks for your help.

    I followed some previous posts related to this FFT algorithm and set the sizes of different buffer according to that post . I am still confused lillte bit.

    Do you mean the size of both input and output buffer of FFT should be N ?

    I modified the size of output buffer as shown below and computed N/2 magnitude values which can be seen in the graph given below.

    float pRFFT_In[N]; // Input Buffer
    float pRFFT_Out[N]; // Output Buffer
    float pTemp[N];

    Why the magnitude of both the peaks are not same ?

    Hope to get your reply soon.

  • Tariq,

    I'm glad that we got rid of that "glitch" in the middle of your magnitude buffer.  Regarding the difference in magnitude between the two peaks, I think this is a normal consequence of the FFT algorithm.  Since the FFT represents a sampled form of the frequency domain, it's possible to "miss" strong narrow peaks.  You can address this by computing the FFT with a higher resolution (i.e. higher N).  One way to do this is zero-pad your input signal.

    For example, your input buffer could contain 1024 input values followed by 1024 zero values.  Then, you could compute a 1024-point real FFT using that 2048-element buffer.  The magnitude in that result should more closely match the ideal DFT you expect to see.

    Hope this helps.

  • Joe,

    I am copying one of your comments which I followed while writing my code. [http://e2e.ti.com/support/dsp/tms320c6000_high_performance_dsps/f/115/p/64079/233741.aspx#233741]

    If you start with a 512-point real sequence, your FFT data will have 1024 values representing 512 complex numbers.  Using the real FFT trick, you can obtain this sequence by computing a 256-point FFT (i.e. N = 256).

    In my case, N is 1024 so I defined different buffer as follows,

    float pRFFT_In[N]; // Input Buffer representing 512 complex values
    float pRFFT_Out[2 * N]; // Output Buffer representing 1024 complex values
    float pTemp[N];

    Now you says after you calculate a N / 2 point FFT, you should only end up with N / 2 complex values in your output buffer ?

    I am really confused. Can you please tell me the exact size of different buffer being used in this code ?

    I will appreciate your help.


     

  • Tariq,

    I think your input buffer has to be interleaved when you do a complex FFT.  (Real - Imaginary - Real - Imaginary ....). So, if your input values are purely real, this means that you have to interleave zeros after each, making your 512 real samples into a 1024 complex samples.

    Regards,
    Dan

     

  • Dan,

    Tariq's usage is correct in this case.  He is following a procedure we specified on the wiki to "trick" the FFT algorithm into treating an N-value real sequence as an N / 2-value complex sequence.  The resulting length N / 2 FFT represents the frequency content of the original N-value real sequence.

    Tariq,

    My previous post was not meant to say that your current usage is incorrect.  What I am saying is that you can use a larger FFT size (with the same input signal) to get better resolution in the frequency domain.  This will give you better looking peaks, etc., at the cost of additional computation.

  • Joe,

    I did computed 2048 point FFT but still the magnitude of both peaks are different.

    Can you please comment on the size of different buffers, I just want to make sure I am using the right size of different buffers.

    #define N 1024

    float pRFFT_In[N];
    float pRFFT_Out[N];
    float pTemp[N];

    //we can generate twidle factors using tw_gen();
    float w[N];
    float A[N];
    float B[N];

    float fft_mag[N/2];

    tw_gen (w, N / 2);
    split_gen (A, B, N / 2);
    twiddle = (float *) w;
       
    DSPF_sp_fftSPxSP (N / 2, pRFFT_In, twiddle, pTemp, brev, rad, 0, N / 2);
    FFT_Split (N / 2, pTemp, A, B, pRFFT_Out);

    Other thing is that we computed the N-point complex FFT, but we only used N/2-point FFT computation. Should I divide by N or N/2 while calculating magnitude ?

    Thanks.

     

  • Tariq,

    Your buffer sizes appear correct for a 512-point (i.e. N/2-point) FFT computation.  Assuming your input signal is 1024 samples in length and you don't want to pad it to get more resolution in the frequency domain, this is correct.

    When calculating magnitude, I believe that you should normalize by N / 2 since that is technically the length of the FFT that you calculated.

  • Joe,

    Few question aries here by looking at the graph.

    Sampling frequency which I am using is 351.56 KHz. If N/2 is the length of FFT and N is 1024 in my case then frequency resolution will be 351.56 KHz/512 = 686.64 Hz which should give a peak for 1 KHz sine wave at 2nd sample. But you can see in the picture that peak rises at 4th sample which seems true if frequency resolution has been calculated using N = 1024.

    Moreover, I divided the FFT magnitude by N which gives almost the same magnitude as of iput signal. If it was N/2 It will not give the right magnitude.

    Another surprising thing is that, FFT magnitude window shows frequencies from 0  to Fs when N/2 Point FFT is considered for calculating freq. resolution (512 FFT Point*686.64 Hz = 351.56 KHz). Otherwise it shows only half of Fs (512*343.23 Hz = 175.78 KHz).

    Above 2 calculations seem OK when considering N point FFT but not the last one . These results are contradictory in nature and I am not satisfied with the result.

    Please help me understand this. If you want I can give you my code.

    Thanks for your consideration.

    -Tariq

     

  • Sorry one mistake in the previous post.

    Magnitude is calculated using N/2 point.

    Amplitude = Magnitude [FFT(A)] / N/2

  • Tariq,

    If your sampling frequency is 351.56 kHz, then the maximum frequency represented in your FFT is FS / 2, or 175.78 kHz.  In this case, having the peak on the fourth sample (corresponding to 1.03 kHz) seems correct.  The FFT buffer should represent frequency content in the range +/- FS / 2.  For a real signal the DFT magnitude should be symmetric, so you can limit yourself to [0:FS / 2] by looking only at the first half of the FFT buffer.

  • Joe,

    Thanks for your quick help.

    I know FFT buffer will show half of the Fs but still sampling frequency will be used to calculate the freq. resolution i.e.

    Frequency Resolution = Sample Rate / FFT Points = 351.56 kHz/512 = 686.64 Hz

    OR what formula did you use which made you say that having the peak on the fourth sample (corresponding to 1.03 kHz) seems correct ?

  • Joe,

    Do I need to consider 175.78 kHz while calculating freq. resolution ?

    175.78 kHz / 512 = 343.32 Hz ?

     

  • Joe,

    Can you please help me clarifying my doubts ?

    I don't understand the relation between the Sampling Rate and No. of FFT Points.. How the peak appeared at 4th sample seem to be correct ? FFT shows frequency range from 0 to Fs /2 which is 175.78 kHz but still the Sampling Freq. is 351.56 kHz..

  • Tariq,

    Sorry for my delay in responding.  I think that you are correct about the frequency resolution.  I'm not sure how to explain the peak placement on your FFT results.  It should be in the position that most closely corresponds to 1 kHz.  I would double check your sampling frequency and the frequency of your input signal to make sure that they are what we think they are.

  • I hope to get your immidiate response.

    -Tariq

  • Joe,

    Yes, I am confident that there is some thing wrong.

    My sampling frequency is 351.56 kHz, lets suppose if N = 1024 in this code example and according to your previous comments we are performing 512 point FFT. This gives frequency resolution of 351.56 kHz/512 = 686.64 Hz. If we discard half of FFT point because of symmetric property then FFT frequency range shown by FFT magnitude would be 686.64 Hz*256 Point = 175.78 kHz which should practically show FFT peak of sine wave with frequency 150 kHz. But it is not showing any peak rather noises on CCS graph window. Maximum frequency peak shown by graph window is 86 kHz.

    According to the post given below, Rahul from TI defined size of different buffers as follows:

    float pRFFT_In[ N];
    float pRFFT_Out[2 * N];

    On the other hand you told me to defined the size of output buffer as N.

    http://e2e.ti.com/support/dsp/tms320c6000_high_performance_dsps/f/115/p/95526/335986.aspx#335986

    My last posts about verifying FFT peak is still pending.

    I hope to get your satisfactory reply ASAP.

    -Tariq

  • Tariq,

    If the highest frequency that you can observe in your FFT results is 86 kHz, that would suggest that the sampling frequency is actually ~172 kHz.  If that were true, it would also explain the placement of your 1 kHz peak.

    I will also check with the algorithm expert to make sure I haven't confused myself on this topic.

  • Tariq,

    Consider f to be the input frequancy of your sine wave and Fs to be the sampling frequency and Fr be the frequency resolution. Fr = Fs/N where N is the number of points in the FFT.Now let us look at the question of Frequency resolution and interpretting the ouput. If the Fs= ~356Khz and you are objective is to compute 1024 point FFT of a sine wave of 1Khz

    According to the wiki, in the first step you compute a 512 point FFT by considering odd samples to be complex terms of the even samples. The resulting FFT will be a 512 point FFT with frequency resolution Fs/512 with the 512th sample corresponding to Fs= ~356Khz

    In the next step you interpolate these 512 values to obtain the 1024 point FFT so your Frequency resolution will be Fs/1024= 356/1024 =0.347 Khz. Hence if your input sine wave is of 1Khz, the peak will appear at (f/Fr)  and at (Fs-f)/Fr that is at X(3) and X(1021) which is what your graph shows.

    Please let us know if this resolves all your questions about the implementation.

    Regards,

    Rahul

  • Rahul,

    You mean if N is 1024, it means that we are performing 1024 point FFT but in two steps (DSPF_sp_fftSPxSP and FFT_Split). So instead of N/2, actual N will be used to compute the freq. resolution i.e 351 kHz/1024 = 0.347 kHz, right ?

    Please have a look on the size of different buffers I am using at the moment.

    #define N 1024

    float pRFFT_In[N];
    float pRFFT_Out[N];
    float pTemp[N];

    //we can generate twidle factors using tw_gen();
    float w[N];
    float A[N];
    float B[N];

    tw_gen (w, N / 2);
    split_gen (A, B, N / 2);
    twiddle = (float *) w;

    I think the size of pRFFT_Out should ne 2*N if 1024 point FFT is being calculated. If the size of pRFFT_Out is N then N/2 point will be considered to compute the magnitude and that will give 512 point which will translate to 512*342.77 = 175.5 kHz and after discarding half of the point it will only show 87.75 kHz which is not right.

    Appreciate your help.

    Tariq.

  • Rahul,

    If I am right and the size of pRFFT_Out should be 2*N then I am having trouble while seeing the FFT peak. It is showing 3 peaks which can be seen below.

    I need immidiate help. Please reply to this post. Please also have a look on the size of different buffers which I have shown in my previous post.

    -Tariq

  • Rahul,

    You have put yourself into the discussion and now you are gone.. Still waiting for your reply..

  • Tariq,

    I think size of FFT Output buffer should ne 2*N otherwise you will have N/2 points after calculating magnitude and that will represent 87.5 kHz if N is 1024 in your code.

    TI experts can explain better...

    -BAS

  • Tariq,

    Sorry I couldn`t reply over the weekend. Here is the reply based on your comments

    You mean if N is 1024, it means that we are performing 1024 point FFT but in two steps (DSPF_sp_fftSPxSP and FFT_Split). So instead of N/2, actual N will be used to compute the freq. resolution i.e 351 kHz/1024 = 0.347 kHz, right ?

    -> This is correct. Please realize here that your input is real and the ouput is complex so if input contains 1024 real values(buffer size 1024) but output will be of 1024 complex values which corresponds to buffer size of 2*1024 (even value corresponds to real part and odd value corresponds to imaginary part ).

    I think the size of pRFFT_Out should ne 2*N if 1024 point FFT is being calculated.

    ->That is correct

    If the size of pRFFT_Out is N then N/2 point will be considered to compute the magnitude and that will give 512 point which will translate to 512*342.77 = 175.5 kHz and after discarding half of the point it will only show 87.75 kHz which is not right.

     -> You may be confusing yourself here. pRFFT is of the size 1024 complex values(buffer size 2*N). Magnitude is calculated as squareroot of sum of squares of real and imaginary part . See Ak formula mentioned here

    http://en.wikipedia.org/wiki/Discrete_Fourier_transform#Definition

    So your magnitude plot will have 1024 frequency bins(0-1023). with frequency resolution of 356/1024. The last point in the spectrum corresponds to 1023 *356/1024 Khz as you first point corresponds to 0.

    Magnitude spectrum is always symetrical which is what you are seeing. The value at 4th bin corresonds to 3* 356/1024 =1.04Khz which is the frequency of your sine wave.

    Please let me know if this makes sense. There may be some bug in your code that is giving you a peak at your 512th bin. For a sine wave of 1Khz sampled at 356Khz , your magnitude spectrum will have peaks at 4th and 1020th sample

    Regards,

    Rahul

  • If you see the wiki this is the formula to calculate the 512th sample

    Gr(N/2) = Xr(0) – Xi(0)
    Gi(N/2) = 0

    There fore

    pGr (512) = PIn[0] -pIn[1]  = -0.0003 ;  // 2*512th value in your 1024 pt FFT output after FFT_split

     Gi(512) =0; // (2*512+1)th value in your 1024 pt output after FFT_split

    PIn[0] =0 and PIn[1]=  sin(2*PI/356) = 0.0003.

    So the magnitude of the 512th sample should be approximately 0. Please check this in your code and let me know if this is true

    Regards,

    Rahul

  • Rahul,

    Thanks for your detailed answer.

    I realized that magnitude of 0th sample is not 0 in my code, I forced it to become 0 because of the DC component. Therefore, magnitude of 512th component is also the same in magnitude as of 0th.

    I think it is ok to force 512th sample to be 0 as well because it is just the copy of the 0th sample which is DC component, isn't ?

    Appreciate your help.

     

  • Rahul,

    I am waiting for your reply......

  • Tariq,

    The 512th sample is not a DC sample. Only the zeroth sample is your DC sample. As per Symmetric propery of the FFT the 1024th sample would be equal to the DC sample.

    The 512th sample corresponds to half of sampling frequency  so I don`t think you should be zeroing out ths sample.

    Regards,

    Rahul

  • Rahul,

    0th and 512th have the same magnitude but they are not zero.

    It means my code is not working properly at this point. We have discussed all the major setup for this code now what could be wrong ?

  • Tariq,

    Can you post the code you are using to generate the sine wave here. Is there a DC component added in there? If you are using CCS can you plot your sine wave using the ploting tools and verify that the sine wave is generated correctly. Do you see the correct output when you run the code that is posted on the wiki ?

    Regards,

    Rahul

     

  • Rahul,

    I have mentioned in my previous post that I am using ADC to sample analog signal of 1 kHz sine wave generated from function generator. However, you can have a look on my code for FFT.

    #define N 1024

    #define PI 3.14159

    #pragma DATA_ALIGN(pRFFT_In, 8);               
    #pragma DATA_ALIGN(pRFFT_Out, 8);             

    #pragma DATA_ALIGN(pTemp, 8);
    #pragma DATA_ALIGN(w, 8);

    void tw_gen (float *w, int n);
    void split_gen (float *A, float *B, int n);
    void DSPF_sp_fftSPxSP (int, float *, float *, float *, unsigned char *, int, int, int);
    void FFT_Split (int n, float *pIn, float *A, float *B, float *pOut);

    /* For FFT implementation that shows efficient mechanism of computing FFT of a real
    valued signal without having to format the real input signal as a complex signal*/

    float pRFFT_In[N];
    float pRFFT_Out[2*N];
    float pTemp[N];

    //we can generate twidle factors using tw_gen();
    float w[N];
    float A[N];
    float B[N];

    #pragma DATA_ALIGN (brev , 8);
    unsigned char brev[64] = {
        0x0, 0x20, 0x10, 0x30, 0x8, 0x28, 0x18, 0x38,
        0x4, 0x24, 0x14, 0x34, 0xc, 0x2c, 0x1c, 0x3c,
        0x2, 0x22, 0x12, 0x32, 0xa, 0x2a, 0x1a, 0x3a,
        0x6, 0x26, 0x16, 0x36, 0xe, 0x2e, 0x1e, 0x3e,
        0x1, 0x21, 0x11, 0x31, 0x9, 0x29, 0x19, 0x39,
        0x5, 0x25, 0x15, 0x35, 0xd, 0x2d, 0x1d, 0x3d,
        0x3, 0x23, 0x13, 0x33, 0xb, 0x2b, 0x1b, 0x3b,
        0x7, 0x27, 0x17, 0x37, 0xf, 0x2f, 0x1f, 0x3f
    };

    int i, rad, nTemp = N / 2;
    float *twiddle;

    #pragma DATA_ALIGN (fft_mag, 8);
    float fft_mag[N];

    float magcalculation(double x, double y);
    int n=0;
    int m=0;
    int j=0;     
    int k=0;


    void main()
    {
        HWI_enable();
        IRQ_enable(IRQ_EVT_EXTINT5);
        TIMER_start(hTimer1);
    }

    void  ExtINT5_ISR(void)
    {
       
        pRFFT_In[k++] = (float)(((*(unsigned volatile int *)ADS7881)>>2&0x0FFF)/4096.0f)*vRef; // Copying data from ADC
               
        if (k >= N)            
       
        {
       
        k=0;

        if (nTemp == 16 || nTemp == 64 || nTemp == 256 || nTemp == 1024 || nTemp == 4096 || nTemp == 16384)
            rad = 4;
        else if (nTemp == 8 || nTemp == 32 || nTemp == 128 || nTemp == 512 || nTemp == 2048 || nTemp == 8192)
            rad = 2;
        else
        {
            printf ("%d Value of N is not supported \n", N);
            exit (0);
        }

        tw_gen (w, N / 2);
        split_gen (A, B, N / 2);
       
        twiddle = (float *) w;

       // Forward FFT Calculation using N/2 complex FFT..
        DSPF_sp_fftSPxSP (N / 2, pRFFT_In, twiddle, pTemp, brev, rad, 0, N / 2);

        // FFT Split call to get complex FFT out of length N..
        FFT_Split (N / 2, pTemp, A, B, pRFFT_Out);

           
        for(j=0; j < N; j++)

            {
                fft_mag[j] = magcalculation((double)pRFFT_Out[2*j], (double)pRFFT_Out[2*j+1]);
                fft_mag[0] = 0.0f;
            }

           }         
    }

  • Rahul,

    I am standing at the same position where I was a month ago. Work which I carried out during the whole month have gone in vain because I am getting the same result which I had before starting this post. Now I have also posted my code for your kind information, will you take this matter seriously and help me solving my problem ?

  • Tariq,

    Looking at your code there appears to be nothing wrong that may be causing the peak on the 512th sample that you are seeing. I was suspecting that this may be a cache related issue. In order to verify, I am looking to replicate the issue at my end by creating a similar project and passing it a continuos sine wave. Please be a little patient here, as we will be able to help you debug the issue only if we can see similar results with the code at our end.

    Regards,

    Rahul

  • Rahul,

    Thanks.

    I wait more for your response.

    -Tariq

  • Tariq,

    One thing that will help us debug this issue, is a a sample output from your ADC. Can you store 1024 samples from RFFT_In into a file  and post the file here?

    Regards,

    Rahul

  • Rahul,

    Please find attached samples of 10 kHz sine wave.

    0.723877
    0.917969
    1.198120
    1.404419
    1.604004
    1.792603
    1.962280
    2.108765
    2.225952
    2.310791
    2.361450
    2.375488
    2.352295
    2.292480
    2.198486
    2.072754
    1.920776
    1.745605
    1.553955
    1.351318
    1.144409
    0.939941
    0.743408
    0.562134
    0.402832
    0.268555
    0.164795
    0.094604
    0.059814
    0.061035
    0.100098
    0.174561
    0.281372
    0.418701
    0.580444
    0.762939
    0.960083
    1.165161
    1.372070
    1.574097
    1.764526
    1.937866
    2.087402
    2.209473
    2.299805
    2.355957
    2.375488
    2.357788
    2.304077
    2.214966
    2.094727
    1.947021
    1.774902
    1.585083
    1.383057
    1.176147
    0.971069
    0.773315
    0.590210
    0.426636
    0.288086
    0.179443
    0.103149
    0.062866
    0.058594
    0.090942
    0.160522
    0.262451
    0.394897
    0.554199
    0.733643
    0.929565
    1.133423
    1.340332
    1.542969
    1.735229
    1.911621
    2.066040
    2.192383
    2.288208
    2.349243
    2.375488
    2.362671
    2.315063
    2.231445
    2.116089
    1.970825
    1.801758
    1.614990
    1.414795
    1.209717
    1.003418
    0.803833
    0.617065
    0.449829
    0.307617
    0.194092
    0.112915
    0.066528
    0.056763
    0.084229
    0.147095
    0.244751
    0.372314
    0.527344
    0.704346
    0.897827
    1.100464
    1.307983
    1.512451
    1.706543
    1.885376
    2.043457
    2.174683
    2.275391
    2.342529
    2.373047
    2.366943
    2.324219
    2.246094
    2.135620
    1.995850
    1.830444
    1.645508
    1.447144
    1.240845
    1.034546
    0.833740
    0.645142
    0.474854
    0.328369
    0.209351
    0.122681
    0.071411
    0.056152
    0.077515
    0.135498
    0.227051
    0.350952
    0.501099
    0.675659
    0.866699
    1.068726
    1.275635
    1.480713
    1.677246
    1.859131
    2.020264
    2.155762
    2.261963
    2.334595
    2.371216
    2.370605
    2.333374
    2.261353
    2.155151
    2.019043
    1.857300
    1.478882
    1.273804
    1.273804
    1.066895
    0.865479
    0.673828
    0.499878
    0.349121
    0.225830
    0.134888
    0.076904
    0.056152
    0.072021
    0.123901
    0.211182
    0.329590
    0.476685
    0.646973
    0.835571
    1.036377
    1.242676
    1.448975
    1.647339
    1.832275
    1.997070
    2.136841
    2.247314
    2.325439
    2.367554
    2.373657
    2.341919
    2.274170
    2.173462
    2.042236
    1.884155
    1.705322
    1.510010
    1.305542
    1.098633
    0.895386
    0.702515
    0.525513
    0.371094
    0.243530
    0.146484
    0.083618
    0.057373
    0.066528
    0.113525
    0.195313
    0.308838
    0.451660
    0.619507
    0.805664
    1.005249
    1.211548
    1.417236
    1.616821
    1.804199
    1.972656
    2.116699
    2.232056
    2.315674
    2.363281
    2.374878
    2.349243
    2.287598
    2.191772
    2.064209
    1.910400
    1.733398
    1.541138
    1.338501
    1.130981
    0.927124
    0.731812
    0.551758
    0.393677
    0.159302
    0.090942
    0.090942
    0.058594
    0.062866
    0.103760
    0.180054
    0.289917
    0.427856
    0.592041
    0.775146
    0.973511
    1.177979
    1.385498
    1.586304
    1.776123
    1.947632
    2.096558
    2.216187
    2.304688
    2.357788
    2.375488
    2.355957
    2.299194
    2.208252
    2.086182
    1.936035
    1.762695
    1.572876
    1.370850
    1.163330
    0.957642
    0.760498
    0.578613
    0.416870
    0.280151
    0.173340
    0.099487
    0.061035
    0.060425
    0.094604
    0.165405
    0.270386
    0.404053
    0.564575
    0.745239
    0.941772
    1.146851
    1.353149
    1.555786
    1.747437
    1.921997
    2.073975
    2.199097
    2.293091
    2.352295
    2.375488
    2.361450
    2.310181
    2.225342
    2.107544
    1.961670
    1.790771
    1.602783
    1.401978
    1.195679
    0.989990
    0.791016
    0.606079
    0.440674
    0.299683
    0.187988
    0.108643
    0.064697
    0.057373
    0.087280
    0.152588
    0.252075
    0.381470
    0.716553
    0.716553
    0.910034
    1.113892
    1.320190
    1.524048
    1.718750
    1.896362
    2.052612
    2.182007
    2.280884
    2.344971
    2.374268
    2.365723
    2.320557
    2.239990
    2.127686
    1.986084
    1.818848
    1.633911
    1.434937
    1.228027
    1.021729
    0.821533
    0.633545
    0.465088
    0.319824
    0.203247
    0.119019
    0.068970
    0.056152
    0.079956
    0.140381
    0.233765
    0.359497
    0.512085
    0.687256
    0.878906
    1.082153
    1.288452
    1.493530
    1.688843
    1.869507
    2.029419
    2.163696
    2.267456
    2.337646
    2.371826
    2.369385
    2.329712
    2.255249
    2.147827
    2.009888
    1.846924
    1.663208
    1.465454
    1.260376
    1.054077
    0.852661
    0.662231
    0.490112
    0.340576
    0.219116
    0.129395
    0.075073
    0.056152
    0.073853
    0.128174
    0.217285
    0.338135
    0.486450
    0.658569
    0.848389
    1.049805
    1.256104
    1.461182
    1.659546
    1.843262
    2.006836
    2.144775
    2.253418
    2.329102
    2.369385
    2.372437
    2.338867
    2.269287
    2.166138
    2.033081
    1.873779
    1.693726
    1.497803
    1.292725
    1.086426
    0.883179
    0.690308
    0.515137
    0.361938
    0.236206
    0.141602
    0.081177
    0.056152
    0.068970
    0.117798
    0.201416
    0.317383
    0.462036
    0.629883
    0.817261
    1.017456
    1.224365
    1.430054
    1.629028
    1.815796
    1.982422
    2.124634
    2.238159
    2.319336
    2.365112
    2.374268
    2.346191
    2.282715
    2.184448
    2.055664
    1.900024
    1.721802
    1.528320
    1.324463
    1.117554
    0.914307
    0.720215
    0.541992
    0.384521
    0.253906
    0.153809
    0.087891
    0.057373
    0.064087
    0.106812
    0.186157
    0.297241
    0.437622
    0.603027
    0.787964
    0.986328
    1.191406
    1.397705
    1.598511
    1.787720
    1.958008
    2.104492
    2.222900
    2.308960
    2.360229
    2.375488
    2.352905
    2.294922
    2.201538
    2.077026
    1.925659
    1.751099
    1.560059
    1.357422
    1.151123
    0.946045
    0.748901
    0.567627
    0.407715
    0.272827
    0.167847
    0.096436
    0.060425
    0.060425
    0.098877
    0.172119
    0.277710
    0.413818
    0.575562
    0.757446
    0.953979
    1.159058
    1.366577
    1.567993
    1.759644
    1.932983
    2.083130
    2.206421
    2.297363
    2.354736
    2.375488
    2.359009
    2.306519
    2.218628
    2.099609
    1.951904
    1.780396
    1.590576
    1.389160
    1.182861
    0.977783
    0.779419
    0.595093
    0.431519
    0.291748
    0.181885
    0.104370
    0.063477
    0.058594
    0.090332
    0.158081
    0.259399
    0.391235
    0.548706
    0.728760
    0.923462
    1.126709
    1.333618
    1.536865
    1.730347
    1.907349
    2.061768
    2.189331
    2.285767
    2.348022
    2.374268
    2.364502
    2.316895
    2.234497
    2.119141
    1.975708
    1.807861
    1.621094
    1.421509
    1.215820
    1.009521
    0.809326
    0.622559
    0.455322
    0.311890
    0.197144
    0.114746
    0.067749
    0.057373
    0.083008
    0.145264
    0.241699
    0.368652
    0.522461
    0.698242
    0.891724
    1.094360
    1.301880
    1.506348
    1.701660
    1.881104
    2.039185
    2.171021
    2.272949
    2.340698
    2.373047
    2.368164
    2.326660
    2.249756
    2.139893
    2.000732
    1.835938
    1.651611
    1.453247
    1.247559
    1.041260
    0.839844
    0.651245
    0.479736
    0.332642
    0.213013
    0.125732
    0.072632
    0.055542
    0.076294
    0.133057
    0.223999
    0.346680
    0.497437
    0.670166
    0.861206
    1.062622
    1.269531
    1.473999
    1.671143
    1.854248
    2.015991
    2.152710
    2.259521
    2.332764
    2.370605
    2.371216
    2.335815
    2.263794
    2.158813
    2.023926
    1.862793
    1.681519
    1.484985
    1.280518
    1.073608
    0.871582
    0.679321
    0.504761
    0.353394
    0.229492
    0.136719
    0.078735
    0.056152
    0.070801
    0.122070
    0.208130
    0.325928
    0.472412
    0.642090
    0.830078
    1.030884
    1.237183
    1.443481
    1.641846
    1.827393
    1.992798
    2.133179
    2.244263
    2.323608
    2.366943
    2.373657
    2.343140
    2.277222
    2.177734
    2.047119
    1.890259
    1.711426
    1.516724
    1.312256
    1.104736
    0.902100
    0.708618
    0.531616
    0.375977
    0.247192
    0.148926
    0.085449
    0.057373
    0.065918
    0.111694
    0.192261
    0.305176
    0.446777
    0.614014
    0.800171
    0.999146
    1.204834
    1.411133
    1.611328
    1.798706
    1.967773
    2.113037
    2.229614
    2.313843
    2.362061
    2.374878
    2.350464
    2.290039
    2.194824
    2.069092
    1.915283
    1.740112
    1.547852
    1.345215
    1.138306
    0.933838
    0.737915
    0.557251
    0.397949
    0.264893
    0.162354
    0.092773
    0.059204
    0.062866
    0.101929
    0.177612
    0.286255
    0.423584
    0.587158
    0.769653
    0.966797
    1.172485
    1.379395
    1.580811
    1.770630
    1.943359
    2.092285
    2.213135
    2.302246
    2.357178
    2.375488
    2.357178
    2.301636
    2.211914
    2.090454
    1.941528
    1.768799
    1.578979
    1.376343
    1.169434
    0.964355
    0.767212
    0.584106
    0.421753
    0.284424
    0.176392
    0.101318
    0.062256
    0.059814
    0.093994
    0.163574
    0.266724
    0.399780
    0.559692
    0.740356
    0.936279
    1.140137
    1.347046
    1.549683
    1.741943
    1.917114
    2.070313
    2.196655
    2.290649
    2.351074
    2.374878
    2.362671
    2.313232
    2.228394
    2.111816
    1.965942
    1.796265
    1.608887
    1.408081
    1.201782
    0.996704
    0.797119
    0.611572
    0.445557
    0.303345
    0.191040
    0.111084
    0.065918
    0.057373
    0.085449
    0.150757
    0.249023
    0.377808
    0.533447
    0.710449
    0.903931
    1.107788
    1.314697
    1.518555
    1.713257
    1.891479
    2.048340
    2.178345
    2.278442
    2.373657
    2.366943
    2.366943
    2.322998
    2.243652
    2.131958
    1.990967
    1.824951
    1.640015
    1.440430
    1.234131
    1.028442
    0.827637
    0.639648
    0.469971
    0.324097
    0.206299
    0.121460
    0.070801
    0.056152
    0.079346
    0.137939
    0.231323
    0.355225
    0.506592
    0.682373
    0.873413
    1.076050
    1.282959
    1.487427
    1.683350
    1.864624
    2.025146
    2.160645
    2.265015
    2.335815
    2.371826
    2.369995
    2.331543
    2.258301
    2.151489
    2.014160
    1.851807
    1.669312
    1.472168
    1.267090
    1.060791
    0.858765
    0.667725
    0.494995
    0.344849
    0.222168
    0.132446
    0.075684
    0.056152
    0.073242
    0.126343
    0.214844
    0.333862
    0.482178
    0.653076
    0.842285
    1.043701
    1.250000
    1.455078
    1.654053
    1.837769
    2.001953
    2.141113
    2.250366
    2.327271
    2.368164
    2.373047
    2.340698
    2.272339
    2.170410
    2.037354
    1.878662
    1.504517
    1.299438
    1.299438
    1.092529
    0.889282
    0.696411
    0.520020
    0.366821
    0.239868
    0.144043
    0.082397
    0.056763
    0.068359
    0.115967
    0.198975
    0.313721
    0.457153
    0.625000
    0.811768
    1.011963
    1.218262
    1.423950
    1.623535
    1.810303
    1.977539
    2.120972
    2.235718
    2.318115
    2.364502
    2.374878
    2.348022
    2.285156
    2.188110
    2.060547
    1.905518
    1.727905
    1.535034
    1.331177
    1.124268
    0.921021
    0.726318
    0.546875
    0.389404
    0.257568
    0.156860
    0.089722
    0.058594
    0.064087
    0.105591
    0.183105
    0.294189
    0.433350
    0.598145
    0.781860
    0.980225
    1.185303
    1.391602
    1.593018
    1.782227
    1.953125
    2.100830
    2.219849
    2.307129
    2.359619
    2.375488
    2.354736
    2.297363
    2.205200
    2.082520
    1.931152
    1.757202
    1.566772
    1.364136
    1.156616
    0.952148
    0.755005
    0.573120
    0.411987
    0.276489
    0.098267
    0.098267
    0.061035
    0.060425
    0.097046
    0.169678
    0.274048
    0.409546
    0.570068
    0.751953
    0.948486
    1.153564
    1.360474
    1.562500
    1.753540
    1.928101
    2.078857
    2.203369
    2.296143
    2.354126
    2.375488
    2.360840
    2.308350
    2.221680
    2.103882
    1.956177
    1.785278
    1.596680
    1.395874
    1.188965
    0.983887
    0.785522
    0.600586
    0.435791
    0.295410
    0.184937
    0.107422
    0.064087
    0.058594
    0.089111
    0.155640
    0.255737
    0.386353
    0.543823
    0.722656
    0.917358
    1.120605
    1.328125
    1.531372
    1.724854
    1.901855
    2.056885
    2.186279
    2.283325
    2.347412
    2.374268
    2.365112
    2.318726
    2.236938
    2.123413
    1.980591
    1.813354
    1.627808
    1.428223
    1.221924
    1.015625
    0.814819
    0.628052
    0.459595
    0.315552
    0.200195
    0.117188
    0.068359
    0.056763
    0.081787
    0.142822
    0.238037
    0.363770
    0.517578
    0.692749
    0.885620
    1.088867
    1.295776
    1.500244
    

  • Tariq,

    Thanks for providing us your input data to the code. I took your input data and plugged the data into the Real FFT code that is distributed on the wiki page. Here is my observation.

    • There is no 512th sample in the Magnitude plot as you have mentioned in earlier post.
    • There is a peak at 0th sample(DC) and at 30th and the 994th sample (corresponding to the 10Khz sine wave)

    Please refer to the output graph as shown below.

    Based on these results,I am pretty sure that the code does not have any bugs that is causing the 512th sample that you are observing. Can you tell me if the code you have implemented is it cache based implementation. If it is a cache based implementation, you may want to manage the cache for input buffers and output buffers appropriately. One important care about while invalidating your cache is that the FFT function in the DSPLIB performs inplace processing of data so treat the input buffer as an input and output buffer.

    Regards,

    Rahul

  • Rahul,

    Strange !! I am not using any cache. Some thing weired is going on with my project . There is a peak at 0th sample (DC), 29th and 995th sample which you can see from the graph.

    Now the only choice I am left with is to show you my project. I am attaching my project, please have a look. I would really appreciate your help..

    1563.FFT_Tariq.zip

    Million of thanks.

  • Tariq,

    Let us analyze your code in reverse order. Your code is giving you a spike at 512th sample. The last place where the 512th sample is computed in the code is in the function FFT_Split. Can you set break point in your code on the highlighted lines and tell me the value of pIn[0] and pIn[1]

    void FFT_Split (int n, float *restrict pIn, float *pATable, float *pBTable, float *pOut)

    {

    int i;

    float Tr, Ti;

    _nassert ((int) pIn % 8 == 0);

    _nassert ((int) pOut % 8 == 0);

    _nassert ((int) pATable % 8 == 0);

    _nassert ((int) pBTable % 8 == 0);

    pIn[2 * n] = pIn[0];

    pIn[2 * n + 1] = pIn[1];

    #pragma UNROLL(2)

    for (i = 0; i < n; i++)

    {

    Tr = (pIn[2 * i] * pATable[2 * i] - pIn[2 * i + 1] * pATable[2 * i + 1] + pIn[2 * n - 2 * i] * pBTable[2 * i] + pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);

    Ti = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] + pIn[2 * n - 2 * i] * pBTable[2 * i + 1] - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);

    pOut[2 * i] = Tr;

    pOut[2 * i + 1] = Ti;

    // Use complex conjugate symmetry properties to get the rest..

    pOut[4 * n - 2 * i] = Tr;

    pOut[4 * n - 2 * i + 1] = -Ti;

    }

    pOut[2 * n] = pIn[0] - pIn[1]; //512th sample is computed here

    pOut[2 * n + 1] = 0;

    }

    In my setup, based on the data you provided the values are 621.192 and 621.976.

    Regards,

    Rahul

  • Rahul,

    Thanks for your help.

    Have you looked over my code ? Is every thing alright there ? I mean variable declarations, data alignments or implementation ?

    I am getting the following values;

    pIn[0] = 624.1718;

    pIn[1] = 623.9752;

    Thanks.



     

  • Tariq,

    Yes, your code looks okay. You have the same data alignment and implementation as the code on the wiki but have used same names for some global and local variables which has a potential to give errors but I don`t think the error in your code is due to this. For the time being let us not look at what the CCS tool is plotting but just look at the values you pass to the tool. Based on the information you have provided, how is it possible to get a peak at the 512th sample? The 512th sample as I have explained earlier is calculated as 

    pOut[2 * n] = pIn[0] - pIn[1]; //real term of 512th complex FFT sample

    pOut[2 * n + 1] = 0; // imaginary term of the 512th complex FFT sample

    As per this code, your 512th sample should be  0.2018 + j 0 which should not provide you a peak on that sample. Please debug your code in the above lines and watch the memory location of these variables using the CCS tool.

     

    Regards,

    Rahul

     

  • Rahul,

    I put the break point at the same point you mentioned earlier and noted the following values.

    pIn[0] = 619.4604

    pIn[0] = 619.6924

    After that I did single-step debugging. It seems that there is some thing wrong with Tr. Tr should be quite small value after computing the equation given below but it is giving me 309.8462.

    Tr = (pIn[2 * i] * pATable[2 * i] - pIn[2 * i + 1] * pATable[2 * i + 1] + pIn[2 * n - 2 * i] * pBTable[2 * i] + pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);

    As soon soon I compute next equation which gives Ti, Tr changes to 1239.153 and pOut which is simply equals to Tr according to the following equation

    pOut[2 * i] = Tr;

    shows different value which is 642.5165. I don't know what's going on but may be they are changing some where inside memory.

    I don't know what to do.. Do they make any sense to you ?

  • Rahul,

    It would be much easier and practical to use my project and see what I am saying.

    Thanks.

  • Rahul,

    Waiting for your help...

  • Tariq,

    At this point in time, we have explained to you how the code works and tried your code and data. Unfortunately we haven`t been able to replicate what you are seeing using your input signals. Beyond the support we have given you, I am not sure how we can help you debug this issue but to recommend that you single step through the code and analyze the code at the point when the code is behaving unexpectedly.

    Just as a test you might want to try out the complex FFT code that is also part of the example and see if that is giving you the correct answer.

    Regards,

    Rahul

  • Hi,

    I gave you my project for the same reason so that you see the unexpected behaviour of my code. Ofcourse my input signal will not give you that unexpected peak because you are using your code. I have been telling you since the day 1 that there is a bug in my code. I appreciate your support but I am standing at the same position even after having your help.

    Where I go now ? What I do in this circumstances ?