arm_rfft_q15.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_q15.c
  4. * Description: RFFT & RIFFT Q15 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_q15(
  33. q15_t * pSrc,
  34. uint32_t fftLen,
  35. const q15_t * pATable,
  36. const 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. const q15_t * pATable,
  43. const 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 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 RFFTQ15.gif "Input and Output Formats for Q15 RFFT"
  62. @par
  63. \image html RIFFTQ15.gif "Input and Output Formats for Q15 RIFFT"
  64. */
  65. void arm_rfft_q15(
  66. const arm_rfft_instance_q15 * S,
  67. q15_t * pSrc,
  68. q15_t * pDst)
  69. {
  70. const arm_cfft_instance_q15 *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_q15 (pSrc, L2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  78. /* Complex IFFT process */
  79. arm_cfft_q15 (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_q15 (S_CFFT, pSrc, S->ifftFlagR, S->bitReverseFlagR);
  90. /* Real FFT core process */
  91. arm_split_rfft_q15 (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. @par
  107. The function implements a Real FFT
  108. */
  109. void arm_split_rfft_q15(
  110. q15_t * pSrc,
  111. uint32_t fftLen,
  112. const q15_t * pATable,
  113. const q15_t * pBTable,
  114. q15_t * pDst,
  115. uint32_t modifier)
  116. {
  117. uint32_t i; /* Loop Counter */
  118. q31_t outR, outI; /* Temporary variables for output */
  119. const q15_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  120. q15_t *pSrc1, *pSrc2;
  121. #if defined (ARM_MATH_DSP)
  122. q15_t *pD1, *pD2;
  123. #endif
  124. /* Init coefficient pointers */
  125. pCoefA = &pATable[modifier * 2];
  126. pCoefB = &pBTable[modifier * 2];
  127. pSrc1 = &pSrc[2];
  128. pSrc2 = &pSrc[(2U * fftLen) - 2U];
  129. #if defined (ARM_MATH_DSP)
  130. i = 1U;
  131. pD1 = pDst + 2;
  132. pD2 = pDst + (4U * fftLen) - 2;
  133. for (i = fftLen - 1; i > 0; i--)
  134. {
  135. /*
  136. outR = ( pSrc[2 * i] * pATable[2 * i]
  137. - pSrc[2 * i + 1] * pATable[2 * i + 1]
  138. + pSrc[2 * n - 2 * i] * pBTable[2 * i]
  139. + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  140. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  141. + pIn[2 * i] * pATable[2 * i + 1]
  142. + pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  143. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i])
  144. */
  145. #ifndef ARM_MATH_BIG_ENDIAN
  146. /* pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  147. outR = __SMUSD(read_q15x2 (pSrc1), read_q15x2((q15_t *) pCoefA));
  148. #else
  149. /* -(pSrc[2 * i + 1] * pATable[2 * i + 1] - pSrc[2 * i] * pATable[2 * i]) */
  150. outR = -(__SMUSD(read_q15x2 (pSrc1), read_q15x2((q15_t *) pCoefA)));
  151. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  152. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]) */
  153. outR = __SMLAD(read_q15x2 (pSrc2), read_q15x2((q15_t *) pCoefB), outR) >> 16U;
  154. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] - pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  155. #ifndef ARM_MATH_BIG_ENDIAN
  156. outI = __SMUSDX(read_q15x2_da (&pSrc2), read_q15x2((q15_t *) pCoefB));
  157. #else
  158. outI = __SMUSDX(read_q15x2 ((q15_t *) pCoefB), read_q15x2_da (&pSrc2));
  159. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  160. /* (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] */
  161. outI = __SMLADX(read_q15x2_ia (&pSrc1), read_q15x2 ((q15_t *) pCoefA), outI);
  162. /* write output */
  163. *pD1++ = (q15_t) outR;
  164. *pD1++ = outI >> 16U;
  165. /* write complex conjugate output */
  166. pD2[0] = (q15_t) outR;
  167. pD2[1] = -(outI >> 16U);
  168. pD2 -= 2;
  169. /* update coefficient pointer */
  170. pCoefB = pCoefB + (2U * modifier);
  171. pCoefA = pCoefA + (2U * modifier);
  172. }
  173. pDst[2U * fftLen] = (pSrc[0] - pSrc[1]) >> 1U;
  174. pDst[2U * fftLen + 1U] = 0;
  175. pDst[0] = (pSrc[0] + pSrc[1]) >> 1U;
  176. pDst[1] = 0;
  177. #else
  178. i = 1U;
  179. while (i < fftLen)
  180. {
  181. /*
  182. outR = ( pSrc[2 * i] * pATable[2 * i]
  183. - pSrc[2 * i + 1] * pATable[2 * i + 1]
  184. + pSrc[2 * n - 2 * i] * pBTable[2 * i]
  185. + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  186. */
  187. outR = *pSrc1 * *pCoefA;
  188. outR = outR - (*(pSrc1 + 1) * *(pCoefA + 1));
  189. outR = outR + (*pSrc2 * *pCoefB);
  190. outR = (outR + (*(pSrc2 + 1) * *(pCoefB + 1))) >> 16;
  191. /*
  192. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  193. + pIn[2 * i] * pATable[2 * i + 1]
  194. + pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  195. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  196. */
  197. outI = *pSrc2 * *(pCoefB + 1);
  198. outI = outI - (*(pSrc2 + 1) * *pCoefB);
  199. outI = outI + (*(pSrc1 + 1) * *pCoefA);
  200. outI = outI + (*pSrc1 * *(pCoefA + 1));
  201. /* update input pointers */
  202. pSrc1 += 2U;
  203. pSrc2 -= 2U;
  204. /* write output */
  205. pDst[2U * i] = (q15_t) outR;
  206. pDst[2U * i + 1U] = outI >> 16U;
  207. /* write complex conjugate output */
  208. pDst[(4U * fftLen) - (2U * i)] = (q15_t) outR;
  209. pDst[((4U * fftLen) - (2U * i)) + 1U] = -(outI >> 16U);
  210. /* update coefficient pointer */
  211. pCoefB = pCoefB + (2U * modifier);
  212. pCoefA = pCoefA + (2U * modifier);
  213. i++;
  214. }
  215. pDst[2U * fftLen] = (pSrc[0] - pSrc[1]) >> 1;
  216. pDst[2U * fftLen + 1U] = 0;
  217. pDst[0] = (pSrc[0] + pSrc[1]) >> 1;
  218. pDst[1] = 0;
  219. #endif /* #if defined (ARM_MATH_DSP) */
  220. }
  221. /**
  222. @brief Core Real IFFT process
  223. @param[in] pSrc points to input buffer
  224. @param[in] fftLen length of FFT
  225. @param[in] pATable points to twiddle Coef A buffer
  226. @param[in] pBTable points to twiddle Coef B buffer
  227. @param[out] pDst points to output buffer
  228. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  229. @return none
  230. @par
  231. The function implements a Real IFFT
  232. */
  233. void arm_split_rifft_q15(
  234. q15_t * pSrc,
  235. uint32_t fftLen,
  236. const q15_t * pATable,
  237. const q15_t * pBTable,
  238. q15_t * pDst,
  239. uint32_t modifier)
  240. {
  241. uint32_t i; /* Loop Counter */
  242. q31_t outR, outI; /* Temporary variables for output */
  243. const q15_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  244. q15_t *pSrc1, *pSrc2;
  245. q15_t *pDst1 = &pDst[0];
  246. pCoefA = &pATable[0];
  247. pCoefB = &pBTable[0];
  248. pSrc1 = &pSrc[0];
  249. pSrc2 = &pSrc[2 * fftLen];
  250. i = fftLen;
  251. while (i > 0U)
  252. {
  253. /*
  254. outR = ( pIn[2 * i] * pATable[2 * i]
  255. + pIn[2 * i + 1] * pATable[2 * i + 1]
  256. + pIn[2 * n - 2 * i] * pBTable[2 * i]
  257. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  258. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  259. - pIn[2 * i] * pATable[2 * i + 1]
  260. - pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  261. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  262. */
  263. #if defined (ARM_MATH_DSP)
  264. #ifndef ARM_MATH_BIG_ENDIAN
  265. /* pIn[2 * n - 2 * i] * pBTable[2 * i] - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]) */
  266. outR = __SMUSD(read_q15x2(pSrc2), read_q15x2((q15_t *) pCoefB));
  267. #else
  268. /* -(-pIn[2 * n - 2 * i] * pBTable[2 * i] + pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1])) */
  269. outR = -(__SMUSD(read_q15x2(pSrc2), read_q15x2((q15_t *) pCoefB)));
  270. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  271. /* pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] + pIn[2 * n - 2 * i] * pBTable[2 * i] */
  272. outR = __SMLAD(read_q15x2(pSrc1), read_q15x2 ((q15_t *) pCoefA), outR) >> 16U;
  273. /* -pIn[2 * n - 2 * i] * pBTable[2 * i + 1] + pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  274. outI = __SMUADX(read_q15x2_da (&pSrc2), read_q15x2((q15_t *) pCoefB));
  275. /* pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] */
  276. #ifndef ARM_MATH_BIG_ENDIAN
  277. outI = __SMLSDX(read_q15x2 ((q15_t *) pCoefA), read_q15x2_ia (&pSrc1), -outI);
  278. #else
  279. outI = __SMLSDX(read_q15x2_ia (&pSrc1), read_q15x2 ((q15_t *) pCoefA), -outI);
  280. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  281. /* write output */
  282. #ifndef ARM_MATH_BIG_ENDIAN
  283. write_q15x2_ia (&pDst1, __PKHBT(outR, (outI >> 16U), 16));
  284. #else
  285. write_q15x2_ia (&pDst1, __PKHBT((outI >> 16U), outR, 16));
  286. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  287. #else /* #if defined (ARM_MATH_DSP) */
  288. outR = *pSrc2 * *pCoefB;
  289. outR = outR - (*(pSrc2 + 1) * *(pCoefB + 1));
  290. outR = outR + (*pSrc1 * *pCoefA);
  291. outR = (outR + (*(pSrc1 + 1) * *(pCoefA + 1))) >> 16;
  292. outI = *(pSrc1 + 1) * *pCoefA;
  293. outI = outI - (*pSrc1 * *(pCoefA + 1));
  294. outI = outI - (*pSrc2 * *(pCoefB + 1));
  295. outI = outI - (*(pSrc2 + 1) * *(pCoefB));
  296. /* update input pointers */
  297. pSrc1 += 2U;
  298. pSrc2 -= 2U;
  299. /* write output */
  300. *pDst1++ = (q15_t) outR;
  301. *pDst1++ = (q15_t) (outI >> 16);
  302. #endif /* #if defined (ARM_MATH_DSP) */
  303. /* update coefficient pointer */
  304. pCoefB = pCoefB + (2 * modifier);
  305. pCoefA = pCoefA + (2 * modifier);
  306. i--;
  307. }
  308. }