r_fft.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**********************************************************************
  2. Each of the companies; Lucent, Motorola, Nokia, and Qualcomm (hereinafter
  3. referred to individually as "Source" or collectively as "Sources") do
  4. hereby state:
  5. To the extent to which the Source(s) may legally and freely do so, the
  6. Source(s), upon submission of a Contribution, grant(s) a free,
  7. irrevocable, non-exclusive, license to the Third Generation Partnership
  8. Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA, TTA,
  9. and TTC, under the Source's copyright or copyright license rights in the
  10. Contribution, to, in whole or in part, copy, make derivative works,
  11. perform, display and distribute the Contribution and derivative works
  12. thereof consistent with 3GPP2's and each Organizational Partner's
  13. policies and procedures, with the right to (i) sublicense the foregoing
  14. rights consistent with 3GPP2's and each Organizational Partner's policies
  15. and procedures and (ii) copyright and sell, if applicable) in 3GPP2's name
  16. or each Organizational Partner's name any 3GPP2 or transposed Publication
  17. even though this Publication may contain the Contribution or a derivative
  18. work thereof. The Contribution shall disclose any known limitations on
  19. the Source's rights to license as herein provided.
  20. When a Contribution is submitted by the Source(s) to assist the
  21. formulating groups of 3GPP2 or any of its Organizational Partners, it
  22. is proposed to the Committee as a basis for discussion and is not to
  23. be construed as a binding proposal on the Source(s). The Source(s)
  24. specifically reserve(s) the right to amend or modify the material
  25. contained in the Contribution. Nothing contained in the Contribution
  26. shall, except as herein expressly provided, be construed as conferring
  27. by implication, estoppel or otherwise, any license or right under (i)
  28. any existing or later issuing patent, whether or not the use of
  29. information in the document necessarily employs an invention of any
  30. existing or later issued patent, (ii) any copyright, (iii) any
  31. trademark, or (iv) any other intellectual property right.
  32. With respect to the Software necessary for the practice of any or
  33. all Normative portions of the Enhanced Variable Rate Codec (EVRC) as
  34. it exists on the date of submittal of this form, should the EVRC be
  35. approved as a Specification or Report by 3GPP2, or as a transposed
  36. Standard by any of the 3GPP2's Organizational Partners, the Source(s)
  37. state(s) that a worldwide license to reproduce, use and distribute the
  38. Software, the license rights to which are held by the Source(s), will
  39. be made available to applicants under terms and conditions that are
  40. reasonable and non-discriminatory, which may include monetary compensation,
  41. and only to the extent necessary for the practice of any or all of the
  42. Normative portions of the EVRC or the field of use of practice of the
  43. EVRC Specification, Report, or Standard. The statement contained above
  44. is irrevocable and shall be binding upon the Source(s). In the event
  45. the rights of the Source(s) in and to copyright or copyright license
  46. rights subject to such commitment are assigned or transferred, the
  47. Source(s) shall notify the assignee or transferee of the existence of
  48. such commitments.
  49. *******************************************************************/
  50. /*======================================================================*/
  51. /* Enhanced Variable Rate Codec - Bit-Exact C Specification */
  52. /* Copyright (C) 1997-1998 Telecommunications Industry Association. */
  53. /* All rights reserved. */
  54. /*----------------------------------------------------------------------*/
  55. /* Note: Reproduction and use of this software for the design and */
  56. /* development of North American Wideband CDMA Digital */
  57. /* Cellular Telephony Standards is authorized by the TIA. */
  58. /* The TIA does not authorize the use of this software for any */
  59. /* other purpose. */
  60. /* */
  61. /* The availability of this software does not provide any license */
  62. /* by implication, estoppel, or otherwise under any patent rights */
  63. /* of TIA member companies or others covering any use of the */
  64. /* contents herein. */
  65. /* */
  66. /* Any copies of this software or derivative works must include */
  67. /* this and all other proprietary notices. */
  68. /*======================================================================*/
  69. /* r_fft.c */
  70. /*****************************************************************
  71. *
  72. * This is an implementation of decimation-in-time FFT algorithm for
  73. * real sequences. The techniques used here can be found in several
  74. * books, e.g., i) Proakis and Manolakis, "Digital Signal Processing",
  75. * 2nd Edition, Chapter 9, and ii) W.H. Press et. al., "Numerical
  76. * Recipes in C", 2nd Ediiton, Chapter 12.
  77. *
  78. * Input - There are two inputs to this function:
  79. *
  80. * 1) An integer pointer to the input data array
  81. * 2) An integer value which should be set as +1 for FFT
  82. * and some other value, e.g., -1 for IFFT
  83. *
  84. * Output - There is no return value.
  85. * The input data are replaced with transformed data. If the
  86. * input is a real time domain sequence, it is replaced with
  87. * the complex FFT for positive frequencies. The FFT value
  88. * for DC and the foldover frequency are combined to form the
  89. * first complex number in the array. The remaining complex
  90. * numbers correspond to increasing frequencies. If the input
  91. * is a complex frequency domain sequence arranged as above,
  92. * it is replaced with the corresponding time domain sequence.
  93. *
  94. * Notes:
  95. *
  96. * 1) This function is designed to be a part of a noise supp-
  97. * ression algorithm that requires 128-point FFT of real
  98. * sequences. This is achieved here through a 64-point
  99. * complex FFT. Consequently, the FFT size information is
  100. * not transmitted explicitly. However, some flexibility
  101. * is provided in the function to change the size of the
  102. * FFT by specifying the size information through "define"
  103. * statements.
  104. *
  105. * 2) The values of the complex sinusoids used in the FFT
  106. * algorithm are computed once (i.e., the first time the
  107. * r_fft function is called) and stored in a table. To
  108. * further speed up the algorithm, these values can be
  109. * precomputed and stored in a ROM table in actual DSP
  110. * based implementations.
  111. *
  112. * 3) In the c_fft function, the FFT values are divided by
  113. * 2 after each stage of computation thus dividing the
  114. * final FFT values by 64. No multiplying factor is used
  115. * for the IFFT. This is somewhat different from the usual
  116. * definition of FFT where the factor 1/N, i.e., 1/64, is
  117. * used for the IFFT and not the FFT. No factor is used in
  118. * the r_fft function.
  119. *
  120. * 4) Much of the code for the FFT and IFFT parts in r_fft
  121. * and c_fft functions are similar and can be combined.
  122. * They are, however, kept separate here to speed up the
  123. * execution.
  124. *
  125. *****************************************************************/
  126. //#include "mathevrc.h"
  127. #include "dsp_math.h"
  128. #include "mathdp31.h"
  129. #include "mathadv.h"
  130. #define SIZE 128
  131. #define SIZE_BY_TWO 64
  132. #define NUM_STAGE 6
  133. #define TRUE 1
  134. #define FALSE 0
  135. static Shortword phs_tbl[] =
  136. {
  137. 32767, 0, 32729, -1608, 32610, -3212, 32413, -4808,
  138. 32138, -6393, 31786, -7962, 31357, -9512, 30853, -11039,
  139. 30274, -12540, 29622, -14010, 28899, -15447, 28106, -16846,
  140. 27246, -18205, 26320, -19520, 25330, -20788, 24279, -22006,
  141. 23170, -23170, 22006, -24279, 20788, -25330, 19520, -26320,
  142. 18205, -27246, 16846, -28106, 15447, -28899, 14010, -29622,
  143. 12540, -30274, 11039, -30853, 9512, -31357, 7962, -31786,
  144. 6393, -32138, 4808, -32413, 3212, -32610, 1608, -32729,
  145. 0, -32768, -1608, -32729, -3212, -32610, -4808, -32413,
  146. -6393, -32138, -7962, -31786, -9512, -31357, -11039, -30853,
  147. -12540, -30274, -14010, -29622, -15447, -28899, -16846, -28106,
  148. -18205, -27246, -19520, -26320, -20788, -25330, -22006, -24279,
  149. -23170, -23170, -24279, -22006, -25330, -20788, -26320, -19520,
  150. -27246, -18205, -28106, -16846, -28899, -15447, -29622, -14010,
  151. -30274, -12540, -30853, -11039, -31357, -9512, -31786, -7962,
  152. -32138, -6393, -32413, -4808, -32610, -3212, -32729, -1608
  153. };
  154. static Shortword ii_table[] =
  155. {SIZE / 2, SIZE / 4, SIZE / 8, SIZE / 16, SIZE / 32, SIZE / 64};
  156. /* FFT/IFFT function for complex sequences */
  157. /*
  158. * The decimation-in-time complex FFT/IFFT is implemented below.
  159. * The input complex numbers are presented as real part followed by
  160. * imaginary part for each sample. The counters are therefore
  161. * incremented by two to access the complex valued samples.
  162. */
  163. #ifndef ANDROID
  164. void c_fft(Shortword * farray_ptr, Shortword isign)
  165. {
  166. Shortword i, j, k, ii, jj, kk, ji, kj;
  167. Longword ftmp, ftmp_real, ftmp_imag;
  168. Shortword tmp, tmp1, tmp2;
  169. /* Rearrange the input array in bit reversed order */
  170. for (i = 0, j = 0; i < SIZE - 2; i = i + 2)
  171. {
  172. if (j > i)
  173. {
  174. ftmp = *(farray_ptr + i);
  175. *(farray_ptr + i) = *(farray_ptr + j);
  176. *(farray_ptr + j) = ftmp;
  177. ftmp = *(farray_ptr + i + 1);
  178. *(farray_ptr + i + 1) = *(farray_ptr + j + 1);
  179. *(farray_ptr + j + 1) = ftmp;
  180. }
  181. k = SIZE_BY_TWO;
  182. while (j >= k)
  183. {
  184. j = sub(j, k);
  185. k = shr(k, 1);
  186. }
  187. j += k;
  188. }
  189. /* The FFT part */
  190. if (isign == 1)
  191. {
  192. for (i = 0; i < NUM_STAGE; i++)
  193. { /* i is stage counter */
  194. jj = shl(2, i); /* FFT size */
  195. kk = shl(jj, 1); /* 2 * FFT size */
  196. ii = ii_table[i]; /* 2 * number of FFT's */
  197. for (j = 0; j < jj; j = j + 2)
  198. { /* j is sample counter */
  199. ji = j * ii; /* ji is phase table index */
  200. for (k = j; k < SIZE; k = k + kk)
  201. { /* k is butterfly top */
  202. kj = add(k, jj); /* kj is butterfly bottom */
  203. /* Butterfly computations */
  204. ftmp_real = L_sub(L_mult(*(farray_ptr + kj), phs_tbl[ji]),
  205. L_mult(*(farray_ptr + kj + 1), phs_tbl[ji + 1]));
  206. ftmp_imag = L_add(L_mult(*(farray_ptr + kj + 1), phs_tbl[ji]),
  207. L_mult(*(farray_ptr + kj), phs_tbl[ji + 1]));
  208. tmp1 = round32(ftmp_real);
  209. tmp2 = round32(ftmp_imag);
  210. tmp = sub(*(farray_ptr + k), tmp1);
  211. *(farray_ptr + kj) = shr(tmp, 1);
  212. tmp = sub(*(farray_ptr + k + 1), tmp2);
  213. *(farray_ptr + kj + 1) = shr(tmp, 1);
  214. tmp = add(*(farray_ptr + k), tmp1);
  215. *(farray_ptr + k) = shr(tmp, 1);
  216. tmp = add(*(farray_ptr + k + 1), tmp2);
  217. *(farray_ptr + k + 1) = shr(tmp, 1);
  218. }
  219. }
  220. }
  221. /* The IFFT part */
  222. }
  223. else
  224. {
  225. for (i = 0; i < NUM_STAGE; i++)
  226. { /* i is stage counter */
  227. jj = shl(2, i); /* FFT size */
  228. kk = shl(jj, 1); /* 2 * FFT size */
  229. ii = ii_table[i]; /* 2 * number of FFT's */
  230. for (j = 0; j < jj; j = j + 2)
  231. { /* j is sample counter */
  232. ji = j * ii; /* ji is phase table index */
  233. for (k = j; k < SIZE; k = k + kk)
  234. { /* k is butterfly top */
  235. kj = add(k, jj); /* kj is butterfly bottom */
  236. /* Butterfly computations */
  237. ftmp_real = L_add(L_mult(*(farray_ptr + kj), phs_tbl[ji]),
  238. L_mult(*(farray_ptr + kj + 1), phs_tbl[ji + 1]));
  239. ftmp_imag = L_sub(L_mult(*(farray_ptr + kj + 1), phs_tbl[ji]),
  240. L_mult(*(farray_ptr + kj), phs_tbl[ji + 1]));
  241. tmp1 = round32(ftmp_real);
  242. tmp2 = round32(ftmp_imag);
  243. *(farray_ptr + kj) = sub(*(farray_ptr + k), tmp1);
  244. *(farray_ptr + kj + 1) = sub(*(farray_ptr + k + 1), tmp2);
  245. *(farray_ptr + k) = add(*(farray_ptr + k), tmp1);
  246. *(farray_ptr + k + 1) = add(*(farray_ptr + k + 1), tmp2);
  247. }
  248. }
  249. }
  250. }
  251. } /* end of c_fft () */
  252. #endif
  253. void r_fft(Shortword * farray_ptr, Shortword isign)
  254. {
  255. Shortword ftmp1_real, ftmp1_imag, ftmp2_real, ftmp2_imag;
  256. Longword Lftmp1_real, Lftmp1_imag, Lftmp2_real, Lftmp2_imag;
  257. Shortword i, j;
  258. Longword Ltmp1, Ltmp2;
  259. /* The FFT part */
  260. if (isign == 1)
  261. {
  262. /* Perform the complex FFT */
  263. c_fft(farray_ptr, isign);
  264. /* First, handle the DC and foldover frequencies */
  265. ftmp1_real = *farray_ptr;
  266. ftmp2_real = *(farray_ptr + 1);
  267. *farray_ptr = add(ftmp1_real, ftmp2_real);
  268. *(farray_ptr + 1) = sub(ftmp1_real, ftmp2_real);
  269. /* Now, handle the remaining positive frequencies */
  270. for (i = 2, j = SIZE - i; i <= SIZE_BY_TWO; i = i + 2, j = SIZE - i)
  271. {
  272. ftmp1_real = add(*(farray_ptr + i), *(farray_ptr + j));
  273. ftmp1_imag = sub(*(farray_ptr + i + 1), *(farray_ptr + j + 1));
  274. ftmp2_real = add(*(farray_ptr + i + 1), *(farray_ptr + j + 1));
  275. ftmp2_imag = sub(*(farray_ptr + j), *(farray_ptr + i));
  276. Lftmp1_real = L_deposit_h(ftmp1_real);
  277. Lftmp1_imag = L_deposit_h(ftmp1_imag);
  278. Lftmp2_real = L_deposit_h(ftmp2_real);
  279. Lftmp2_imag = L_deposit_h(ftmp2_imag);
  280. Ltmp1 = L_sub(L_mult(ftmp2_real, phs_tbl[i]), L_mult(ftmp2_imag, phs_tbl[i + 1]));
  281. *(farray_ptr + i) = round32(L_shr(L_add(Lftmp1_real, Ltmp1), 1));
  282. Ltmp1 = L_add(L_mult(ftmp2_imag, phs_tbl[i]), L_mult(ftmp2_real, phs_tbl[i + 1]));
  283. *(farray_ptr + i + 1) = round32(L_shr(L_add(Lftmp1_imag, Ltmp1), 1));
  284. Ltmp1 = L_add(L_mult(ftmp2_real, phs_tbl[j]), L_mult(ftmp2_imag, phs_tbl[j + 1]));
  285. *(farray_ptr + j) = round32(L_shr(L_add(Lftmp1_real, Ltmp1), 1));
  286. Ltmp1 = L_add(L_negate(L_mult(ftmp2_imag, phs_tbl[j])), L_mult(ftmp2_real, phs_tbl[j + 1]));
  287. Ltmp2 = L_add(L_negate(Lftmp1_imag), Ltmp1);
  288. *(farray_ptr + j + 1) = round32(L_shr(Ltmp2, 1));
  289. }
  290. }
  291. else
  292. {
  293. /* First, handle the DC and foldover frequencies */
  294. ftmp1_real = *farray_ptr;
  295. ftmp2_real = *(farray_ptr + 1);
  296. *farray_ptr = shr(add(ftmp1_real, ftmp2_real), 1);
  297. *(farray_ptr + 1) = shr(sub(ftmp1_real, ftmp2_real), 1);
  298. /* Now, handle the remaining positive frequencies */
  299. for (i = 2, j = SIZE - i; i <= SIZE_BY_TWO; i = i + 2, j = SIZE - i)
  300. {
  301. ftmp1_real = add(*(farray_ptr + i), *(farray_ptr + j));
  302. ftmp1_imag = sub(*(farray_ptr + i + 1), *(farray_ptr + j + 1));
  303. ftmp2_real = negate(add(*(farray_ptr + j + 1), *(farray_ptr + i + 1)));
  304. ftmp2_imag = negate(sub(*(farray_ptr + j), *(farray_ptr + i)));
  305. Lftmp1_real = L_deposit_h(ftmp1_real);
  306. Lftmp1_imag = L_deposit_h(ftmp1_imag);
  307. Lftmp2_real = L_deposit_h(ftmp2_real);
  308. Lftmp2_imag = L_deposit_h(ftmp2_imag);
  309. Ltmp1 = L_add(L_mult(ftmp2_real, phs_tbl[i]), L_mult(ftmp2_imag, phs_tbl[i + 1]));
  310. *(farray_ptr + i) = round32(L_shr(L_add(Lftmp1_real, Ltmp1), 1));
  311. Ltmp1 = L_sub(L_mult(ftmp2_imag, phs_tbl[i]), L_mult(ftmp2_real, phs_tbl[i + 1]));
  312. *(farray_ptr + i + 1) = round32(L_shr(L_add(Lftmp1_imag, Ltmp1), 1));
  313. Ltmp1 = L_sub(L_mult(ftmp2_real, phs_tbl[j]), L_mult(ftmp2_imag, phs_tbl[j + 1]));
  314. *(farray_ptr + j) = round32(L_shr(L_add(Lftmp1_real, Ltmp1), 1));
  315. Ltmp1 = L_negate(L_add(L_mult(ftmp2_imag, phs_tbl[j]), L_mult(ftmp2_real, phs_tbl[j + 1])));
  316. Ltmp2 = L_add(L_negate(Lftmp1_imag), Ltmp1);
  317. *(farray_ptr + j + 1) = round32(L_shr(Ltmp2, 1));
  318. }
  319. /* Perform the complex IFFT */
  320. c_fft(farray_ptr, isign);
  321. }
  322. } /* end r_fft () */