arm_rfft_q31.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 RealFFTQ31
  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. @par Input an output formats
  56. Internally input is downscaled by 2 for every stage to avoid saturations inside CFFT/CIFFT process.
  57. Hence the output format is different for different RFFT sizes.
  58. 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:
  59. @par Input and Output formats for RFFT Q31
  60. | RFFT Size | Input Format | Output Format | Number of bits to upscale |
  61. | ---------: | ------------: | -------------: | ------------------------: |
  62. | 32 | 1.31 | 6.26 | 5 |
  63. | 64 | 1.31 | 7.25 | 6 |
  64. | 128 | 1.31 | 8.24 | 7 |
  65. | 256 | 1.31 | 9.23 | 8 |
  66. | 512 | 1.31 | 10.22 | 9 |
  67. | 1024 | 1.31 | 11.21 | 10 |
  68. | 2048 | 1.31 | 12.20 | 11 |
  69. | 4096 | 1.31 | 13.19 | 12 |
  70. | 8192 | 1.31 | 14.18 | 13 |
  71. @par Input and Output formats for RIFFT Q31
  72. | RIFFT Size | Input Format | Output Format | Number of bits to upscale |
  73. | ----------: | ------------: | -------------: | ------------------------: |
  74. | 32 | 1.31 | 6.26 | 0 |
  75. | 64 | 1.31 | 7.25 | 0 |
  76. | 128 | 1.31 | 8.24 | 0 |
  77. | 256 | 1.31 | 9.23 | 0 |
  78. | 512 | 1.31 | 10.22 | 0 |
  79. | 1024 | 1.31 | 11.21 | 0 |
  80. | 2048 | 1.31 | 12.20 | 0 |
  81. | 4096 | 1.31 | 13.19 | 0 |
  82. | 8192 | 1.31 | 14.18 | 0 |
  83. @par
  84. If the input buffer is of length N (fftLenReal), the output buffer must have length 2N
  85. since it is containing the conjugate part (except for MVE version where N+2 is enough).
  86. The input buffer is modified by this function.
  87. @par
  88. For the RIFFT, the source buffer must have length N+2 since the Nyquist frequency value
  89. is needed but conjugate part is ignored.
  90. It is not using the packing trick of the float version.
  91. */
  92. void arm_rfft_q31(
  93. const arm_rfft_instance_q31 * S,
  94. q31_t * pSrc,
  95. q31_t * pDst)
  96. {
  97. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  98. const arm_cfft_instance_q31 *S_CFFT = &(S->cfftInst);
  99. #else
  100. const arm_cfft_instance_q31 *S_CFFT = S->pCfft;
  101. #endif
  102. uint32_t L2 = S->fftLenReal >> 1U;
  103. /* Calculation of RIFFT of input */
  104. if (S->ifftFlagR == 1U)
  105. {
  106. /* Real IFFT core process */
  107. arm_split_rifft_q31 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  108. /* Complex IFFT process */
  109. arm_cfft_q31 (S_CFFT, pDst, S->ifftFlagR, S->bitReverseFlagR);
  110. arm_shift_q31(pDst, 1, pDst, S->fftLenReal);
  111. }
  112. else
  113. {
  114. /* Calculation of RFFT of input */
  115. /* Complex FFT process */
  116. arm_cfft_q31 (S_CFFT, pSrc, S->ifftFlagR, S->bitReverseFlagR);
  117. /* Real FFT core process */
  118. arm_split_rfft_q31 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  119. }
  120. }
  121. /**
  122. @} end of RealFFTQ31 group
  123. */
  124. /**
  125. @brief Core Real FFT process
  126. @param[in] pSrc points to input buffer
  127. @param[in] fftLen length of FFT
  128. @param[in] pATable points to twiddle Coef A buffer
  129. @param[in] pBTable points to twiddle Coef B buffer
  130. @param[out] pDst points to output buffer
  131. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  132. */
  133. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  134. #include "arm_helium_utils.h"
  135. #include "arm_vec_fft.h"
  136. #if defined(__CMSIS_GCC_H)
  137. #define MVE_CMPLX_MULT_FX_AxB_S32(A,B) vqdmladhxq_s32(vqdmlsdhq_s32((__typeof(A))vuninitializedq_s32(), A, B), A, B)
  138. #define MVE_CMPLX_MULT_FX_AxConjB_S32(A,B) vqdmladhq_s32(vqdmlsdhxq_s32((__typeof(A))vuninitializedq_s32(), A, B), A, B)
  139. #endif
  140. void arm_split_rfft_q31(
  141. q31_t *pSrc,
  142. uint32_t fftLen,
  143. const q31_t *pATable,
  144. const q31_t *pBTable,
  145. q31_t *pDst,
  146. uint32_t modifier)
  147. {
  148. uint32_t i; /* Loop Counter */
  149. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  150. q31_t *pOut1 = &pDst[2];
  151. q31_t *pIn1 = &pSrc[2];
  152. uint32x4_t offset = { 2, 3, 0, 1 };
  153. uint32x4_t offsetCoef = { 0, 1, modifier * 2, modifier * 2 + 1 };
  154. offset = offset + (2 * fftLen - 4);
  155. /* Init coefficient pointers */
  156. pCoefA = &pATable[modifier * 2];
  157. pCoefB = &pBTable[modifier * 2];
  158. const q31_t *pCoefAb, *pCoefBb;
  159. pCoefAb = pCoefA;
  160. pCoefBb = pCoefB;
  161. pIn1 = &pSrc[2];
  162. i = fftLen - 1U;
  163. i = i / 2 + 1;
  164. while (i > 0U) {
  165. q31x4_t in1 = vld1q_s32(pIn1);
  166. q31x4_t in2 = vldrwq_gather_shifted_offset_s32(pSrc, offset);
  167. q31x4_t coefA = vldrwq_gather_shifted_offset_s32(pCoefAb, offsetCoef);
  168. q31x4_t coefB = vldrwq_gather_shifted_offset_s32(pCoefBb, offsetCoef);
  169. #if defined(__CMSIS_GCC_H)
  170. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxB_S32(in1, coefA),MVE_CMPLX_MULT_FX_AxConjB_S32(coefB, in2));
  171. #else
  172. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxB(in1, coefA, q31x4_t),
  173. MVE_CMPLX_MULT_FX_AxConjB(coefB, in2, q31x4_t));
  174. #endif
  175. vst1q(pOut1, out);
  176. pOut1 += 4;
  177. offsetCoef += modifier * 4;
  178. offset -= 4;
  179. pIn1 += 4;
  180. i -= 1;
  181. }
  182. pDst[2 * fftLen] = (pSrc[0] - pSrc[1]) >> 1U;
  183. pDst[2 * fftLen + 1] = 0;
  184. pDst[0] = (pSrc[0] + pSrc[1]) >> 1U;
  185. pDst[1] = 0;
  186. }
  187. #else
  188. void arm_split_rfft_q31(
  189. q31_t * pSrc,
  190. uint32_t fftLen,
  191. const q31_t * pATable,
  192. const q31_t * pBTable,
  193. q31_t * pDst,
  194. uint32_t modifier)
  195. {
  196. uint32_t i; /* Loop Counter */
  197. q31_t outR, outI; /* Temporary variables for output */
  198. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  199. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  200. q31_t *pOut1 = &pDst[2], *pOut2 = &pDst[4 * fftLen - 1];
  201. q31_t *pIn1 = &pSrc[2], *pIn2 = &pSrc[2 * fftLen - 1];
  202. /* Init coefficient pointers */
  203. pCoefA = &pATable[modifier * 2];
  204. pCoefB = &pBTable[modifier * 2];
  205. i = fftLen - 1U;
  206. while (i > 0U)
  207. {
  208. /*
  209. outR = ( pSrc[2 * i] * pATable[2 * i]
  210. - pSrc[2 * i + 1] * pATable[2 * i + 1]
  211. + pSrc[2 * n - 2 * i] * pBTable[2 * i]
  212. + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  213. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  214. + pIn[2 * i] * pATable[2 * i + 1]
  215. + pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  216. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  217. */
  218. CoefA1 = *pCoefA++;
  219. CoefA2 = *pCoefA;
  220. /* outR = (pSrc[2 * i] * pATable[2 * i] */
  221. mult_32x32_keep32_R (outR, *pIn1, CoefA1);
  222. /* outI = pIn[2 * i] * pATable[2 * i + 1] */
  223. mult_32x32_keep32_R (outI, *pIn1++, CoefA2);
  224. /* - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  225. multSub_32x32_keep32_R (outR, *pIn1, CoefA2);
  226. /* (pIn[2 * i + 1] * pATable[2 * i] */
  227. multAcc_32x32_keep32_R (outI, *pIn1++, CoefA1);
  228. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] */
  229. multSub_32x32_keep32_R (outR, *pIn2, CoefA2);
  230. CoefB1 = *pCoefB;
  231. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  232. multSub_32x32_keep32_R (outI, *pIn2--, CoefB1);
  233. /* pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  234. multAcc_32x32_keep32_R (outR, *pIn2, CoefB1);
  235. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  236. multSub_32x32_keep32_R (outI, *pIn2--, CoefA2);
  237. /* write output */
  238. *pOut1++ = outR;
  239. *pOut1++ = outI;
  240. /* write complex conjugate output */
  241. *pOut2-- = -outI;
  242. *pOut2-- = outR;
  243. /* update coefficient pointer */
  244. pCoefB = pCoefB + (2 * modifier);
  245. pCoefA = pCoefA + (2 * modifier - 1);
  246. /* Decrement loop count */
  247. i--;
  248. }
  249. pDst[2 * fftLen] = (pSrc[0] - pSrc[1]) >> 1U;
  250. pDst[2 * fftLen + 1] = 0;
  251. pDst[0] = (pSrc[0] + pSrc[1]) >> 1U;
  252. pDst[1] = 0;
  253. }
  254. #endif /* defined(ARM_MATH_MVEI) */
  255. /**
  256. @brief Core Real IFFT process
  257. @param[in] pSrc points to input buffer
  258. @param[in] fftLen length of FFT
  259. @param[in] pATable points to twiddle Coef A buffer
  260. @param[in] pBTable points to twiddle Coef B buffer
  261. @param[out] pDst points to output buffer
  262. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  263. */
  264. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  265. void arm_split_rifft_q31(
  266. q31_t * pSrc,
  267. uint32_t fftLen,
  268. const q31_t * pATable,
  269. const q31_t * pBTable,
  270. q31_t * pDst,
  271. uint32_t modifier)
  272. {
  273. uint32_t i; /* Loop Counter */
  274. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  275. q31_t *pIn1;
  276. uint32x4_t offset = { 2, 3, 0, 1 };
  277. uint32x4_t offsetCoef = { 0, 1, modifier * 2, modifier * 2 + 1 };
  278. int32x4_t conj = { 1, -1, 1, -1 };
  279. offset = offset + (2 * fftLen - 2);
  280. /* Init coefficient pointers */
  281. pCoefA = &pATable[0];
  282. pCoefB = &pBTable[0];
  283. const q31_t *pCoefAb, *pCoefBb;
  284. pCoefAb = pCoefA;
  285. pCoefBb = pCoefB;
  286. pIn1 = &pSrc[0];
  287. i = fftLen;
  288. i = i >> 1;
  289. while (i > 0U) {
  290. q31x4_t in1 = vld1q_s32(pIn1);
  291. q31x4_t in2 = vldrwq_gather_shifted_offset_s32(pSrc, offset);
  292. q31x4_t coefA = vldrwq_gather_shifted_offset_s32(pCoefAb, offsetCoef);
  293. q31x4_t coefB = vldrwq_gather_shifted_offset_s32(pCoefBb, offsetCoef);
  294. /* can we avoid the conjugate here ? */
  295. #if defined(__CMSIS_GCC_H)
  296. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxConjB_S32(in1, coefA),
  297. vmulq_s32(conj, MVE_CMPLX_MULT_FX_AxB_S32(in2, coefB)));
  298. #else
  299. q31x4_t out = vhaddq_s32(MVE_CMPLX_MULT_FX_AxConjB(in1, coefA, q31x4_t),
  300. vmulq_s32(conj, MVE_CMPLX_MULT_FX_AxB(in2, coefB, q31x4_t)));
  301. #endif
  302. vst1q_s32(pDst, out);
  303. pDst += 4;
  304. offsetCoef += modifier * 4;
  305. offset -= 4;
  306. pIn1 += 4;
  307. i -= 1;
  308. }
  309. }
  310. #else
  311. void arm_split_rifft_q31(
  312. q31_t * pSrc,
  313. uint32_t fftLen,
  314. const q31_t * pATable,
  315. const q31_t * pBTable,
  316. q31_t * pDst,
  317. uint32_t modifier)
  318. {
  319. q31_t outR, outI; /* Temporary variables for output */
  320. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  321. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  322. q31_t *pIn1 = &pSrc[0], *pIn2 = &pSrc[2 * fftLen + 1];
  323. pCoefA = &pATable[0];
  324. pCoefB = &pBTable[0];
  325. while (fftLen > 0U)
  326. {
  327. /*
  328. outR = ( pIn[2 * i] * pATable[2 * i]
  329. + pIn[2 * i + 1] * pATable[2 * i + 1]
  330. + pIn[2 * n - 2 * i] * pBTable[2 * i]
  331. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  332. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  333. - pIn[2 * i] * pATable[2 * i + 1]
  334. - pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  335. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  336. */
  337. CoefA1 = *pCoefA++;
  338. CoefA2 = *pCoefA;
  339. /* outR = (pIn[2 * i] * pATable[2 * i] */
  340. mult_32x32_keep32_R (outR, *pIn1, CoefA1);
  341. /* - pIn[2 * i] * pATable[2 * i + 1] */
  342. mult_32x32_keep32_R (outI, *pIn1++, -CoefA2);
  343. /* pIn[2 * i + 1] * pATable[2 * i + 1] */
  344. multAcc_32x32_keep32_R (outR, *pIn1, CoefA2);
  345. /* pIn[2 * i + 1] * pATable[2 * i] */
  346. multAcc_32x32_keep32_R (outI, *pIn1++, CoefA1);
  347. /* pIn[2 * n - 2 * i] * pBTable[2 * i] */
  348. multAcc_32x32_keep32_R (outR, *pIn2, CoefA2);
  349. CoefB1 = *pCoefB;
  350. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  351. multSub_32x32_keep32_R (outI, *pIn2--, CoefB1);
  352. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  353. multAcc_32x32_keep32_R (outR, *pIn2, CoefB1);
  354. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  355. multAcc_32x32_keep32_R (outI, *pIn2--, CoefA2);
  356. /* write output */
  357. *pDst++ = outR;
  358. *pDst++ = outI;
  359. /* update coefficient pointer */
  360. pCoefB = pCoefB + (modifier * 2);
  361. pCoefA = pCoefA + (modifier * 2 - 1);
  362. /* Decrement loop count */
  363. fftLen--;
  364. }
  365. }
  366. #endif /* defined(ARM_MATH_MVEI) */