arm_rfft_q31.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_q31.c
  4. * Description: FFT & RIFFT Q31 process 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. /* ----------------------------------------------------------------------
  30. * Internal functions prototypes
  31. * -------------------------------------------------------------------- */
  32. void arm_split_rfft_q31(
  33. q31_t * pSrc,
  34. uint32_t fftLen,
  35. q31_t * pATable,
  36. q31_t * pBTable,
  37. q31_t * pDst,
  38. uint32_t modifier);
  39. void arm_split_rifft_q31(
  40. q31_t * pSrc,
  41. uint32_t fftLen,
  42. q31_t * pATable,
  43. q31_t * pBTable,
  44. q31_t * pDst,
  45. uint32_t modifier);
  46. /**
  47. * @addtogroup RealFFT
  48. * @{
  49. */
  50. /**
  51. * @brief Processing function for the Q31 RFFT/RIFFT.
  52. * @param[in] *S points to an instance of the Q31 RFFT/RIFFT structure.
  53. * @param[in] *pSrc points to the input buffer.
  54. * @param[out] *pDst points to the output buffer.
  55. * @return none.
  56. *
  57. * \par Input an output formats:
  58. * \par
  59. * Internally input is downscaled by 2 for every stage to avoid saturations inside CFFT/CIFFT process.
  60. * Hence the output format is different for different RFFT sizes.
  61. * The input and output formats for different RFFT sizes and number of bits to upscale are mentioned in the tables below for RFFT and RIFFT:
  62. * \par
  63. * \image html RFFTQ31.gif "Input and Output Formats for Q31 RFFT"
  64. *
  65. * \par
  66. * \image html RIFFTQ31.gif "Input and Output Formats for Q31 RIFFT"
  67. */
  68. void arm_rfft_q31(
  69. const arm_rfft_instance_q31 * S,
  70. q31_t * pSrc,
  71. q31_t * pDst)
  72. {
  73. const arm_cfft_instance_q31 *S_CFFT = S->pCfft;
  74. uint32_t i;
  75. uint32_t L2 = S->fftLenReal >> 1;
  76. /* Calculation of RIFFT of input */
  77. if (S->ifftFlagR == 1U)
  78. {
  79. /* Real IFFT core process */
  80. arm_split_rifft_q31(pSrc, L2, S->pTwiddleAReal,
  81. S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  82. /* Complex IFFT process */
  83. arm_cfft_q31(S_CFFT, pDst, S->ifftFlagR, S->bitReverseFlagR);
  84. for(i=0;i<S->fftLenReal;i++)
  85. {
  86. pDst[i] = pDst[i] << 1;
  87. }
  88. }
  89. else
  90. {
  91. /* Calculation of RFFT of input */
  92. /* Complex FFT process */
  93. arm_cfft_q31(S_CFFT, pSrc, S->ifftFlagR, S->bitReverseFlagR);
  94. /* Real FFT core process */
  95. arm_split_rfft_q31(pSrc, L2, S->pTwiddleAReal,
  96. S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  97. }
  98. }
  99. /**
  100. * @} end of RealFFT group
  101. */
  102. /**
  103. * @brief Core Real FFT process
  104. * @param[in] *pSrc points to the input buffer.
  105. * @param[in] fftLen length of FFT.
  106. * @param[in] *pATable points to the twiddle Coef A buffer.
  107. * @param[in] *pBTable points to the twiddle Coef B buffer.
  108. * @param[out] *pDst points to the output buffer.
  109. * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
  110. * @return none.
  111. */
  112. void arm_split_rfft_q31(
  113. q31_t * pSrc,
  114. uint32_t fftLen,
  115. q31_t * pATable,
  116. q31_t * pBTable,
  117. q31_t * pDst,
  118. uint32_t modifier)
  119. {
  120. uint32_t i; /* Loop Counter */
  121. q31_t outR, outI; /* Temporary variables for output */
  122. q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  123. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  124. q31_t *pOut1 = &pDst[2], *pOut2 = &pDst[(4U * fftLen) - 1U];
  125. q31_t *pIn1 = &pSrc[2], *pIn2 = &pSrc[(2U * fftLen) - 1U];
  126. /* Init coefficient pointers */
  127. pCoefA = &pATable[modifier * 2U];
  128. pCoefB = &pBTable[modifier * 2U];
  129. i = fftLen - 1U;
  130. while (i > 0U)
  131. {
  132. /*
  133. outR = (pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1]
  134. + pSrc[2 * n - 2 * i] * pBTable[2 * i] +
  135. pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  136. */
  137. /* outI = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] +
  138. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  139. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]); */
  140. CoefA1 = *pCoefA++;
  141. CoefA2 = *pCoefA;
  142. /* outR = (pSrc[2 * i] * pATable[2 * i] */
  143. mult_32x32_keep32_R(outR, *pIn1, CoefA1);
  144. /* outI = pIn[2 * i] * pATable[2 * i + 1] */
  145. mult_32x32_keep32_R(outI, *pIn1++, CoefA2);
  146. /* - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  147. multSub_32x32_keep32_R(outR, *pIn1, CoefA2);
  148. /* (pIn[2 * i + 1] * pATable[2 * i] */
  149. multAcc_32x32_keep32_R(outI, *pIn1++, CoefA1);
  150. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] */
  151. multSub_32x32_keep32_R(outR, *pIn2, CoefA2);
  152. CoefB1 = *pCoefB;
  153. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  154. multSub_32x32_keep32_R(outI, *pIn2--, CoefB1);
  155. /* pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  156. multAcc_32x32_keep32_R(outR, *pIn2, CoefB1);
  157. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  158. multSub_32x32_keep32_R(outI, *pIn2--, CoefA2);
  159. /* write output */
  160. *pOut1++ = outR;
  161. *pOut1++ = outI;
  162. /* write complex conjugate output */
  163. *pOut2-- = -outI;
  164. *pOut2-- = outR;
  165. /* update coefficient pointer */
  166. pCoefB = pCoefB + (modifier * 2U);
  167. pCoefA = pCoefA + ((modifier * 2U) - 1U);
  168. i--;
  169. }
  170. pDst[2U * fftLen] = (pSrc[0] - pSrc[1]) >> 1;
  171. pDst[(2U * fftLen) + 1U] = 0;
  172. pDst[0] = (pSrc[0] + pSrc[1]) >> 1;
  173. pDst[1] = 0;
  174. }
  175. /**
  176. * @brief Core Real IFFT process
  177. * @param[in] *pSrc points to the input buffer.
  178. * @param[in] fftLen length of FFT.
  179. * @param[in] *pATable points to the twiddle Coef A buffer.
  180. * @param[in] *pBTable points to the twiddle Coef B buffer.
  181. * @param[out] *pDst points to the output buffer.
  182. * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
  183. * @return none.
  184. */
  185. void arm_split_rifft_q31(
  186. q31_t * pSrc,
  187. uint32_t fftLen,
  188. q31_t * pATable,
  189. q31_t * pBTable,
  190. q31_t * pDst,
  191. uint32_t modifier)
  192. {
  193. q31_t outR, outI; /* Temporary variables for output */
  194. q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  195. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  196. q31_t *pIn1 = &pSrc[0], *pIn2 = &pSrc[(2U * fftLen) + 1U];
  197. pCoefA = &pATable[0];
  198. pCoefB = &pBTable[0];
  199. while (fftLen > 0U)
  200. {
  201. /*
  202. outR = (pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
  203. pIn[2 * n - 2 * i] * pBTable[2 * i] -
  204. pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  205. outI = (pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] -
  206. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  207. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  208. */
  209. CoefA1 = *pCoefA++;
  210. CoefA2 = *pCoefA;
  211. /* outR = (pIn[2 * i] * pATable[2 * i] */
  212. mult_32x32_keep32_R(outR, *pIn1, CoefA1);
  213. /* - pIn[2 * i] * pATable[2 * i + 1] */
  214. mult_32x32_keep32_R(outI, *pIn1++, -CoefA2);
  215. /* pIn[2 * i + 1] * pATable[2 * i + 1] */
  216. multAcc_32x32_keep32_R(outR, *pIn1, CoefA2);
  217. /* pIn[2 * i + 1] * pATable[2 * i] */
  218. multAcc_32x32_keep32_R(outI, *pIn1++, CoefA1);
  219. /* pIn[2 * n - 2 * i] * pBTable[2 * i] */
  220. multAcc_32x32_keep32_R(outR, *pIn2, CoefA2);
  221. CoefB1 = *pCoefB;
  222. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  223. multSub_32x32_keep32_R(outI, *pIn2--, CoefB1);
  224. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  225. multAcc_32x32_keep32_R(outR, *pIn2, CoefB1);
  226. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  227. multAcc_32x32_keep32_R(outI, *pIn2--, CoefA2);
  228. /* write output */
  229. *pDst++ = outR;
  230. *pDst++ = outI;
  231. /* update coefficient pointer */
  232. pCoefB = pCoefB + (modifier * 2U);
  233. pCoefA = pCoefA + ((modifier * 2U) - 1U);
  234. /* Decrement loop count */
  235. fftLen--;
  236. }
  237. }