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.
Signal Processing Examples Using the TMS320C67x DSP Library (Rev. A)
http://www.ti.com/lit/an/spra947a/spra947a.pdf
3.3.4 FFT Example (Page 15)
For this example the output data is only the output image (Figure 10).
Please show how FFT was performed.
Best regards,
Daisuke
Daisuke,
The FFT was calculated using the DSPF_sp_fftSPxSP API provided inside the C67x DSPLIB with N=1024. Unfortunately, it appears the sample project is not provided as part of the C67x DSPLIB but if you download the C674x DSPLIB and check the example source for the DSPF_sp_fftSPxSP, it very similar to the code required to compute the FFT in that app Note. The Magnitude is calculated by taking the sum of square of the real and the imaginary part obtained from the complex FFT output obtained from that API.
Could you also confirm, that you are using a C6700 DSP and not a C674x DSP? The DSPLIB for the two are different so I would like to confirm that you are refering to the right library.
Regards,
Rahul
Hi Rahul,
Thank you for your reply.
I used the C674x DSPLIB with the C6747 Device Functional Simulator, and used the attached file source that changed the C674x DSPLIB FFT example source.
/* ======================================================================= */ /* fft_sp_example.c -- FFT Example File */ /* */ /* This example demonstrates the usage of the various FFT kernels */ /* provided with the C6x DSPLIB. The example shows: */ /* - Twiddle factor generation for the various kernels */ /* - Scaling that needs to be applied to get similar output */ /* when using different kernels */ /* - Demonstrates the usage of FFT APIs */ /* */ /* Rev 0.0.1 */ /* */ /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ */ /* */ /* */ /* Redistribution and use in source and binary forms, with or without */ /* modification, are permitted provided that the following conditions */ /* are met: */ /* */ /* Redistributions of source code must retain the above copyright */ /* notice, this list of conditions and the following disclaimer. */ /* */ /* Redistributions in binary form must reproduce the above copyright */ /* notice, this list of conditions and the following disclaimer in the */ /* documentation and/or other materials provided with the */ /* distribution. */ /* */ /* Neither the name of Texas Instruments Incorporated nor the names of */ /* its contributors may be used to endorse or promote products derived */ /* from this software without specific prior written permission. */ /* */ /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */ /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */ /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */ /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */ /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */ /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */ /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */ /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* */ /* ----------------------------------------------------------------------- */ /* Copyright (c) 2011 Texas Instruments, Incorporated. */ /* All Rights Reserved. */ /* ======================================================================= */ #include <stdint.h> #include <math.h> #include "../inc/dsplib.h" /* Global definitions */ /* Number of samples for which FFT needs to be calculated */ //#define N 256 #define N 512 /* Number of unique sine waves in input data */ #define NUM_SIN_WAVES 4 /* Align the tables that we have to use */ #pragma DATA_ALIGN(x_ref, 8); float x_ref [2*N]; #pragma DATA_ALIGN(x_sp, 8); float x_sp [2*N]; #pragma DATA_ALIGN(y_sp, 8); float y_sp [2*N]; #pragma DATA_ALIGN(w_sp, 8); float w_sp [2*N]; 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 }; /* This function generates the input data and also updates the input data arrays used by the various FFT kernels */ void generateInput (int numSinWaves) { int i, j; float sinWaveIncFreq, sinWaveMag; /* Based on numSinWave information, create the input data. The input data is first created using floating point dataType. The floating point data type is then converted to the appropriate fixed point notation */ /* Clear the input floating point array */ for (i = 0; i < N; i++) { x_ref[2*i] = (float)0.0; x_ref[2*i+1] = (float)0.0; } /* Calculate the incremental freq for each sin wave */ sinWaveIncFreq = ((float)3.142)/(numSinWaves*(float)1.0); /* Calculate the magnitude for each sin wave */ sinWaveMag = (float)1.0/(numSinWaves * (float)1.0*N); /* Create the input array as sum of the various sin wave data */ for (j = 0; j < numSinWaves; j++) { for (i = 0; i < N; i++) { x_ref[2*i] += sinWaveMag * (float)cos(sinWaveIncFreq*j*i); x_ref[2*i+1] = (float) 0.0; } } /* Copy the reference input data to the various input arrays */ for (i = 0; i < N; i++) { x_sp [2*i] = x_ref[2*i]; x_sp [2*i+1] = x_ref[2*i+1]; } } /* The seperateRealImg function seperates the real and imaginary data of the FFT output. This is needed so that the data can be plotted using the CCS graph feature */ float y_real_sp [N]; float y_imag_sp [N]; seperateRealImg () { int i, j; for (i = 0, j = 0; j < N; i+=2, j++) { y_real_sp[j] = y_sp[i]; y_imag_sp[j] = y_sp[i + 1]; } } /* The main function that implements the example functionality. This example demonstrates the usage of the various FFT kernels provided with the C6x DSPLIB. The example shows: - Twiddle factor generation for the various kernels - Needed scaling to get correct output - Demonstrates usage of FFT APIs */ /* Function for generating Specialized sequence of twiddle factors */ void gen_twiddle_fft_sp (float *w, int n) { int i, j, k; double x_t, y_t, theta1, theta2, theta3; const double PI = 3.141592654; for (j = 1, k = 0; j <= n >> 2; j = j << 2) { for (i = 0; i < n >> 2; i += j) { theta1 = 2 * PI * i / n; x_t = cos (theta1); y_t = sin (theta1); w[k] = (float) x_t; w[k + 1] = (float) y_t; theta2 = 4 * PI * i / n; x_t = cos (theta2); y_t = sin (theta2); w[k + 2] = (float) x_t; w[k + 3] = (float) y_t; theta3 = 6 * PI * i / n; x_t = cos (theta3); y_t = sin (theta3); w[k + 4] = (float) x_t; w[k + 5] = (float) y_t; k += 6; } } } #define PI ((float)3.142) #define F1 10 #define F2 40 void main () { int i; /* Generate the input data */ // generateInput (NUM_SIN_WAVES); for(i=0; i<N; i++) { /* real part */ x_ref[2 * i] = (sin(2 * PI * F1 * i / N) + sin(2 * PI * F2 * i / N)); /* img part */ x_ref[2 * i + 1] = 0; } /* Copy the reference input data to the various input arrays */ for (i = 0; i < N; i++) { x_sp [2*i] = x_ref[2*i]; x_sp [2*i+1] = x_ref[2*i+1]; } /* Genarate twiddle factors */ gen_twiddle_fft_sp(w_sp, N); /* Call FFT routine */ // DSPF_sp_fftSPxSP(N, x_sp, w_sp, y_sp, brev, 4, 0, N); DSPF_sp_fftSPxSP(N, x_sp, w_sp, y_sp, brev, 2, 0, N); /* Call the test code to seperate the real and imaginary data */ seperateRealImg (); } /* ======================================================================== */ /* End of file: fft_example.c */ /* ------------------------------------------------------------------------ */ /* Copyright (c) 2011 Texas Instruments, Incorporated. */ /* All Rights Reserved. */ /* ======================================================================== */
Is the calculation result different between C67x DSPLIB and C674x DSPLIB?
Best regards,
Daisuke
The calculation results should be the same for both the functions. Let us know if you are still seeing some issues
Regards
Rahul
Hi Rahul,
Thank you for your reply.
I calculated the Magnitude.
/* ======================================================================= */ /* fft_sp_example.c -- FFT Example File */ /* */ /* This example demonstrates the usage of the various FFT kernels */ /* provided with the C6x DSPLIB. The example shows: */ /* - Twiddle factor generation for the various kernels */ /* - Scaling that needs to be applied to get similar output */ /* when using different kernels */ /* - Demonstrates the usage of FFT APIs */ /* */ /* Rev 0.0.1 */ /* */ /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ */ /* */ /* */ /* Redistribution and use in source and binary forms, with or without */ /* modification, are permitted provided that the following conditions */ /* are met: */ /* */ /* Redistributions of source code must retain the above copyright */ /* notice, this list of conditions and the following disclaimer. */ /* */ /* Redistributions in binary form must reproduce the above copyright */ /* notice, this list of conditions and the following disclaimer in the */ /* documentation and/or other materials provided with the */ /* distribution. */ /* */ /* Neither the name of Texas Instruments Incorporated nor the names of */ /* its contributors may be used to endorse or promote products derived */ /* from this software without specific prior written permission. */ /* */ /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */ /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */ /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */ /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */ /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */ /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */ /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */ /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */ /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* */ /* ----------------------------------------------------------------------- */ /* Copyright (c) 2011 Texas Instruments, Incorporated. */ /* All Rights Reserved. */ /* ======================================================================= */ #include <stdint.h> #include <math.h> #include "../inc/dsplib.h" /* Global definitions */ /* Number of samples for which FFT needs to be calculated */ //#define N 256 #define N 512 /* Number of unique sine waves in input data */ #define NUM_SIN_WAVES 4 /* Align the tables that we have to use */ #pragma DATA_ALIGN(x_ref, 8); float x_ref [2*N]; #pragma DATA_ALIGN(x_sp, 8); float x_sp [2*N]; #pragma DATA_ALIGN(y_sp, 8); float y_sp [2*N]; #pragma DATA_ALIGN(w_sp, 8); float w_sp [2*N]; 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 }; /* This function generates the input data and also updates the input data arrays used by the various FFT kernels */ void generateInput (int numSinWaves) { int i, j; float sinWaveIncFreq, sinWaveMag; /* Based on numSinWave information, create the input data. The input data is first created using floating point dataType. The floating point data type is then converted to the appropriate fixed point notation */ /* Clear the input floating point array */ for (i = 0; i < N; i++) { x_ref[2*i] = (float)0.0; x_ref[2*i+1] = (float)0.0; } /* Calculate the incremental freq for each sin wave */ sinWaveIncFreq = ((float)3.142)/(numSinWaves*(float)1.0); /* Calculate the magnitude for each sin wave */ sinWaveMag = (float)1.0/(numSinWaves * (float)1.0*N); /* Create the input array as sum of the various sin wave data */ for (j = 0; j < numSinWaves; j++) { for (i = 0; i < N; i++) { x_ref[2*i] += sinWaveMag * (float)cos(sinWaveIncFreq*j*i); x_ref[2*i+1] = (float) 0.0; } } /* Copy the reference input data to the various input arrays */ for (i = 0; i < N; i++) { x_sp [2*i] = x_ref[2*i]; x_sp [2*i+1] = x_ref[2*i+1]; } } /* The seperateRealImg function seperates the real and imaginary data of the FFT output. This is needed so that the data can be plotted using the CCS graph feature */ float y_real_sp [N]; float y_imag_sp [N]; seperateRealImg () { int i, j; for (i = 0, j = 0; j < N; i+=2, j++) { y_real_sp[j] = y_sp[i]; y_imag_sp[j] = y_sp[i + 1]; } } /* The main function that implements the example functionality. This example demonstrates the usage of the various FFT kernels provided with the C6x DSPLIB. The example shows: - Twiddle factor generation for the various kernels - Needed scaling to get correct output - Demonstrates usage of FFT APIs */ /* Function for generating Specialized sequence of twiddle factors */ void gen_twiddle_fft_sp (float *w, int n) { int i, j, k; double x_t, y_t, theta1, theta2, theta3; const double PI = 3.141592654; for (j = 1, k = 0; j <= n >> 2; j = j << 2) { for (i = 0; i < n >> 2; i += j) { theta1 = 2 * PI * i / n; x_t = cos (theta1); y_t = sin (theta1); w[k] = (float) x_t; w[k + 1] = (float) y_t; theta2 = 4 * PI * i / n; x_t = cos (theta2); y_t = sin (theta2); w[k + 2] = (float) x_t; w[k + 3] = (float) y_t; theta3 = 6 * PI * i / n; x_t = cos (theta3); y_t = sin (theta3); w[k + 4] = (float) x_t; w[k + 5] = (float) y_t; k += 6; } } } #define PI ((float)3.142) #define F1 10 #define F2 40 float y_ref[N]; void main () { int i; double y_pow, y_r, y_i; /* Generate the input data */ // generateInput (NUM_SIN_WAVES); for(i=0; i<N; i++) { /* real part */ x_ref[2 * i] = (sin(2 * PI * F1 * i / N) + sin(2 * PI * F2 * i / N)); /* img part */ x_ref[2 * i + 1] = 0; } /* Copy the reference input data to the various input arrays */ for (i = 0; i < N; i++) { x_sp [2*i] = x_ref[2*i]; x_sp [2*i+1] = x_ref[2*i+1]; } /* Genarate twiddle factors */ gen_twiddle_fft_sp(w_sp, N); /* Call FFT routine */ // DSPF_sp_fftSPxSP(N, x_sp, w_sp, y_sp, brev, 4, 0, N); DSPF_sp_fftSPxSP(N, x_sp, w_sp, y_sp, brev, 2, 0, N); /* Call the test code to seperate the real and imaginary data */ seperateRealImg (); /* Calculating the square root of the sum of square of the real and the imaginary part */ for(i=0; i<N; i++) { y_r = y_real_sp[i]; y_i = y_imag_sp[i]; y_pow = pow(y_r, y_r) - pow(y_i, y_i); y_ref[i] = sqrt(y_pow); } } /* ======================================================================== */ /* End of file: fft_example.c */ /* ------------------------------------------------------------------------ */ /* Copyright (c) 2011 Texas Instruments, Incorporated. */ /* All Rights Reserved. */ /* ======================================================================== */
Is my calculation correct?
Best regards,
Daisuke
Hi, Daisuke,
May I know which tools are you using to create the graph in your attached picture?
I notice that there is a file (refOutput_sp.jpg) in DSPLIB example folder. It seems that the graph in this file is created by CCS. I am wondering how to create the graph automatically when running the example program in CCS. Any idea?
Thanks and Regards,
Shunnian
Shunnian,
Their are graph tools provided under Tools tab that allow you to plot input and output buffers of the FFT.
Hi Daisuke,
What is the input signal to the FFT function? Are you plotting all the samples in the Magnitude plot. Plot of FFT spectrum is symmetrical, so if you compute an N point FFT of a signal the magnitude a sample f will be same as that of (N-f-1) th sample.
Regards,
Rahul
Hi Shunnian,
Here is how to display graphs by CCS.
http://processors.wiki.ti.com/index.php/GSG:Debugging_projects_v5#Displaying_graphs
The Help Contents of CCS have the details.
Best regards,
Daisuke
Hi Rahul,
Thank you for your reply.
Sorry for my late reply.
The input signal is the combination of sine waves of 10Hz and 40Hz. It is the same as the FFT Example in the application note (SPRA947A).
I want to get an output graph same as Figure 10 in the application note.
Best regards,
Daisuke
Hi, Rahul and Daisuke,
Thank you for your information.Now I am able to create the graph.
Best Regards,
Shunnian