arm_rfft_f32.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_f32.c
  4. * Description: RFFT & RIFFT Floating point 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 "dsp/transform_functions.h"
  29. /* ----------------------------------------------------------------------
  30. * Internal functions prototypes
  31. * -------------------------------------------------------------------- */
  32. extern void arm_radix4_butterfly_f32(
  33. float32_t * pSrc,
  34. uint16_t fftLen,
  35. const float32_t * pCoef,
  36. uint16_t twidCoefModifier);
  37. extern void arm_radix4_butterfly_inverse_f32(
  38. float32_t * pSrc,
  39. uint16_t fftLen,
  40. const float32_t * pCoef,
  41. uint16_t twidCoefModifier,
  42. float32_t onebyfftLen);
  43. extern void arm_bitreversal_f32(
  44. float32_t * pSrc,
  45. uint16_t fftSize,
  46. uint16_t bitRevFactor,
  47. const uint16_t * pBitRevTab);
  48. void arm_split_rfft_f32(
  49. float32_t * pSrc,
  50. uint32_t fftLen,
  51. const float32_t * pATable,
  52. const float32_t * pBTable,
  53. float32_t * pDst,
  54. uint32_t modifier);
  55. void arm_split_rifft_f32(
  56. float32_t * pSrc,
  57. uint32_t fftLen,
  58. const float32_t * pATable,
  59. const float32_t * pBTable,
  60. float32_t * pDst,
  61. uint32_t modifier);
  62. /**
  63. @ingroup groupTransforms
  64. */
  65. /**
  66. @addtogroup RealFFT
  67. @{
  68. */
  69. /**
  70. @brief Processing function for the floating-point RFFT/RIFFT.
  71. Source buffer is modified by this function.
  72. @deprecated Do not use this function. It has been superceded by \ref arm_rfft_fast_f32 and will be removed in the future.
  73. @param[in] S points to an instance of the floating-point RFFT/RIFFT structure
  74. @param[in] pSrc points to the input buffer
  75. @param[out] pDst points to the output buffer
  76. @return none
  77. @par
  78. For the RIFFT, the source buffer must at least have length
  79. fftLenReal + 2.
  80. The last two elements must be equal to what would be generated
  81. by the RFFT:
  82. (pSrc[0] - pSrc[1]) and 0.0f
  83. */
  84. void arm_rfft_f32(
  85. const arm_rfft_instance_f32 * S,
  86. float32_t * pSrc,
  87. float32_t * pDst)
  88. {
  89. const arm_cfft_radix4_instance_f32 *S_CFFT = S->pCfft;
  90. /* Calculation of Real IFFT of input */
  91. if (S->ifftFlagR == 1U)
  92. {
  93. /* Real IFFT core process */
  94. arm_split_rifft_f32 (pSrc, S->fftLenBy2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  95. /* Complex radix-4 IFFT process */
  96. arm_radix4_butterfly_inverse_f32 (pDst, S_CFFT->fftLen, S_CFFT->pTwiddle, S_CFFT->twidCoefModifier, S_CFFT->onebyfftLen);
  97. /* Bit reversal process */
  98. if (S->bitReverseFlagR == 1U)
  99. {
  100. arm_bitreversal_f32 (pDst, S_CFFT->fftLen, S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
  101. }
  102. }
  103. else
  104. {
  105. /* Calculation of RFFT of input */
  106. /* Complex radix-4 FFT process */
  107. arm_radix4_butterfly_f32 (pSrc, S_CFFT->fftLen, S_CFFT->pTwiddle, S_CFFT->twidCoefModifier);
  108. /* Bit reversal process */
  109. if (S->bitReverseFlagR == 1U)
  110. {
  111. arm_bitreversal_f32 (pSrc, S_CFFT->fftLen, S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
  112. }
  113. /* Real FFT core process */
  114. arm_split_rfft_f32 (pSrc, S->fftLenBy2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  115. }
  116. }
  117. /**
  118. @} end of RealFFT group
  119. */
  120. /**
  121. @brief Core Real FFT process
  122. @param[in] pSrc points to input buffer
  123. @param[in] fftLen length of FFT
  124. @param[in] pATable points to twiddle Coef A buffer
  125. @param[in] pBTable points to twiddle Coef B buffer
  126. @param[out] pDst points to output buffer
  127. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  128. @return none
  129. */
  130. void arm_split_rfft_f32(
  131. float32_t * pSrc,
  132. uint32_t fftLen,
  133. const float32_t * pATable,
  134. const float32_t * pBTable,
  135. float32_t * pDst,
  136. uint32_t modifier)
  137. {
  138. uint32_t i; /* Loop Counter */
  139. float32_t outR, outI; /* Temporary variables for output */
  140. const float32_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  141. float32_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  142. float32_t *pDst1 = &pDst[2], *pDst2 = &pDst[(4U * fftLen) - 1U]; /* temp pointers for output buffer */
  143. float32_t *pSrc1 = &pSrc[2], *pSrc2 = &pSrc[(2U * fftLen) - 1U]; /* temp pointers for input buffer */
  144. /* Init coefficient pointers */
  145. pCoefA = &pATable[modifier * 2];
  146. pCoefB = &pBTable[modifier * 2];
  147. i = fftLen - 1U;
  148. while (i > 0U)
  149. {
  150. /*
  151. outR = ( pSrc[2 * i] * pATable[2 * i]
  152. - pSrc[2 * i + 1] * pATable[2 * i + 1]
  153. + pSrc[2 * n - 2 * i] * pBTable[2 * i]
  154. + pSrc[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  155. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  156. + pIn[2 * i] * pATable[2 * i + 1]
  157. + pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  158. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  159. */
  160. /* read pATable[2 * i] */
  161. CoefA1 = *pCoefA++;
  162. /* pATable[2 * i + 1] */
  163. CoefA2 = *pCoefA;
  164. /* pSrc[2 * i] * pATable[2 * i] */
  165. outR = *pSrc1 * CoefA1;
  166. /* pSrc[2 * i] * CoefA2 */
  167. outI = *pSrc1++ * CoefA2;
  168. /* (pSrc[2 * i + 1] + pSrc[2 * fftLen - 2 * i + 1]) * CoefA2 */
  169. outR -= (*pSrc1 + *pSrc2) * CoefA2;
  170. /* pSrc[2 * i + 1] * CoefA1 */
  171. outI += *pSrc1++ * CoefA1;
  172. CoefB1 = *pCoefB;
  173. /* pSrc[2 * fftLen - 2 * i + 1] * CoefB1 */
  174. outI -= *pSrc2-- * CoefB1;
  175. /* pSrc[2 * fftLen - 2 * i] * CoefA2 */
  176. outI -= *pSrc2 * CoefA2;
  177. /* pSrc[2 * fftLen - 2 * i] * CoefB1 */
  178. outR += *pSrc2-- * CoefB1;
  179. /* write output */
  180. *pDst1++ = outR;
  181. *pDst1++ = outI;
  182. /* write complex conjugate output */
  183. *pDst2-- = -outI;
  184. *pDst2-- = outR;
  185. /* update coefficient pointer */
  186. pCoefB = pCoefB + (modifier * 2U);
  187. pCoefA = pCoefA + ((modifier * 2U) - 1U);
  188. i--;
  189. }
  190. pDst[2U * fftLen] = pSrc[0] - pSrc[1];
  191. pDst[(2U * fftLen) + 1U] = 0.0f;
  192. pDst[0] = pSrc[0] + pSrc[1];
  193. pDst[1] = 0.0f;
  194. }
  195. /**
  196. @brief Core Real IFFT process
  197. @param[in] pSrc points to input buffer
  198. @param[in] fftLen length of FFT
  199. @param[in] pATable points to twiddle Coef A buffer
  200. @param[in] pBTable points to twiddle Coef B buffer
  201. @param[out] pDst points to output buffer
  202. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  203. @return none
  204. */
  205. void arm_split_rifft_f32(
  206. float32_t * pSrc,
  207. uint32_t fftLen,
  208. const float32_t * pATable,
  209. const float32_t * pBTable,
  210. float32_t * pDst,
  211. uint32_t modifier)
  212. {
  213. float32_t outR, outI; /* Temporary variables for output */
  214. const float32_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  215. float32_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  216. float32_t *pSrc1 = &pSrc[0], *pSrc2 = &pSrc[(2U * fftLen) + 1U];
  217. pCoefA = &pATable[0];
  218. pCoefB = &pBTable[0];
  219. while (fftLen > 0U)
  220. {
  221. /*
  222. outR = ( pIn[2 * i] * pATable[2 * i]
  223. + pIn[2 * i + 1] * pATable[2 * i + 1]
  224. + pIn[2 * n - 2 * i] * pBTable[2 * i]
  225. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  226. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  227. - pIn[2 * i] * pATable[2 * i + 1]
  228. - pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  229. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  230. */
  231. CoefA1 = *pCoefA++;
  232. CoefA2 = *pCoefA;
  233. /* outR = (pSrc[2 * i] * CoefA1 */
  234. outR = *pSrc1 * CoefA1;
  235. /* - pSrc[2 * i] * CoefA2 */
  236. outI = -(*pSrc1++) * CoefA2;
  237. /* (pSrc[2 * i + 1] + pSrc[2 * fftLen - 2 * i + 1]) * CoefA2 */
  238. outR += (*pSrc1 + *pSrc2) * CoefA2;
  239. /* pSrc[2 * i + 1] * CoefA1 */
  240. outI += (*pSrc1++) * CoefA1;
  241. CoefB1 = *pCoefB;
  242. /* - pSrc[2 * fftLen - 2 * i + 1] * CoefB1 */
  243. outI -= *pSrc2-- * CoefB1;
  244. /* pSrc[2 * fftLen - 2 * i] * CoefB1 */
  245. outR += *pSrc2 * CoefB1;
  246. /* pSrc[2 * fftLen - 2 * i] * CoefA2 */
  247. outI += *pSrc2-- * CoefA2;
  248. /* write output */
  249. *pDst++ = outR;
  250. *pDst++ = outI;
  251. /* update coefficient pointer */
  252. pCoefB = pCoefB + (modifier * 2);
  253. pCoefA = pCoefA + (modifier * 2 - 1);
  254. /* Decrement loop count */
  255. fftLen--;
  256. }
  257. }