arm_cfft_q31.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cfft_q31.c
  4. * Description: Combined Radix Decimation in Frequency CFFT fixed point processing function
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "arm_math.h"
  29. extern void arm_radix4_butterfly_q31(
  30. q31_t * pSrc,
  31. uint32_t fftLen,
  32. q31_t * pCoef,
  33. uint32_t twidCoefModifier);
  34. extern void arm_radix4_butterfly_inverse_q31(
  35. q31_t * pSrc,
  36. uint32_t fftLen,
  37. q31_t * pCoef,
  38. uint32_t twidCoefModifier);
  39. extern void arm_bitreversal_32(
  40. uint32_t * pSrc,
  41. const uint16_t bitRevLen,
  42. const uint16_t * pBitRevTable);
  43. void arm_cfft_radix4by2_q31(
  44. q31_t * pSrc,
  45. uint32_t fftLen,
  46. const q31_t * pCoef);
  47. void arm_cfft_radix4by2_inverse_q31(
  48. q31_t * pSrc,
  49. uint32_t fftLen,
  50. const q31_t * pCoef);
  51. /**
  52. * @ingroup groupTransforms
  53. */
  54. /**
  55. * @addtogroup ComplexFFT
  56. * @{
  57. */
  58. /**
  59. * @details
  60. * @brief Processing function for the fixed-point complex FFT in Q31 format.
  61. * @param[in] *S points to an instance of the fixed-point CFFT structure.
  62. * @param[in, out] *p1 points to the complex data buffer of size <code>2*fftLen</code>. Processing occurs in-place.
  63. * @param[in] ifftFlag flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform.
  64. * @param[in] bitReverseFlag flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output.
  65. * @return none.
  66. */
  67. void arm_cfft_q31(
  68. const arm_cfft_instance_q31 * S,
  69. q31_t * p1,
  70. uint8_t ifftFlag,
  71. uint8_t bitReverseFlag)
  72. {
  73. uint32_t L = S->fftLen;
  74. if (ifftFlag == 1U)
  75. {
  76. switch (L)
  77. {
  78. case 16:
  79. case 64:
  80. case 256:
  81. case 1024:
  82. case 4096:
  83. arm_radix4_butterfly_inverse_q31 ( p1, L, (q31_t*)S->pTwiddle, 1 );
  84. break;
  85. case 32:
  86. case 128:
  87. case 512:
  88. case 2048:
  89. arm_cfft_radix4by2_inverse_q31 ( p1, L, S->pTwiddle );
  90. break;
  91. }
  92. }
  93. else
  94. {
  95. switch (L)
  96. {
  97. case 16:
  98. case 64:
  99. case 256:
  100. case 1024:
  101. case 4096:
  102. arm_radix4_butterfly_q31 ( p1, L, (q31_t*)S->pTwiddle, 1 );
  103. break;
  104. case 32:
  105. case 128:
  106. case 512:
  107. case 2048:
  108. arm_cfft_radix4by2_q31 ( p1, L, S->pTwiddle );
  109. break;
  110. }
  111. }
  112. if ( bitReverseFlag )
  113. arm_bitreversal_32((uint32_t*)p1,S->bitRevLength,S->pBitRevTable);
  114. }
  115. /**
  116. * @} end of ComplexFFT group
  117. */
  118. void arm_cfft_radix4by2_q31(
  119. q31_t * pSrc,
  120. uint32_t fftLen,
  121. const q31_t * pCoef)
  122. {
  123. uint32_t i, l;
  124. uint32_t n2, ia;
  125. q31_t xt, yt, cosVal, sinVal;
  126. q31_t p0, p1;
  127. n2 = fftLen >> 1;
  128. ia = 0;
  129. for (i = 0; i < n2; i++)
  130. {
  131. cosVal = pCoef[2*ia];
  132. sinVal = pCoef[2*ia + 1];
  133. ia++;
  134. l = i + n2;
  135. xt = (pSrc[2 * i] >> 2) - (pSrc[2 * l] >> 2);
  136. pSrc[2 * i] = (pSrc[2 * i] >> 2) + (pSrc[2 * l] >> 2);
  137. yt = (pSrc[2 * i + 1] >> 2) - (pSrc[2 * l + 1] >> 2);
  138. pSrc[2 * i + 1] = (pSrc[2 * l + 1] >> 2) + (pSrc[2 * i + 1] >> 2);
  139. mult_32x32_keep32_R(p0, xt, cosVal);
  140. mult_32x32_keep32_R(p1, yt, cosVal);
  141. multAcc_32x32_keep32_R(p0, yt, sinVal);
  142. multSub_32x32_keep32_R(p1, xt, sinVal);
  143. pSrc[2U * l] = p0 << 1;
  144. pSrc[2U * l + 1U] = p1 << 1;
  145. }
  146. // first col
  147. arm_radix4_butterfly_q31( pSrc, n2, (q31_t*)pCoef, 2U);
  148. // second col
  149. arm_radix4_butterfly_q31( pSrc + fftLen, n2, (q31_t*)pCoef, 2U);
  150. for (i = 0; i < fftLen >> 1; i++)
  151. {
  152. p0 = pSrc[4*i+0];
  153. p1 = pSrc[4*i+1];
  154. xt = pSrc[4*i+2];
  155. yt = pSrc[4*i+3];
  156. p0 <<= 1;
  157. p1 <<= 1;
  158. xt <<= 1;
  159. yt <<= 1;
  160. pSrc[4*i+0] = p0;
  161. pSrc[4*i+1] = p1;
  162. pSrc[4*i+2] = xt;
  163. pSrc[4*i+3] = yt;
  164. }
  165. }
  166. void arm_cfft_radix4by2_inverse_q31(
  167. q31_t * pSrc,
  168. uint32_t fftLen,
  169. const q31_t * pCoef)
  170. {
  171. uint32_t i, l;
  172. uint32_t n2, ia;
  173. q31_t xt, yt, cosVal, sinVal;
  174. q31_t p0, p1;
  175. n2 = fftLen >> 1;
  176. ia = 0;
  177. for (i = 0; i < n2; i++)
  178. {
  179. cosVal = pCoef[2*ia];
  180. sinVal = pCoef[2*ia + 1];
  181. ia++;
  182. l = i + n2;
  183. xt = (pSrc[2 * i] >> 2) - (pSrc[2 * l] >> 2);
  184. pSrc[2 * i] = (pSrc[2 * i] >> 2) + (pSrc[2 * l] >> 2);
  185. yt = (pSrc[2 * i + 1] >> 2) - (pSrc[2 * l + 1] >> 2);
  186. pSrc[2 * i + 1] = (pSrc[2 * l + 1] >> 2) + (pSrc[2 * i + 1] >> 2);
  187. mult_32x32_keep32_R(p0, xt, cosVal);
  188. mult_32x32_keep32_R(p1, yt, cosVal);
  189. multSub_32x32_keep32_R(p0, yt, sinVal);
  190. multAcc_32x32_keep32_R(p1, xt, sinVal);
  191. pSrc[2U * l] = p0 << 1;
  192. pSrc[2U * l + 1U] = p1 << 1;
  193. }
  194. // first col
  195. arm_radix4_butterfly_inverse_q31( pSrc, n2, (q31_t*)pCoef, 2U);
  196. // second col
  197. arm_radix4_butterfly_inverse_q31( pSrc + fftLen, n2, (q31_t*)pCoef, 2U);
  198. for (i = 0; i < fftLen >> 1; i++)
  199. {
  200. p0 = pSrc[4*i+0];
  201. p1 = pSrc[4*i+1];
  202. xt = pSrc[4*i+2];
  203. yt = pSrc[4*i+3];
  204. p0 <<= 1;
  205. p1 <<= 1;
  206. xt <<= 1;
  207. yt <<= 1;
  208. pSrc[4*i+0] = p0;
  209. pSrc[4*i+1] = p1;
  210. pSrc[4*i+2] = xt;
  211. pSrc[4*i+3] = yt;
  212. }
  213. }