arm_rfft_q31.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 (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. */
  68. void arm_rfft_q31(
  69. const arm_rfft_instance_q31 * S,
  70. q31_t * pSrc,
  71. q31_t * pDst)
  72. {
  73. #if defined(ARM_MATH_MVEI)
  74. const arm_cfft_instance_q31 *S_CFFT = &(S->cfftInst);
  75. #else
  76. const arm_cfft_instance_q31 *S_CFFT = S->pCfft;
  77. #endif
  78. uint32_t L2 = S->fftLenReal >> 1U;
  79. uint32_t i;
  80. /* Calculation of RIFFT of input */
  81. if (S->ifftFlagR == 1U)
  82. {
  83. /* Real IFFT core process */
  84. arm_split_rifft_q31 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  85. /* Complex IFFT process */
  86. arm_cfft_q31 (S_CFFT, pDst, S->ifftFlagR, S->bitReverseFlagR);
  87. for(i = 0; i < S->fftLenReal; i++)
  88. {
  89. pDst[i] = pDst[i] << 1U;
  90. }
  91. }
  92. else
  93. {
  94. /* Calculation of RFFT of input */
  95. /* Complex FFT process */
  96. arm_cfft_q31 (S_CFFT, pSrc, S->ifftFlagR, S->bitReverseFlagR);
  97. /* Real FFT core process */
  98. arm_split_rfft_q31 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  99. }
  100. }
  101. /**
  102. @} end of RealFFT group
  103. */
  104. /**
  105. @brief Core Real FFT process
  106. @param[in] pSrc points to input buffer
  107. @param[in] fftLen length of FFT
  108. @param[in] pATable points to twiddle Coef A buffer
  109. @param[in] pBTable points to twiddle Coef B buffer
  110. @param[out] pDst points to output buffer
  111. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  112. @return none
  113. */
  114. #if defined(ARM_MATH_MVEI)
  115. void arm_split_rfft_q31(
  116. q31_t *pSrc,
  117. uint32_t fftLen,
  118. const q31_t *pATable,
  119. const q31_t *pBTable,
  120. q31_t *pDst,
  121. uint32_t modifier)
  122. {
  123. q31_t const *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  124. q31_t *pDst1 = &pDst[2], *pDst2 = &pDst[(4U * fftLen) - 1U]; /* temp pointers for output buffer */
  125. q31_t const *pSrc1 = &pSrc[2], *pSrc2 = &pSrc[(2U * fftLen) - 1U]; /* temp pointers for input buffer */
  126. q31_t const *pVecSrc1;
  127. q31_t *pVecDst1;
  128. q31x4x2_t vecIn, vecSum;
  129. uint32_t blkCnt;
  130. uint32x4_t vecStridesFwd, vecStridesBkwd;
  131. q31x4_t vecInBkwd, vecCoefFwd0, vecCoefFwd1;
  132. /*
  133. * Init coefficient pointers
  134. */
  135. pCoefA = &pATable[modifier * 2U];
  136. pCoefB = &pBTable[modifier * 2U];
  137. /*
  138. * scatter / gather offsets
  139. * for ascending & descending addressing
  140. */
  141. vecStridesFwd = vidupq_u32((uint32_t)0, 2);
  142. vecStridesBkwd = -vecStridesFwd;
  143. vecStridesFwd = vecStridesFwd * modifier;
  144. pVecSrc1 = (q31_t const *) pSrc1;
  145. pVecDst1 = pDst1;
  146. blkCnt = fftLen >> 2;
  147. while (blkCnt > 0U)
  148. {
  149. vecCoefFwd0 = vldrwq_gather_shifted_offset(pCoefA, vecStridesFwd);
  150. vecCoefFwd1 = vldrwq_gather_shifted_offset(&pCoefA[1], vecStridesFwd);
  151. vecIn = vld2q(pVecSrc1);
  152. pVecSrc1 += 8;
  153. /*
  154. * outR = *pSrc1 * CoefA1;
  155. */
  156. vecSum.val[0] = vmulhq(vecIn.val[0], vecCoefFwd0);
  157. /*
  158. * outI = *pSrc1++ * CoefA2;
  159. */
  160. vecSum.val[1] = vmulhq(vecIn.val[0], vecCoefFwd1);
  161. vecInBkwd = vldrwq_gather_shifted_offset(pSrc2, vecStridesBkwd);
  162. /*
  163. * outR -= (*pSrc1 + *pSrc2) * CoefA2;
  164. */
  165. vecInBkwd = vqaddq(vecIn.val[1], vecInBkwd);
  166. vecSum.val[0] = vqsubq(vecSum.val[0], vmulhq(vecInBkwd, vecCoefFwd1));
  167. vecInBkwd = vldrwq_gather_shifted_offset(pSrc2, vecStridesBkwd);
  168. /*
  169. * outI += *pSrc1++ * CoefA1;
  170. */
  171. vecSum.val[1] = vqaddq(vecSum.val[1], vmulhq(vecIn.val[1], vecCoefFwd0));
  172. vecCoefFwd0 = vldrwq_gather_shifted_offset(pCoefB, vecStridesFwd);
  173. /*
  174. * outI -= *pSrc2-- * CoefB1;
  175. */
  176. vecSum.val[1] = vqsubq(vecSum.val[1], vmulhq(vecInBkwd, vecCoefFwd0));
  177. vecInBkwd = vldrwq_gather_shifted_offset(&pSrc2[-1], vecStridesBkwd);
  178. /*
  179. * outI -= *pSrc2 * CoefA2;
  180. */
  181. vecSum.val[1] = vqsubq(vecSum.val[1], vmulhq(vecInBkwd, vecCoefFwd1));
  182. /*
  183. * outR += *pSrc2-- * CoefB1;
  184. */
  185. vecSum.val[0] = vqaddq(vecSum.val[0], vmulhq(vecInBkwd, vecCoefFwd0));
  186. vst2q(pVecDst1, vecSum);
  187. pVecDst1 += 8;
  188. /*
  189. * write complex conjugate output
  190. */
  191. vecSum.val[1] = -vecSum.val[1];
  192. vstrwq_scatter_shifted_offset(pDst2, vecStridesBkwd, vecSum.val[1]);
  193. vstrwq_scatter_shifted_offset(&pDst2[-1], vecStridesBkwd, vecSum.val[0]);
  194. /*
  195. * update fwd and backwd offsets
  196. */
  197. vecStridesFwd = vecStridesFwd + (modifier * 8U);
  198. vecStridesBkwd = vecStridesBkwd - 8;
  199. blkCnt--;
  200. }
  201. pDst[2U * fftLen] = (pSrc[0] - pSrc[1]) >> 1;
  202. pDst[(2U * fftLen) + 1U] = 0;
  203. pDst[0] = (pSrc[0] + pSrc[1]) >> 1;
  204. pDst[1] = 0;
  205. }
  206. #else
  207. void arm_split_rfft_q31(
  208. q31_t * pSrc,
  209. uint32_t fftLen,
  210. const q31_t * pATable,
  211. const q31_t * pBTable,
  212. q31_t * pDst,
  213. uint32_t modifier)
  214. {
  215. uint32_t i; /* Loop Counter */
  216. q31_t outR, outI; /* Temporary variables for output */
  217. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  218. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  219. q31_t *pOut1 = &pDst[2], *pOut2 = &pDst[4 * fftLen - 1];
  220. q31_t *pIn1 = &pSrc[2], *pIn2 = &pSrc[2 * fftLen - 1];
  221. /* Init coefficient pointers */
  222. pCoefA = &pATable[modifier * 2];
  223. pCoefB = &pBTable[modifier * 2];
  224. i = fftLen - 1U;
  225. while (i > 0U)
  226. {
  227. /*
  228. outR = ( pSrc[2 * i] * pATable[2 * i]
  229. - pSrc[2 * i + 1] * pATable[2 * i + 1]
  230. + pSrc[2 * n - 2 * i] * pBTable[2 * i]
  231. + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  232. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  233. + pIn[2 * i] * pATable[2 * i + 1]
  234. + pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  235. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  236. */
  237. CoefA1 = *pCoefA++;
  238. CoefA2 = *pCoefA;
  239. /* outR = (pSrc[2 * i] * pATable[2 * i] */
  240. mult_32x32_keep32_R (outR, *pIn1, CoefA1);
  241. /* outI = pIn[2 * i] * pATable[2 * i + 1] */
  242. mult_32x32_keep32_R (outI, *pIn1++, CoefA2);
  243. /* - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  244. multSub_32x32_keep32_R (outR, *pIn1, CoefA2);
  245. /* (pIn[2 * i + 1] * pATable[2 * i] */
  246. multAcc_32x32_keep32_R (outI, *pIn1++, CoefA1);
  247. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] */
  248. multSub_32x32_keep32_R (outR, *pIn2, CoefA2);
  249. CoefB1 = *pCoefB;
  250. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  251. multSub_32x32_keep32_R (outI, *pIn2--, CoefB1);
  252. /* pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  253. multAcc_32x32_keep32_R (outR, *pIn2, CoefB1);
  254. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  255. multSub_32x32_keep32_R (outI, *pIn2--, CoefA2);
  256. /* write output */
  257. *pOut1++ = outR;
  258. *pOut1++ = outI;
  259. /* write complex conjugate output */
  260. *pOut2-- = -outI;
  261. *pOut2-- = outR;
  262. /* update coefficient pointer */
  263. pCoefB = pCoefB + (2 * modifier);
  264. pCoefA = pCoefA + (2 * modifier - 1);
  265. /* Decrement loop count */
  266. i--;
  267. }
  268. pDst[2 * fftLen] = (pSrc[0] - pSrc[1]) >> 1U;
  269. pDst[2 * fftLen + 1] = 0;
  270. pDst[0] = (pSrc[0] + pSrc[1]) >> 1U;
  271. pDst[1] = 0;
  272. }
  273. #endif /* defined(ARM_MATH_MVEI) */
  274. /**
  275. @brief Core Real IFFT process
  276. @param[in] pSrc points to input buffer
  277. @param[in] fftLen length of FFT
  278. @param[in] pATable points to twiddle Coef A buffer
  279. @param[in] pBTable points to twiddle Coef B buffer
  280. @param[out] pDst points to output buffer
  281. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  282. @return none
  283. */
  284. #if defined(ARM_MATH_MVEI)
  285. void arm_split_rifft_q31(
  286. q31_t * pSrc,
  287. uint32_t fftLen,
  288. const q31_t * pATable,
  289. const q31_t * pBTable,
  290. q31_t * pDst,
  291. uint32_t modifier)
  292. {
  293. q31_t const *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  294. q31_t const *pSrc1 = &pSrc[0], *pSrc2 = &pSrc[(2U * fftLen) + 1U];
  295. q31_t const *pVecSrc1;
  296. q31_t *pVecDst;
  297. q31x4x2_t vecIn, vecSum;
  298. uint32_t blkCnt;
  299. uint32x4_t vecStridesFwd, vecStridesBkwd;
  300. q31x4_t vecInBkwd, vecCoefFwd0, vecCoefFwd1;
  301. /*
  302. * Init coefficient pointers
  303. */
  304. pCoefA = &pATable[0];
  305. pCoefB = &pBTable[0];
  306. /*
  307. * scatter / gather offsets
  308. * for ascending & descending addressing
  309. */
  310. vecStridesFwd = vidupq_u32((uint32_t)0, 2);
  311. vecStridesBkwd = -vecStridesFwd;
  312. vecStridesFwd = vecStridesFwd * modifier;
  313. pVecSrc1 = (q31_t const *) pSrc1;
  314. pVecDst = pDst;
  315. blkCnt = fftLen >> 2;
  316. while (blkCnt > 0U)
  317. {
  318. vecCoefFwd0 = vldrwq_gather_shifted_offset(pCoefA, vecStridesFwd);
  319. vecCoefFwd1 = vldrwq_gather_shifted_offset(&pCoefA[1], vecStridesFwd);
  320. vecIn = vld2q(pVecSrc1);
  321. pVecSrc1 += 8;
  322. /*
  323. * outR = *pSrc1 * CoefA1;
  324. */
  325. vecSum.val[0] = vmulhq(vecIn.val[0], vecCoefFwd0);
  326. /*
  327. * outI = -(*pSrc1++) * CoefA2;
  328. */
  329. vecIn.val[0] = (-vecIn.val[0]);
  330. vecSum.val[1] = vmulhq(vecIn.val[0], vecCoefFwd1);
  331. vecInBkwd = vldrwq_gather_shifted_offset(pSrc2, vecStridesBkwd);
  332. /*
  333. * outR += (*pSrc1 + *pSrc2) * CoefA2;
  334. */
  335. vecInBkwd = vqaddq(vecIn.val[1], vecInBkwd);
  336. vecSum.val[0] = vqaddq(vecSum.val[0], vmulhq(vecInBkwd, vecCoefFwd1));
  337. vecInBkwd = vldrwq_gather_shifted_offset(pSrc2, vecStridesBkwd);
  338. /*
  339. * outI += *pSrc1++ * CoefA1;
  340. */
  341. vecSum.val[1] = vqaddq(vecSum.val[1], vmulhq(vecIn.val[1], vecCoefFwd0));
  342. vecCoefFwd0 = vldrwq_gather_shifted_offset(pCoefB, vecStridesFwd);
  343. /*
  344. * outI -= *pSrc2-- * CoefB1;
  345. */
  346. vecSum.val[1] = vqsubq(vecSum.val[1], vmulhq(vecInBkwd, vecCoefFwd0));
  347. vecInBkwd = vldrwq_gather_shifted_offset(&pSrc2[-1], vecStridesBkwd);
  348. /*
  349. * outI += *pSrc2-- * CoefA2;;
  350. */
  351. vecSum.val[1] = vqaddq(vecSum.val[1], vmulhq(vecInBkwd, vecCoefFwd1));
  352. /*
  353. * outR += *pSrc2-- * CoefB1;
  354. */
  355. vecSum.val[0] = vqaddq(vecSum.val[0], vmulhq(vecInBkwd, vecCoefFwd0));
  356. vst2q(pVecDst, vecSum);
  357. pVecDst += 8;
  358. /*
  359. * update fwd and backwd offsets
  360. */
  361. vecStridesFwd = vecStridesFwd + (modifier * 8U);
  362. vecStridesBkwd = vecStridesBkwd - 8;
  363. blkCnt--;
  364. }
  365. }
  366. #else
  367. void arm_split_rifft_q31(
  368. q31_t * pSrc,
  369. uint32_t fftLen,
  370. const q31_t * pATable,
  371. const q31_t * pBTable,
  372. q31_t * pDst,
  373. uint32_t modifier)
  374. {
  375. q31_t outR, outI; /* Temporary variables for output */
  376. const q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  377. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  378. q31_t *pIn1 = &pSrc[0], *pIn2 = &pSrc[2 * fftLen + 1];
  379. pCoefA = &pATable[0];
  380. pCoefB = &pBTable[0];
  381. while (fftLen > 0U)
  382. {
  383. /*
  384. outR = ( pIn[2 * i] * pATable[2 * i]
  385. + pIn[2 * i + 1] * pATable[2 * i + 1]
  386. + pIn[2 * n - 2 * i] * pBTable[2 * i]
  387. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  388. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  389. - pIn[2 * i] * pATable[2 * i + 1]
  390. - pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  391. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  392. */
  393. CoefA1 = *pCoefA++;
  394. CoefA2 = *pCoefA;
  395. /* outR = (pIn[2 * i] * pATable[2 * i] */
  396. mult_32x32_keep32_R (outR, *pIn1, CoefA1);
  397. /* - pIn[2 * i] * pATable[2 * i + 1] */
  398. mult_32x32_keep32_R (outI, *pIn1++, -CoefA2);
  399. /* pIn[2 * i + 1] * pATable[2 * i + 1] */
  400. multAcc_32x32_keep32_R (outR, *pIn1, CoefA2);
  401. /* pIn[2 * i + 1] * pATable[2 * i] */
  402. multAcc_32x32_keep32_R (outI, *pIn1++, CoefA1);
  403. /* pIn[2 * n - 2 * i] * pBTable[2 * i] */
  404. multAcc_32x32_keep32_R (outR, *pIn2, CoefA2);
  405. CoefB1 = *pCoefB;
  406. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  407. multSub_32x32_keep32_R (outI, *pIn2--, CoefB1);
  408. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  409. multAcc_32x32_keep32_R (outR, *pIn2, CoefB1);
  410. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  411. multAcc_32x32_keep32_R (outI, *pIn2--, CoefA2);
  412. /* write output */
  413. *pDst++ = outR;
  414. *pDst++ = outI;
  415. /* update coefficient pointer */
  416. pCoefB = pCoefB + (modifier * 2);
  417. pCoefA = pCoefA + (modifier * 2 - 1);
  418. /* Decrement loop count */
  419. fftLen--;
  420. }
  421. }
  422. #endif /* defined(ARM_MATH_MVEI) */