arm_rfft_q31.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_q31.c
  4. * Description: FFT & RIFFT Q31 process function
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 "dsp/transform_functions.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 (Source buffer is modified by this function)
  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. @par
  65. If the input buffer is of length N, the output buffer must have length 2*N.
  66. The input buffer is modified by this function.
  67. @par
  68. For the RIFFT, the source buffer must at least have length
  69. fftLenReal + 2.
  70. The last two elements must be equal to what would be generated
  71. by the RFFT:
  72. (pSrc[0] - pSrc[1]) >> 1 and 0
  73. */
  74. void arm_rfft_q31(
  75. const arm_rfft_instance_q31 * S,
  76. q31_t * pSrc,
  77. q31_t * pDst)
  78. {
  79. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  80. const arm_cfft_instance_q31 *S_CFFT = &(S->cfftInst);
  81. #else
  82. const arm_cfft_instance_q31 *S_CFFT = S->pCfft;
  83. #endif
  84. uint32_t L2 = S->fftLenReal >> 1U;
  85. /* Calculation of RIFFT of input */
  86. if (S->ifftFlagR == 1U)
  87. {
  88. /* Real IFFT core process */
  89. arm_split_rifft_q31 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  90. /* Complex IFFT process */
  91. arm_cfft_q31 (S_CFFT, pDst, S->ifftFlagR, S->bitReverseFlagR);
  92. arm_shift_q31(pDst, 1, pDst, S->fftLenReal);
  93. }
  94. else
  95. {
  96. /* Calculation of RFFT of input */
  97. /* Complex FFT process */
  98. arm_cfft_q31 (S_CFFT, pSrc, S->ifftFlagR, S->bitReverseFlagR);
  99. /* Real FFT core process */
  100. arm_split_rfft_q31 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  101. }
  102. }
  103. /**
  104. @} end of RealFFT group
  105. */
  106. /**
  107. @brief Core Real FFT process
  108. @param[in] pSrc points to input buffer
  109. @param[in] fftLen length of FFT
  110. @param[in] pATable points to twiddle Coef A buffer
  111. @param[in] pBTable points to twiddle Coef B buffer
  112. @param[out] pDst points to output buffer
  113. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  114. @return none
  115. */
  116. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  117. #include "arm_helium_utils.h"
  118. #include "arm_vec_fft.h"
  119. #if defined(__CMSIS_GCC_H)
  120. #define MVE_CMPLX_MULT_FX_AxB_S32(A,B) vqdmladhxq_s32(vqdmlsdhq_s32((__typeof(A))vuninitializedq_s32(), A, B), A, B)
  121. #define MVE_CMPLX_MULT_FX_AxConjB_S32(A,B) vqdmladhq_s32(vqdmlsdhxq_s32((__typeof(A))vuninitializedq_s32(), A, B), A, B)
  122. #endif
  123. void arm_split_rfft_q31(
  124. q31_t *pSrc,
  125. uint32_t fftLen,
  126. const q31_t *pATable,
  127. const q31_t *pBTable,
  128. q31_t *pDst,
  129. uint32_t modifier)
  130. {
  131. uint32_t i; /* Loop Counter */
  132. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  133. q31_t *pOut1 = &pDst[2];
  134. q31_t *pIn1 = &pSrc[2];
  135. uint32x4_t offset = { 2, 3, 0, 1 };
  136. uint32x4_t offsetCoef = { 0, 1, modifier * 2, modifier * 2 + 1 };
  137. offset = offset + (2 * fftLen - 4);
  138. /* Init coefficient pointers */
  139. pCoefA = &pATable[modifier * 2];
  140. pCoefB = &pBTable[modifier * 2];
  141. const q31_t *pCoefAb, *pCoefBb;
  142. pCoefAb = pCoefA;
  143. pCoefBb = pCoefB;
  144. pIn1 = &pSrc[2];
  145. i = fftLen - 1U;
  146. i = i / 2 + 1;
  147. while (i > 0U) {
  148. q31x4_t in1 = vld1q_s32(pIn1);
  149. q31x4_t in2 = vldrwq_gather_shifted_offset_s32(pSrc, offset);
  150. q31x4_t coefA = vldrwq_gather_shifted_offset_s32(pCoefAb, offsetCoef);
  151. q31x4_t coefB = vldrwq_gather_shifted_offset_s32(pCoefBb, offsetCoef);
  152. #if defined(__CMSIS_GCC_H)
  153. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxB_S32(in1, coefA),MVE_CMPLX_MULT_FX_AxConjB_S32(coefB, in2));
  154. #else
  155. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxB(in1, coefA, q31x4_t),
  156. MVE_CMPLX_MULT_FX_AxConjB(coefB, in2, q31x4_t));
  157. #endif
  158. vst1q(pOut1, out);
  159. pOut1 += 4;
  160. offsetCoef += modifier * 4;
  161. offset -= 4;
  162. pIn1 += 4;
  163. i -= 1;
  164. }
  165. pDst[2 * fftLen] = (pSrc[0] - pSrc[1]) >> 1U;
  166. pDst[2 * fftLen + 1] = 0;
  167. pDst[0] = (pSrc[0] + pSrc[1]) >> 1U;
  168. pDst[1] = 0;
  169. }
  170. #else
  171. void arm_split_rfft_q31(
  172. q31_t * pSrc,
  173. uint32_t fftLen,
  174. const q31_t * pATable,
  175. const q31_t * pBTable,
  176. q31_t * pDst,
  177. uint32_t modifier)
  178. {
  179. uint32_t i; /* Loop Counter */
  180. q31_t outR, outI; /* Temporary variables for output */
  181. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  182. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  183. q31_t *pOut1 = &pDst[2], *pOut2 = &pDst[4 * fftLen - 1];
  184. q31_t *pIn1 = &pSrc[2], *pIn2 = &pSrc[2 * fftLen - 1];
  185. /* Init coefficient pointers */
  186. pCoefA = &pATable[modifier * 2];
  187. pCoefB = &pBTable[modifier * 2];
  188. i = fftLen - 1U;
  189. while (i > 0U)
  190. {
  191. /*
  192. outR = ( pSrc[2 * i] * pATable[2 * i]
  193. - pSrc[2 * i + 1] * pATable[2 * i + 1]
  194. + pSrc[2 * n - 2 * i] * pBTable[2 * i]
  195. + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  196. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  197. + pIn[2 * i] * pATable[2 * i + 1]
  198. + pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  199. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  200. */
  201. CoefA1 = *pCoefA++;
  202. CoefA2 = *pCoefA;
  203. /* outR = (pSrc[2 * i] * pATable[2 * i] */
  204. mult_32x32_keep32_R (outR, *pIn1, CoefA1);
  205. /* outI = pIn[2 * i] * pATable[2 * i + 1] */
  206. mult_32x32_keep32_R (outI, *pIn1++, CoefA2);
  207. /* - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  208. multSub_32x32_keep32_R (outR, *pIn1, CoefA2);
  209. /* (pIn[2 * i + 1] * pATable[2 * i] */
  210. multAcc_32x32_keep32_R (outI, *pIn1++, CoefA1);
  211. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] */
  212. multSub_32x32_keep32_R (outR, *pIn2, CoefA2);
  213. CoefB1 = *pCoefB;
  214. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  215. multSub_32x32_keep32_R (outI, *pIn2--, CoefB1);
  216. /* pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  217. multAcc_32x32_keep32_R (outR, *pIn2, CoefB1);
  218. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  219. multSub_32x32_keep32_R (outI, *pIn2--, CoefA2);
  220. /* write output */
  221. *pOut1++ = outR;
  222. *pOut1++ = outI;
  223. /* write complex conjugate output */
  224. *pOut2-- = -outI;
  225. *pOut2-- = outR;
  226. /* update coefficient pointer */
  227. pCoefB = pCoefB + (2 * modifier);
  228. pCoefA = pCoefA + (2 * modifier - 1);
  229. /* Decrement loop count */
  230. i--;
  231. }
  232. pDst[2 * fftLen] = (pSrc[0] - pSrc[1]) >> 1U;
  233. pDst[2 * fftLen + 1] = 0;
  234. pDst[0] = (pSrc[0] + pSrc[1]) >> 1U;
  235. pDst[1] = 0;
  236. }
  237. #endif /* defined(ARM_MATH_MVEI) */
  238. /**
  239. @brief Core Real IFFT process
  240. @param[in] pSrc points to input buffer
  241. @param[in] fftLen length of FFT
  242. @param[in] pATable points to twiddle Coef A buffer
  243. @param[in] pBTable points to twiddle Coef B buffer
  244. @param[out] pDst points to output buffer
  245. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  246. @return none
  247. */
  248. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  249. void arm_split_rifft_q31(
  250. q31_t * pSrc,
  251. uint32_t fftLen,
  252. const q31_t * pATable,
  253. const q31_t * pBTable,
  254. q31_t * pDst,
  255. uint32_t modifier)
  256. {
  257. uint32_t i; /* Loop Counter */
  258. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  259. q31_t *pIn1;
  260. uint32x4_t offset = { 2, 3, 0, 1 };
  261. uint32x4_t offsetCoef = { 0, 1, modifier * 2, modifier * 2 + 1 };
  262. int32x4_t conj = { 1, -1, 1, -1 };
  263. offset = offset + (2 * fftLen - 2);
  264. /* Init coefficient pointers */
  265. pCoefA = &pATable[0];
  266. pCoefB = &pBTable[0];
  267. const q31_t *pCoefAb, *pCoefBb;
  268. pCoefAb = pCoefA;
  269. pCoefBb = pCoefB;
  270. pIn1 = &pSrc[0];
  271. i = fftLen;
  272. i = i >> 1;
  273. while (i > 0U) {
  274. q31x4_t in1 = vld1q_s32(pIn1);
  275. q31x4_t in2 = vldrwq_gather_shifted_offset_s32(pSrc, offset);
  276. q31x4_t coefA = vldrwq_gather_shifted_offset_s32(pCoefAb, offsetCoef);
  277. q31x4_t coefB = vldrwq_gather_shifted_offset_s32(pCoefBb, offsetCoef);
  278. /* can we avoid the conjugate here ? */
  279. #if defined(__CMSIS_GCC_H)
  280. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxConjB_S32(in1, coefA),
  281. vmulq_s32(conj, MVE_CMPLX_MULT_FX_AxB_S32(in2, coefB)));
  282. #else
  283. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxConjB(in1, coefA, q31x4_t),
  284. vmulq_s32(conj, MVE_CMPLX_MULT_FX_AxB(in2, coefB, q31x4_t)));
  285. #endif
  286. vst1q_s32(pDst, out);
  287. pDst += 4;
  288. offsetCoef += modifier * 4;
  289. offset -= 4;
  290. pIn1 += 4;
  291. i -= 1;
  292. }
  293. }
  294. #else
  295. void arm_split_rifft_q31(
  296. q31_t * pSrc,
  297. uint32_t fftLen,
  298. const q31_t * pATable,
  299. const q31_t * pBTable,
  300. q31_t * pDst,
  301. uint32_t modifier)
  302. {
  303. q31_t outR, outI; /* Temporary variables for output */
  304. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  305. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  306. q31_t *pIn1 = &pSrc[0], *pIn2 = &pSrc[2 * fftLen + 1];
  307. pCoefA = &pATable[0];
  308. pCoefB = &pBTable[0];
  309. while (fftLen > 0U)
  310. {
  311. /*
  312. outR = ( pIn[2 * i] * pATable[2 * i]
  313. + pIn[2 * i + 1] * pATable[2 * i + 1]
  314. + pIn[2 * n - 2 * i] * pBTable[2 * i]
  315. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  316. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  317. - pIn[2 * i] * pATable[2 * i + 1]
  318. - pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  319. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  320. */
  321. CoefA1 = *pCoefA++;
  322. CoefA2 = *pCoefA;
  323. /* outR = (pIn[2 * i] * pATable[2 * i] */
  324. mult_32x32_keep32_R (outR, *pIn1, CoefA1);
  325. /* - pIn[2 * i] * pATable[2 * i + 1] */
  326. mult_32x32_keep32_R (outI, *pIn1++, -CoefA2);
  327. /* pIn[2 * i + 1] * pATable[2 * i + 1] */
  328. multAcc_32x32_keep32_R (outR, *pIn1, CoefA2);
  329. /* pIn[2 * i + 1] * pATable[2 * i] */
  330. multAcc_32x32_keep32_R (outI, *pIn1++, CoefA1);
  331. /* pIn[2 * n - 2 * i] * pBTable[2 * i] */
  332. multAcc_32x32_keep32_R (outR, *pIn2, CoefA2);
  333. CoefB1 = *pCoefB;
  334. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  335. multSub_32x32_keep32_R (outI, *pIn2--, CoefB1);
  336. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  337. multAcc_32x32_keep32_R (outR, *pIn2, CoefB1);
  338. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  339. multAcc_32x32_keep32_R (outI, *pIn2--, CoefA2);
  340. /* write output */
  341. *pDst++ = outR;
  342. *pDst++ = outI;
  343. /* update coefficient pointer */
  344. pCoefB = pCoefB + (modifier * 2);
  345. pCoefA = pCoefA + (modifier * 2 - 1);
  346. /* Decrement loop count */
  347. fftLen--;
  348. }
  349. }
  350. #endif /* defined(ARM_MATH_MVEI) */