arm_rfft_q31.c 9.0 KB

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