You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

arm_rfft_q31.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2013 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 17. January 2013
  5. * $Revision: V1.4.1
  6. *
  7. * Project: CMSIS DSP Library
  8. * Title: arm_rfft_q31.c
  9. *
  10. * Description: RFFT & RIFFT Q31 process function
  11. *
  12. *
  13. * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. * - Neither the name of ARM LIMITED nor the names of its contributors
  25. * may be used to endorse or promote products derived from this
  26. * software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  31. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  32. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  33. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  34. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. * -------------------------------------------------------------------- */
  41. #include "arm_math.h"
  42. void arm_radix4_butterfly_inverse_q31(
  43. q31_t * pSrc,
  44. uint32_t fftLen,
  45. q31_t * pCoef,
  46. uint32_t twidCoefModifier);
  47. void arm_radix4_butterfly_q31(
  48. q31_t * pSrc,
  49. uint32_t fftLen,
  50. q31_t * pCoef,
  51. uint32_t twidCoefModifier);
  52. void arm_bitreversal_q31(
  53. q31_t * pSrc,
  54. uint32_t fftLen,
  55. uint16_t bitRevFactor,
  56. uint16_t * pBitRevTab);
  57. /*--------------------------------------------------------------------
  58. * Internal functions prototypes
  59. --------------------------------------------------------------------*/
  60. void arm_split_rfft_q31(
  61. q31_t * pSrc,
  62. uint32_t fftLen,
  63. q31_t * pATable,
  64. q31_t * pBTable,
  65. q31_t * pDst,
  66. uint32_t modifier);
  67. void arm_split_rifft_q31(
  68. q31_t * pSrc,
  69. uint32_t fftLen,
  70. q31_t * pATable,
  71. q31_t * pBTable,
  72. q31_t * pDst,
  73. uint32_t modifier);
  74. /**
  75. * @addtogroup RealFFT
  76. * @{
  77. */
  78. /**
  79. * @brief Processing function for the Q31 RFFT/RIFFT.
  80. * @param[in] *S points to an instance of the Q31 RFFT/RIFFT structure.
  81. * @param[in] *pSrc points to the input buffer.
  82. * @param[out] *pDst points to the output buffer.
  83. * @return none.
  84. *
  85. * \par Input an output formats:
  86. * \par
  87. * Internally input is downscaled by 2 for every stage to avoid saturations inside CFFT/CIFFT process.
  88. * Hence the output format is different for different RFFT sizes.
  89. * 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:
  90. * \par
  91. * \image html RFFTQ31.gif "Input and Output Formats for Q31 RFFT"
  92. *
  93. * \par
  94. * \image html RIFFTQ31.gif "Input and Output Formats for Q31 RIFFT"
  95. */
  96. void arm_rfft_q31(
  97. const arm_rfft_instance_q31 * S,
  98. q31_t * pSrc,
  99. q31_t * pDst)
  100. {
  101. const arm_cfft_radix4_instance_q31 *S_CFFT = S->pCfft;
  102. /* Calculation of RIFFT of input */
  103. if(S->ifftFlagR == 1u)
  104. {
  105. /* Real IFFT core process */
  106. arm_split_rifft_q31(pSrc, S->fftLenBy2, S->pTwiddleAReal,
  107. S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  108. /* Complex readix-4 IFFT process */
  109. arm_radix4_butterfly_inverse_q31(pDst, S_CFFT->fftLen,
  110. S_CFFT->pTwiddle,
  111. S_CFFT->twidCoefModifier);
  112. /* Bit reversal process */
  113. if(S->bitReverseFlagR == 1u)
  114. {
  115. arm_bitreversal_q31(pDst, S_CFFT->fftLen,
  116. S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
  117. }
  118. }
  119. else
  120. {
  121. /* Calculation of RFFT of input */
  122. /* Complex readix-4 FFT process */
  123. arm_radix4_butterfly_q31(pSrc, S_CFFT->fftLen,
  124. S_CFFT->pTwiddle, S_CFFT->twidCoefModifier);
  125. /* Bit reversal process */
  126. if(S->bitReverseFlagR == 1u)
  127. {
  128. arm_bitreversal_q31(pSrc, S_CFFT->fftLen,
  129. S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
  130. }
  131. /* Real FFT core process */
  132. arm_split_rfft_q31(pSrc, S->fftLenBy2, S->pTwiddleAReal,
  133. S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  134. }
  135. }
  136. /**
  137. * @} end of RealFFT group
  138. */
  139. /**
  140. * @brief Core Real FFT process
  141. * @param[in] *pSrc points to the input buffer.
  142. * @param[in] fftLen length of FFT.
  143. * @param[in] *pATable points to the twiddle Coef A buffer.
  144. * @param[in] *pBTable points to the twiddle Coef B buffer.
  145. * @param[out] *pDst points to the output buffer.
  146. * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
  147. * @return none.
  148. */
  149. void arm_split_rfft_q31(
  150. q31_t * pSrc,
  151. uint32_t fftLen,
  152. q31_t * pATable,
  153. q31_t * pBTable,
  154. q31_t * pDst,
  155. uint32_t modifier)
  156. {
  157. uint32_t i; /* Loop Counter */
  158. q31_t outR, outI; /* Temporary variables for output */
  159. q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  160. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  161. q31_t *pOut1 = &pDst[2], *pOut2 = &pDst[(4u * fftLen) - 1u];
  162. q31_t *pIn1 = &pSrc[2], *pIn2 = &pSrc[(2u * fftLen) - 1u];
  163. /* Init coefficient pointers */
  164. pCoefA = &pATable[modifier * 2u];
  165. pCoefB = &pBTable[modifier * 2u];
  166. i = fftLen - 1u;
  167. while(i > 0u)
  168. {
  169. /*
  170. outR = (pSrc[2 * i] * pATable[2 * i] - pSrc[2 * i + 1] * pATable[2 * i + 1]
  171. + pSrc[2 * n - 2 * i] * pBTable[2 * i] +
  172. pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  173. */
  174. /* outI = (pIn[2 * i + 1] * pATable[2 * i] + pIn[2 * i] * pATable[2 * i + 1] +
  175. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  176. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]); */
  177. CoefA1 = *pCoefA++;
  178. CoefA2 = *pCoefA;
  179. /* outR = (pSrc[2 * i] * pATable[2 * i] */
  180. outR = ((int32_t) (((q63_t) * pIn1 * CoefA1) >> 32));
  181. /* outI = pIn[2 * i] * pATable[2 * i + 1] */
  182. outI = ((int32_t) (((q63_t) * pIn1++ * CoefA2) >> 32));
  183. /* - pSrc[2 * i + 1] * pATable[2 * i + 1] */
  184. outR =
  185. (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn1 * (-CoefA2))) >> 32);
  186. /* (pIn[2 * i + 1] * pATable[2 * i] */
  187. outI =
  188. (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn1++ * (CoefA1))) >> 32);
  189. /* pSrc[2 * n - 2 * i] * pBTable[2 * i] */
  190. outR =
  191. (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (-CoefA2))) >> 32);
  192. CoefB1 = *pCoefB;
  193. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  194. outI =
  195. (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn2-- * (-CoefB1))) >> 32);
  196. /* pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  197. outR =
  198. (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (CoefB1))) >> 32);
  199. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  200. outI =
  201. (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn2-- * (-CoefA2))) >> 32);
  202. /* write output */
  203. *pOut1++ = (outR << 1u);
  204. *pOut1++ = (outI << 1u);
  205. /* write complex conjugate output */
  206. *pOut2-- = -(outI << 1u);
  207. *pOut2-- = (outR << 1u);
  208. /* update coefficient pointer */
  209. pCoefB = pCoefB + (modifier * 2u);
  210. pCoefA = pCoefA + ((modifier * 2u) - 1u);
  211. i--;
  212. }
  213. pDst[2u * fftLen] = pSrc[0] - pSrc[1];
  214. pDst[(2u * fftLen) + 1u] = 0;
  215. pDst[0] = pSrc[0] + pSrc[1];
  216. pDst[1] = 0;
  217. }
  218. /**
  219. * @brief Core Real IFFT process
  220. * @param[in] *pSrc points to the input buffer.
  221. * @param[in] fftLen length of FFT.
  222. * @param[in] *pATable points to the twiddle Coef A buffer.
  223. * @param[in] *pBTable points to the twiddle Coef B buffer.
  224. * @param[out] *pDst points to the output buffer.
  225. * @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table.
  226. * @return none.
  227. */
  228. void arm_split_rifft_q31(
  229. q31_t * pSrc,
  230. uint32_t fftLen,
  231. q31_t * pATable,
  232. q31_t * pBTable,
  233. q31_t * pDst,
  234. uint32_t modifier)
  235. {
  236. q31_t outR, outI; /* Temporary variables for output */
  237. q31_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  238. q31_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  239. q31_t *pIn1 = &pSrc[0], *pIn2 = &pSrc[(2u * fftLen) + 1u];
  240. pCoefA = &pATable[0];
  241. pCoefB = &pBTable[0];
  242. while(fftLen > 0u)
  243. {
  244. /*
  245. outR = (pIn[2 * i] * pATable[2 * i] + pIn[2 * i + 1] * pATable[2 * i + 1] +
  246. pIn[2 * n - 2 * i] * pBTable[2 * i] -
  247. pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  248. outI = (pIn[2 * i + 1] * pATable[2 * i] - pIn[2 * i] * pATable[2 * i + 1] -
  249. pIn[2 * n - 2 * i] * pBTable[2 * i + 1] -
  250. pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  251. */
  252. CoefA1 = *pCoefA++;
  253. CoefA2 = *pCoefA;
  254. /* outR = (pIn[2 * i] * pATable[2 * i] */
  255. outR = ((int32_t) (((q63_t) * pIn1 * CoefA1) >> 32));
  256. /* - pIn[2 * i] * pATable[2 * i + 1] */
  257. outI = -((int32_t) (((q63_t) * pIn1++ * CoefA2) >> 32));
  258. /* pIn[2 * i + 1] * pATable[2 * i + 1] */
  259. outR =
  260. (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn1 * (CoefA2))) >> 32);
  261. /* pIn[2 * i + 1] * pATable[2 * i] */
  262. outI =
  263. (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn1++ * (CoefA1))) >> 32);
  264. /* pIn[2 * n - 2 * i] * pBTable[2 * i] */
  265. outR =
  266. (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (CoefA2))) >> 32);
  267. CoefB1 = *pCoefB;
  268. /* pIn[2 * n - 2 * i] * pBTable[2 * i + 1] */
  269. outI =
  270. (q31_t) ((((q63_t) outI << 32) - ((q63_t) * pIn2-- * (CoefB1))) >> 32);
  271. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1] */
  272. outR =
  273. (q31_t) ((((q63_t) outR << 32) + ((q63_t) * pIn2 * (CoefB1))) >> 32);
  274. /* pIn[2 * n - 2 * i + 1] * pBTable[2 * i] */
  275. outI =
  276. (q31_t) ((((q63_t) outI << 32) + ((q63_t) * pIn2-- * (CoefA2))) >> 32);
  277. /* write output */
  278. *pDst++ = (outR << 1u);
  279. *pDst++ = (outI << 1u);
  280. /* update coefficient pointer */
  281. pCoefB = pCoefB + (modifier * 2u);
  282. pCoefA = pCoefA + ((modifier * 2u) - 1u);
  283. /* Decrement loop count */
  284. fftLen--;
  285. }
  286. }