1 /* ======================================================================= */
2 /* fft_example.c -- FFT Example File */
3 /* */
4 /* Rev 0.0.1 */
5 /* */
6 /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ */
7 /* */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without */
10 /* modification, are permitted provided that the following conditions */
11 /* are met: */
12 /* */
13 /* Redistributions of source code must retain the above copyright */
14 /* notice, this list of conditions and the following disclaimer. */
15 /* */
16 /* Redistributions in binary form must reproduce the above copyright */
17 /* notice, this list of conditions and the following disclaimer in the */
18 /* documentation and/or other materials provided with the */
19 /* distribution. */
20 /* */
21 /* Neither the name of Texas Instruments Incorporated nor the names of */
22 /* its contributors may be used to endorse or promote products derived */
23 /* from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
28 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
31 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
32 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
33 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
34 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
35 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /* This example demonstrates the usage of the various FFT kernels */
38 /* provided with the C6x DSPLIB. The example shows: */
39 /* - Twiddle factor generation for the various kernels */
40 /* - Scaling that needs to be applied to get similar output */
41 /* when using different kernels */
42 /* - Demonstrates the usage of FFT APIs */
43 /* */
44 /* ----------------------------------------------------------------------- */
45 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
46 /* All Rights Reserved. */
47 /* ======================================================================= */
49 #include <stdint.h>
50 #include <math.h>
51 #include <ti/dsplib/dsplib.h>
53 #include "gen_twiddle_fft16x16.h"
54 #include "gen_twiddle_fft16x32.h"
55 #include "gen_twiddle_fft32x32.h"
57 /* Global definitions */
58 /* Number of samples for which FFT needs to be calculated */
59 #define N 256
60 /* Number of unique sine waves in input data */
61 #define NUM_SIN_WAVES 4
63 /*
64 Scale need to be applied (1/2^SCALE) to input data for 16x32 and 32x32
65 FFT for their output to match 16x16. This is dure to the inherent
66 scaling present in 16x16 kernel
67 */
68 #define SCALE 3
71 /* Align the tables that we have to use */
72 #pragma DATA_ALIGN(x_ref, 8);
73 int32_t x_ref [2*N];
75 #pragma DATA_ALIGN(x_16x16, 8);
76 int16_t x_16x16 [2*N];
77 #pragma DATA_ALIGN(y_16x16, 8);
78 int16_t y_16x16 [2*N];
79 #pragma DATA_ALIGN(w_16x16, 8);
80 int16_t w_16x16 [2*N];
82 #pragma DATA_ALIGN(x_16x32, 8);
83 int32_t x_16x32 [2*N];
84 #pragma DATA_ALIGN(y_16x32, 8);
85 int32_t y_16x32 [2*N];
86 #pragma DATA_ALIGN(w_16x32, 8);
87 int16_t w_16x32 [2*N];
89 #pragma DATA_ALIGN(x_32x32, 8);
90 int32_t x_32x32 [2*N];
91 #pragma DATA_ALIGN(y_32x32, 8);
92 int32_t y_32x32 [2*N];
93 #pragma DATA_ALIGN(w_32x32, 8);
94 int32_t w_32x32 [2*N];
96 /*
97 This function generates the input data and also updates the
98 input data arrays used by the various FFT kernels
99 */
100 float x_ref_float [2*N];
101 void generateInput (int32_t numSinWaves) {
102 int32_t i, j;
103 float sinWaveIncFreq, sinWaveMag;
105 /*
106 Based on numSinWave information, create the input data. The
107 input data is first created using floating point dataType. The
108 floating point data type is then converted to the appropriate
109 fixed point notation
110 */
111 /* Clear the input floating point array */
112 for (i = 0; i < N; i++) {
113 x_ref_float[2*i] = (float)0.0;
114 x_ref_float[2*i + 1] = (float)0.0;
115 }
117 /* Calculate the incremental freq for each sin wave */
118 sinWaveIncFreq = ((float)3.142)/(numSinWaves*(float)1.0);
120 /* Calculate the magnitude for each sin wave */
121 sinWaveMag = (float)1.0/(numSinWaves * (float)1.0*N);
123 /* Create the input array as sum of the various sin wave data */
124 for (j = 0; j < numSinWaves; j++) {
125 for (i = 0; i < N; i++) {
126 x_ref_float[2*i] += sinWaveMag * (float)cos(sinWaveIncFreq*j*i);
127 x_ref_float[2*i + 1] = (float) 0.0;
128 }
129 }
131 /* Convert the floating point data to reference fixed point data */
132 for (i = 0; i < N; i++) {
133 x_ref[2*i] = x_ref_float[2*i] * 2147483648;
134 x_ref[2*i + 1] = x_ref_float[2*i + 1] * 2147483648;
135 }
137 /* Copy the reference input data to the various input arrays */
138 for (i = 0; i < N; i++) {
139 x_16x16[2*i] = (x_ref[2*i] >> 16);
140 x_16x16[2*i + 1] = (x_ref[2*i + 1] >> 16);
142 x_16x32[2*i] = x_ref[2*i] >> SCALE;
143 x_16x32[2*i + 1] = x_ref[2*i + 1] >> SCALE;
145 x_32x32[2*i] = x_ref[2*i] >> SCALE;
146 x_32x32[2*i + 1] = x_ref[2*i + 1] >> SCALE;
147 }
148 }
150 /*
151 The seperateRealImg function seperates the real and imaginary data
152 of the FFT output. This is needed so that the data can be plotted
153 using the CCS graph feature
154 */
156 int16_t y_real_16x16 [N];
157 int16_t y_imag_16x16 [N];
159 int32_t y_real_16x32 [N];
160 int32_t y_imag_16x32 [N];
162 int32_t y_real_32x32 [N];
163 int32_t y_imag_32x32 [N];
165 seperateRealImg () {
166 int32_t i, j;
168 for (i = 0, j = 0; j < N; i+=2, j++) {
169 y_real_16x16[j] = y_16x16[i];
170 y_imag_16x16[j] = y_16x16[i + 1];
172 y_real_16x32[j] = y_16x32[i];
173 y_imag_16x32[j] = y_16x32[i + 1];
175 y_real_32x32[j] = y_32x32[i];
176 y_imag_32x32[j] = y_32x32[i + 1];
177 }
178 }
180 /*
181 The main function that implements the example functionality.
182 This example demonstrates the usage of the various FFT kernels provided
183 with the C6x DSPLIB. The example shows:
184 - Twiddle factor generation for the various kernels
185 - Needed scaling to get correct output
186 - Demonstrates usage of FFT APIs
187 */
189 void main () {
190 /* Generate the input data */
191 generateInput (NUM_SIN_WAVES);
193 /* Genarate the various twiddle factors */
194 gen_twiddle_fft16x16(w_16x16, N);
195 gen_twiddle_fft16x32(w_16x32, N);
196 gen_twiddle_fft32x32(w_32x32, N, 2147483647.5);
198 /* Call the various FFT routines */
199 DSP_fft16x16(w_16x16, N, x_16x16, y_16x16);
200 DSP_fft16x32(w_16x32, N, x_16x32, y_16x32);
201 DSP_fft32x32(w_32x32, N, x_32x32, y_32x32);
203 /* Call the test code to seperate the real and imaginary data */
204 seperateRealImg ();
205 }
207 /* ======================================================================== */
208 /* End of file: fft_example.c */
209 /* ------------------------------------------------------------------------ */
210 /* Copyright (c) 2011 Texas Instruments, Incorporated. */
211 /* All Rights Reserved. */
212 /* ======================================================================== */