arm_cfft_q15.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cfft_q15.c
  4. * Description: Combined Radix Decimation in Q15 Frequency CFFT processing function
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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_q15(
  30. q15_t * pSrc,
  31. uint32_t fftLen,
  32. const q15_t * pCoef,
  33. uint32_t twidCoefModifier);
  34. extern void arm_radix4_butterfly_inverse_q15(
  35. q15_t * pSrc,
  36. uint32_t fftLen,
  37. const q15_t * pCoef,
  38. uint32_t twidCoefModifier);
  39. extern void arm_bitreversal_16(
  40. uint16_t * pSrc,
  41. const uint16_t bitRevLen,
  42. const uint16_t * pBitRevTable);
  43. void arm_cfft_radix4by2_q15(
  44. q15_t * pSrc,
  45. uint32_t fftLen,
  46. const q15_t * pCoef);
  47. void arm_cfft_radix4by2_inverse_q15(
  48. q15_t * pSrc,
  49. uint32_t fftLen,
  50. const q15_t * pCoef);
  51. /**
  52. @ingroup groupTransforms
  53. */
  54. /**
  55. @addtogroup ComplexFFT
  56. @{
  57. */
  58. /**
  59. @brief Processing function for Q15 complex FFT.
  60. @param[in] S points to an instance of Q15 CFFT structure
  61. @param[in,out] p1 points to the complex data buffer of size <code>2*fftLen</code>. Processing occurs in-place
  62. @param[in] ifftFlag flag that selects transform direction
  63. - value = 0: forward transform
  64. - value = 1: inverse transform
  65. @param[in] bitReverseFlag flag that enables / disables bit reversal of output
  66. - value = 0: disables bit reversal of output
  67. - value = 1: enables bit reversal of output
  68. @return none
  69. */
  70. void arm_cfft_q15(
  71. const arm_cfft_instance_q15 * S,
  72. q15_t * p1,
  73. uint8_t ifftFlag,
  74. uint8_t bitReverseFlag)
  75. {
  76. uint32_t L = S->fftLen;
  77. if (ifftFlag == 1U)
  78. {
  79. switch (L)
  80. {
  81. case 16:
  82. case 64:
  83. case 256:
  84. case 1024:
  85. case 4096:
  86. arm_radix4_butterfly_inverse_q15 ( p1, L, (q15_t*)S->pTwiddle, 1 );
  87. break;
  88. case 32:
  89. case 128:
  90. case 512:
  91. case 2048:
  92. arm_cfft_radix4by2_inverse_q15 ( p1, L, S->pTwiddle );
  93. break;
  94. }
  95. }
  96. else
  97. {
  98. switch (L)
  99. {
  100. case 16:
  101. case 64:
  102. case 256:
  103. case 1024:
  104. case 4096:
  105. arm_radix4_butterfly_q15 ( p1, L, (q15_t*)S->pTwiddle, 1 );
  106. break;
  107. case 32:
  108. case 128:
  109. case 512:
  110. case 2048:
  111. arm_cfft_radix4by2_q15 ( p1, L, S->pTwiddle );
  112. break;
  113. }
  114. }
  115. if ( bitReverseFlag )
  116. arm_bitreversal_16 ((uint16_t*) p1, S->bitRevLength, S->pBitRevTable);
  117. }
  118. /**
  119. @} end of ComplexFFT group
  120. */
  121. void arm_cfft_radix4by2_q15(
  122. q15_t * pSrc,
  123. uint32_t fftLen,
  124. const q15_t * pCoef)
  125. {
  126. uint32_t i;
  127. uint32_t n2;
  128. q15_t p0, p1, p2, p3;
  129. #if defined (ARM_MATH_DSP)
  130. q31_t T, S, R;
  131. q31_t coeff, out1, out2;
  132. const q15_t *pC = pCoef;
  133. q15_t *pSi = pSrc;
  134. q15_t *pSl = pSrc + fftLen;
  135. #else
  136. uint32_t l;
  137. q15_t xt, yt, cosVal, sinVal;
  138. #endif
  139. n2 = fftLen >> 1U;
  140. #if defined (ARM_MATH_DSP)
  141. for (i = n2; i > 0; i--)
  142. {
  143. coeff = read_q15x2_ia ((q15_t **) &pC);
  144. T = read_q15x2 (pSi);
  145. T = __SHADD16(T, 0); /* this is just a SIMD arithmetic shift right by 1 */
  146. S = read_q15x2 (pSl);
  147. S = __SHADD16(S, 0); /* this is just a SIMD arithmetic shift right by 1 */
  148. R = __QSUB16(T, S);
  149. write_q15x2_ia (&pSi, __SHADD16(T, S));
  150. #ifndef ARM_MATH_BIG_ENDIAN
  151. out1 = __SMUAD(coeff, R) >> 16U;
  152. out2 = __SMUSDX(coeff, R);
  153. #else
  154. out1 = __SMUSDX(R, coeff) >> 16U;
  155. out2 = __SMUAD(coeff, R);
  156. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  157. write_q15x2_ia (&pSl, (q31_t) ((out2) & 0xFFFF0000) | (out1 & 0x0000FFFF));
  158. }
  159. #else /* #if defined (ARM_MATH_DSP) */
  160. for (i = 0; i < n2; i++)
  161. {
  162. cosVal = pCoef[2 * i];
  163. sinVal = pCoef[2 * i + 1];
  164. l = i + n2;
  165. xt = (pSrc[2 * i] >> 1U) - (pSrc[2 * l] >> 1U);
  166. pSrc[2 * i] = ((pSrc[2 * i] >> 1U) + (pSrc[2 * l] >> 1U)) >> 1U;
  167. yt = (pSrc[2 * i + 1] >> 1U) - (pSrc[2 * l + 1] >> 1U);
  168. pSrc[2 * i + 1] = ((pSrc[2 * l + 1] >> 1U) + (pSrc[2 * i + 1] >> 1U)) >> 1U;
  169. pSrc[2 * l] = (((int16_t) (((q31_t) xt * cosVal) >> 16U)) +
  170. ((int16_t) (((q31_t) yt * sinVal) >> 16U)) );
  171. pSrc[2 * l + 1] = (((int16_t) (((q31_t) yt * cosVal) >> 16U)) -
  172. ((int16_t) (((q31_t) xt * sinVal) >> 16U)) );
  173. }
  174. #endif /* #if defined (ARM_MATH_DSP) */
  175. /* first col */
  176. arm_radix4_butterfly_q15( pSrc, n2, (q15_t*)pCoef, 2U);
  177. /* second col */
  178. arm_radix4_butterfly_q15( pSrc + fftLen, n2, (q15_t*)pCoef, 2U);
  179. n2 = fftLen >> 1U;
  180. for (i = 0; i < n2; i++)
  181. {
  182. p0 = pSrc[4 * i + 0];
  183. p1 = pSrc[4 * i + 1];
  184. p2 = pSrc[4 * i + 2];
  185. p3 = pSrc[4 * i + 3];
  186. p0 <<= 1U;
  187. p1 <<= 1U;
  188. p2 <<= 1U;
  189. p3 <<= 1U;
  190. pSrc[4 * i + 0] = p0;
  191. pSrc[4 * i + 1] = p1;
  192. pSrc[4 * i + 2] = p2;
  193. pSrc[4 * i + 3] = p3;
  194. }
  195. }
  196. void arm_cfft_radix4by2_inverse_q15(
  197. q15_t * pSrc,
  198. uint32_t fftLen,
  199. const q15_t * pCoef)
  200. {
  201. uint32_t i;
  202. uint32_t n2;
  203. q15_t p0, p1, p2, p3;
  204. #if defined (ARM_MATH_DSP)
  205. q31_t T, S, R;
  206. q31_t coeff, out1, out2;
  207. const q15_t *pC = pCoef;
  208. q15_t *pSi = pSrc;
  209. q15_t *pSl = pSrc + fftLen;
  210. #else
  211. uint32_t l;
  212. q15_t xt, yt, cosVal, sinVal;
  213. #endif
  214. n2 = fftLen >> 1U;
  215. #if defined (ARM_MATH_DSP)
  216. for (i = n2; i > 0; i--)
  217. {
  218. coeff = read_q15x2_ia ((q15_t **) &pC);
  219. T = read_q15x2 (pSi);
  220. T = __SHADD16(T, 0); /* this is just a SIMD arithmetic shift right by 1 */
  221. S = read_q15x2 (pSl);
  222. S = __SHADD16(S, 0); /* this is just a SIMD arithmetic shift right by 1 */
  223. R = __QSUB16(T, S);
  224. write_q15x2_ia (&pSi, __SHADD16(T, S));
  225. #ifndef ARM_MATH_BIG_ENDIAN
  226. out1 = __SMUSD(coeff, R) >> 16U;
  227. out2 = __SMUADX(coeff, R);
  228. #else
  229. out1 = __SMUADX(R, coeff) >> 16U;
  230. out2 = __SMUSD(__QSUB(0, coeff), R);
  231. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  232. write_q15x2_ia (&pSl, (q31_t) ((out2) & 0xFFFF0000) | (out1 & 0x0000FFFF));
  233. }
  234. #else /* #if defined (ARM_MATH_DSP) */
  235. for (i = 0; i < n2; i++)
  236. {
  237. cosVal = pCoef[2 * i];
  238. sinVal = pCoef[2 * i + 1];
  239. l = i + n2;
  240. xt = (pSrc[2 * i] >> 1U) - (pSrc[2 * l] >> 1U);
  241. pSrc[2 * i] = ((pSrc[2 * i] >> 1U) + (pSrc[2 * l] >> 1U)) >> 1U;
  242. yt = (pSrc[2 * i + 1] >> 1U) - (pSrc[2 * l + 1] >> 1U);
  243. pSrc[2 * i + 1] = ((pSrc[2 * l + 1] >> 1U) + (pSrc[2 * i + 1] >> 1U)) >> 1U;
  244. pSrc[2 * l] = (((int16_t) (((q31_t) xt * cosVal) >> 16U)) -
  245. ((int16_t) (((q31_t) yt * sinVal) >> 16U)) );
  246. pSrc[2 * l + 1] = (((int16_t) (((q31_t) yt * cosVal) >> 16U)) +
  247. ((int16_t) (((q31_t) xt * sinVal) >> 16U)) );
  248. }
  249. #endif /* #if defined (ARM_MATH_DSP) */
  250. /* first col */
  251. arm_radix4_butterfly_inverse_q15( pSrc, n2, (q15_t*)pCoef, 2U);
  252. /* second col */
  253. arm_radix4_butterfly_inverse_q15( pSrc + fftLen, n2, (q15_t*)pCoef, 2U);
  254. n2 = fftLen >> 1U;
  255. for (i = 0; i < n2; i++)
  256. {
  257. p0 = pSrc[4 * i + 0];
  258. p1 = pSrc[4 * i + 1];
  259. p2 = pSrc[4 * i + 2];
  260. p3 = pSrc[4 * i + 3];
  261. p0 <<= 1U;
  262. p1 <<= 1U;
  263. p2 <<= 1U;
  264. p3 <<= 1U;
  265. pSrc[4 * i + 0] = p0;
  266. pSrc[4 * i + 1] = p1;
  267. pSrc[4 * i + 2] = p2;
  268. pSrc[4 * i + 3] = p3;
  269. }
  270. }