arm_rfft_q15.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_q15.c
  4. * Description: RFFT & RIFFT Q15 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_q15(
  33. q15_t * pSrc,
  34. uint32_t fftLen,
  35. q15_t * pATable,
  36. q15_t * pBTable,
  37. q15_t * pDst,
  38. uint32_t modifier);
  39. void arm_split_rifft_q15(
  40. q15_t * pSrc,
  41. uint32_t fftLen,
  42. q15_t * pATable,
  43. q15_t * pBTable,
  44. q15_t * pDst,
  45. uint32_t modifier);
  46. /**
  47. * @addtogroup RealFFT
  48. * @{
  49. */
  50. /**
  51. * @brief Processing function for the Q15 RFFT/RIFFT.
  52. * @param[in] *S points to an instance of the Q15 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 RFFTQ15.gif "Input and Output Formats for Q15 RFFT"
  64. * \par
  65. * \image html RIFFTQ15.gif "Input and Output Formats for Q15 RIFFT"
  66. */
  67. void arm_rfft_q15(
  68. const arm_rfft_instance_q15 * S,
  69. q15_t * pSrc,
  70. q15_t * pDst)
  71. {
  72. const arm_cfft_instance_q15 *S_CFFT = S->pCfft;
  73. uint32_t i;
  74. uint32_t L2 = S->fftLenReal >> 1;
  75. /* Calculation of RIFFT of input */
  76. if (S->ifftFlagR == 1U)
  77. {
  78. /* Real IFFT core process */
  79. arm_split_rifft_q15(pSrc, L2, S->pTwiddleAReal,
  80. S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  81. /* Complex IFFT process */
  82. arm_cfft_q15(S_CFFT, pDst, S->ifftFlagR, S->bitReverseFlagR);
  83. for(i=0;i<S->fftLenReal;i++)
  84. {
  85. pDst[i] = pDst[i] << 1;
  86. }
  87. }
  88. else
  89. {
  90. /* Calculation of RFFT of input */
  91. /* Complex FFT process */
  92. arm_cfft_q15(S_CFFT, pSrc, S->ifftFlagR, S->bitReverseFlagR);
  93. /* Real FFT core process */
  94. arm_split_rfft_q15(pSrc, L2, S->pTwiddleAReal,
  95. S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  96. }
  97. }
  98. /**
  99. * @} end of RealFFT group
  100. */
  101. /**
  102. * @brief Core Real FFT process
  103. * @param *pSrc points to the input buffer.
  104. * @param fftLen length of FFT.
  105. * @param *pATable points to the A twiddle Coef buffer.
  106. * @param *pBTable points to the B twiddle Coef buffer.
  107. * @param *pDst points to the output buffer.
  108. * @param modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
  109. * @return none.
  110. * The function implements a Real FFT
  111. */
  112. void arm_split_rfft_q15(
  113. q15_t * pSrc,
  114. uint32_t fftLen,
  115. q15_t * pATable,
  116. q15_t * pBTable,
  117. q15_t * pDst,
  118. uint32_t modifier)
  119. {
  120. uint32_t i; /* Loop Counter */
  121. q31_t outR, outI; /* Temporary variables for output */
  122. q15_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  123. q15_t *pSrc1, *pSrc2;
  124. #if defined (ARM_MATH_DSP)
  125. q15_t *pD1, *pD2;
  126. #endif
  127. // pSrc[2U * fftLen] = pSrc[0];
  128. // pSrc[(2U * fftLen) + 1U] = pSrc[1];
  129. pCoefA = &pATable[modifier * 2U];
  130. pCoefB = &pBTable[modifier * 2U];
  131. pSrc1 = &pSrc[2];
  132. pSrc2 = &pSrc[(2U * fftLen) - 2U];
  133. #if defined (ARM_MATH_DSP)
  134. /* Run the below code for Cortex-M4 and Cortex-M3 */
  135. i = 1U;
  136. pD1 = pDst + 2;
  137. pD2 = pDst + (4U * fftLen) - 2;
  138. for(i = fftLen - 1; i > 0; i--)
  139. {
  140. /*
  141. outR = (pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1]
  142. + pSrc[2 * n - 2 * i] * pBTable[2 * i] +
  143. pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  144. */
  145. /* outI = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] +
  146. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  147. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]); */
  148. #ifndef ARM_MATH_BIG_ENDIAN
  149. /* pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  150. outR = __SMUSD(*__SIMD32(pSrc1), *__SIMD32(pCoefA));
  151. #else
  152. /* -(pSrc[2 * i + 1] * pATable[2 * i + 1] - pSrc[2 * i] * pATable[2 * i]) */
  153. outR = -(__SMUSD(*__SIMD32(pSrc1), *__SIMD32(pCoefA)));
  154. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  155. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] +
  156. pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]) */
  157. outR = __SMLAD(*__SIMD32(pSrc2), *__SIMD32(pCoefB), outR) >> 16U;
  158. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  159. pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  160. #ifndef ARM_MATH_BIG_ENDIAN
  161. outI = __SMUSDX(*__SIMD32(pSrc2)--, *__SIMD32(pCoefB));
  162. #else
  163. outI = __SMUSDX(*__SIMD32(pCoefB), *__SIMD32(pSrc2)--);
  164. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  165. /* (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] */
  166. outI = __SMLADX(*__SIMD32(pSrc1)++, *__SIMD32(pCoefA), outI);
  167. /* write output */
  168. *pD1++ = (q15_t) outR;
  169. *pD1++ = outI >> 16U;
  170. /* write complex conjugate output */
  171. pD2[0] = (q15_t) outR;
  172. pD2[1] = -(outI >> 16U);
  173. pD2 -= 2;
  174. /* update coefficient pointer */
  175. pCoefB = pCoefB + (2U * modifier);
  176. pCoefA = pCoefA + (2U * modifier);
  177. }
  178. pDst[2U * fftLen] = (pSrc[0] - pSrc[1]) >> 1;
  179. pDst[(2U * fftLen) + 1U] = 0;
  180. pDst[0] = (pSrc[0] + pSrc[1]) >> 1;
  181. pDst[1] = 0;
  182. #else
  183. /* Run the below code for Cortex-M0 */
  184. i = 1U;
  185. while (i < fftLen)
  186. {
  187. /*
  188. outR = (pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1]
  189. + pSrc[2 * n - 2 * i] * pBTable[2 * i] +
  190. pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  191. */
  192. outR = *pSrc1 * *pCoefA;
  193. outR = outR - (*(pSrc1 + 1) * *(pCoefA + 1));
  194. outR = outR + (*pSrc2 * *pCoefB);
  195. outR = (outR + (*(pSrc2 + 1) * *(pCoefB + 1))) >> 16;
  196. /* outI = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] +
  197. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  198. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  199. */
  200. outI = *pSrc2 * *(pCoefB + 1);
  201. outI = outI - (*(pSrc2 + 1) * *pCoefB);
  202. outI = outI + (*(pSrc1 + 1) * *pCoefA);
  203. outI = outI + (*pSrc1 * *(pCoefA + 1));
  204. /* update input pointers */
  205. pSrc1 += 2U;
  206. pSrc2 -= 2U;
  207. /* write output */
  208. pDst[2U * i] = (q15_t) outR;
  209. pDst[(2U * i) + 1U] = outI >> 16U;
  210. /* write complex conjugate output */
  211. pDst[(4U * fftLen) - (2U * i)] = (q15_t) outR;
  212. pDst[((4U * fftLen) - (2U * i)) + 1U] = -(outI >> 16U);
  213. /* update coefficient pointer */
  214. pCoefB = pCoefB + (2U * modifier);
  215. pCoefA = pCoefA + (2U * modifier);
  216. i++;
  217. }
  218. pDst[2U * fftLen] = (pSrc[0] - pSrc[1]) >> 1;
  219. pDst[(2U * fftLen) + 1U] = 0;
  220. pDst[0] = (pSrc[0] + pSrc[1]) >> 1;
  221. pDst[1] = 0;
  222. #endif /* #if defined (ARM_MATH_DSP) */
  223. }
  224. /**
  225. * @brief Core Real IFFT process
  226. * @param[in] *pSrc points to the input buffer.
  227. * @param[in] fftLen length of FFT.
  228. * @param[in] *pATable points to the twiddle Coef A buffer.
  229. * @param[in] *pBTable points to the twiddle Coef B buffer.
  230. * @param[out] *pDst points to the output buffer.
  231. * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
  232. * @return none.
  233. * The function implements a Real IFFT
  234. */
  235. void arm_split_rifft_q15(
  236. q15_t * pSrc,
  237. uint32_t fftLen,
  238. q15_t * pATable,
  239. q15_t * pBTable,
  240. q15_t * pDst,
  241. uint32_t modifier)
  242. {
  243. uint32_t i; /* Loop Counter */
  244. q31_t outR, outI; /* Temporary variables for output */
  245. q15_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  246. q15_t *pSrc1, *pSrc2;
  247. q15_t *pDst1 = &pDst[0];
  248. pCoefA = &pATable[0];
  249. pCoefB = &pBTable[0];
  250. pSrc1 = &pSrc[0];
  251. pSrc2 = &pSrc[2U * fftLen];
  252. #if defined (ARM_MATH_DSP)
  253. /* Run the below code for Cortex-M4 and Cortex-M3 */
  254. i = fftLen;
  255. while (i > 0U)
  256. {
  257. /*
  258. outR = (pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
  259. pIn[2 * n - 2 * i] * pBTable[2 * i] -
  260. pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  261. outI = (pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] -
  262. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  263. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  264. */
  265. #ifndef ARM_MATH_BIG_ENDIAN
  266. /* pIn[2 * n - 2 * i] * pBTable[2 * i] -
  267. pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]) */
  268. outR = __SMUSD(*__SIMD32(pSrc2), *__SIMD32(pCoefB));
  269. #else
  270. /* -(-pIn[2 * n - 2 * i] * pBTable[2 * i] +
  271. pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1])) */
  272. outR = -(__SMUSD(*__SIMD32(pSrc2), *__SIMD32(pCoefB)));
  273. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  274. /* pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
  275. pIn[2 * n - 2 * i] * pBTable[2 * i] */
  276. outR = __SMLAD(*__SIMD32(pSrc1), *__SIMD32(pCoefA), outR) >> 16U;
  277. /*
  278. -pIn[2 * n - 2 * i] * pBTable[2 * i + 1] +
  279. pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  280. outI = __SMUADX(*__SIMD32(pSrc2)--, *__SIMD32(pCoefB));
  281. /* pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] */
  282. #ifndef ARM_MATH_BIG_ENDIAN
  283. outI = __SMLSDX(*__SIMD32(pCoefA), *__SIMD32(pSrc1)++, -outI);
  284. #else
  285. outI = __SMLSDX(*__SIMD32(pSrc1)++, *__SIMD32(pCoefA), -outI);
  286. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  287. /* write output */
  288. #ifndef ARM_MATH_BIG_ENDIAN
  289. *__SIMD32(pDst1)++ = __PKHBT(outR, (outI >> 16U), 16);
  290. #else
  291. *__SIMD32(pDst1)++ = __PKHBT((outI >> 16U), outR, 16);
  292. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  293. /* update coefficient pointer */
  294. pCoefB = pCoefB + (2U * modifier);
  295. pCoefA = pCoefA + (2U * modifier);
  296. i--;
  297. }
  298. #else
  299. /* Run the below code for Cortex-M0 */
  300. i = fftLen;
  301. while (i > 0U)
  302. {
  303. /*
  304. outR = (pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
  305. pIn[2 * n - 2 * i] * pBTable[2 * i] -
  306. pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  307. */
  308. outR = *pSrc2 * *pCoefB;
  309. outR = outR - (*(pSrc2 + 1) * *(pCoefB + 1));
  310. outR = outR + (*pSrc1 * *pCoefA);
  311. outR = (outR + (*(pSrc1 + 1) * *(pCoefA + 1))) >> 16;
  312. /*
  313. outI = (pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] -
  314. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  315. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  316. */
  317. outI = *(pSrc1 + 1) * *pCoefA;
  318. outI = outI - (*pSrc1 * *(pCoefA + 1));
  319. outI = outI - (*pSrc2 * *(pCoefB + 1));
  320. outI = outI - (*(pSrc2 + 1) * *(pCoefB));
  321. /* update input pointers */
  322. pSrc1 += 2U;
  323. pSrc2 -= 2U;
  324. /* write output */
  325. *pDst1++ = (q15_t) outR;
  326. *pDst1++ = (q15_t) (outI >> 16);
  327. /* update coefficient pointer */
  328. pCoefB = pCoefB + (2U * modifier);
  329. pCoefA = pCoefA + (2U * modifier);
  330. i--;
  331. }
  332. #endif /* #if defined (ARM_MATH_DSP) */
  333. }