arm_rfft_f32.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_rfft_f32.c
  4. * Description: RFFT & RIFFT Floating point process function
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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. @addtogroup DeprecatedRealFFT
  64. @{
  65. */
  66. /**
  67. @brief Processing function for the floating-point RFFT/RIFFT.
  68. Source buffer is modified by this function.
  69. @deprecated Do not use this function. It has been superceded by \ref arm_rfft_fast_f32 and will be removed in the future.
  70. @param[in] S points to an instance of the floating-point RFFT/RIFFT structure
  71. @param[in] pSrc points to the input buffer
  72. @param[out] pDst points to the output buffer
  73. */
  74. void arm_rfft_f32(
  75. const arm_rfft_instance_f32 * S,
  76. float32_t * pSrc,
  77. float32_t * pDst)
  78. {
  79. const arm_cfft_radix4_instance_f32 *S_CFFT = S->pCfft;
  80. /* Calculation of Real IFFT of input */
  81. if (S->ifftFlagR == 1U)
  82. {
  83. /* Real IFFT core process */
  84. arm_split_rifft_f32 (pSrc, S->fftLenBy2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  85. /* Complex radix-4 IFFT process */
  86. arm_radix4_butterfly_inverse_f32 (pDst, S_CFFT->fftLen, S_CFFT->pTwiddle, S_CFFT->twidCoefModifier, S_CFFT->onebyfftLen);
  87. /* Bit reversal process */
  88. if (S->bitReverseFlagR == 1U)
  89. {
  90. arm_bitreversal_f32 (pDst, S_CFFT->fftLen, S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
  91. }
  92. }
  93. else
  94. {
  95. /* Calculation of RFFT of input */
  96. /* Complex radix-4 FFT process */
  97. arm_radix4_butterfly_f32 (pSrc, S_CFFT->fftLen, S_CFFT->pTwiddle, S_CFFT->twidCoefModifier);
  98. /* Bit reversal process */
  99. if (S->bitReverseFlagR == 1U)
  100. {
  101. arm_bitreversal_f32 (pSrc, S_CFFT->fftLen, S_CFFT->bitRevFactor, S_CFFT->pBitRevTable);
  102. }
  103. /* Real FFT core process */
  104. arm_split_rfft_f32 (pSrc, S->fftLenBy2, S->pTwiddleAReal, S->pTwiddleBReal, pDst, S->twidCoefRModifier);
  105. }
  106. }
  107. /**
  108. @} end of DeprecatedRealFFT group
  109. */
  110. /**
  111. @brief Core Real FFT process
  112. @param[in] pSrc points to input buffer
  113. @param[in] fftLen length of FFT
  114. @param[in] pATable points to twiddle Coef A buffer
  115. @param[in] pBTable points to twiddle Coef B buffer
  116. @param[out] pDst points to output buffer
  117. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  118. */
  119. void arm_split_rfft_f32(
  120. float32_t * pSrc,
  121. uint32_t fftLen,
  122. const float32_t * pATable,
  123. const float32_t * pBTable,
  124. float32_t * pDst,
  125. uint32_t modifier)
  126. {
  127. uint32_t i; /* Loop Counter */
  128. float32_t outR, outI; /* Temporary variables for output */
  129. const float32_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  130. float32_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  131. float32_t *pDst1 = &pDst[2], *pDst2 = &pDst[(4U * fftLen) - 1U]; /* temp pointers for output buffer */
  132. float32_t *pSrc1 = &pSrc[2], *pSrc2 = &pSrc[(2U * fftLen) - 1U]; /* temp pointers for input buffer */
  133. /* Init coefficient pointers */
  134. pCoefA = &pATable[modifier * 2];
  135. pCoefB = &pBTable[modifier * 2];
  136. i = fftLen - 1U;
  137. while (i > 0U)
  138. {
  139. /*
  140. outR = ( pSrc[2 * i] * pATable[2 * i]
  141. - 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. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  145. + 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. */
  149. /* read pATable[2 * i] */
  150. CoefA1 = *pCoefA++;
  151. /* pATable[2 * i + 1] */
  152. CoefA2 = *pCoefA;
  153. /* pSrc[2 * i] * pATable[2 * i] */
  154. outR = *pSrc1 * CoefA1;
  155. /* pSrc[2 * i] * CoefA2 */
  156. outI = *pSrc1++ * CoefA2;
  157. /* (pSrc[2 * i + 1] + pSrc[2 * fftLen - 2 * i + 1]) * CoefA2 */
  158. outR -= (*pSrc1 + *pSrc2) * CoefA2;
  159. /* pSrc[2 * i + 1] * CoefA1 */
  160. outI += *pSrc1++ * CoefA1;
  161. CoefB1 = *pCoefB;
  162. /* pSrc[2 * fftLen - 2 * i + 1] * CoefB1 */
  163. outI -= *pSrc2-- * CoefB1;
  164. /* pSrc[2 * fftLen - 2 * i] * CoefA2 */
  165. outI -= *pSrc2 * CoefA2;
  166. /* pSrc[2 * fftLen - 2 * i] * CoefB1 */
  167. outR += *pSrc2-- * CoefB1;
  168. /* write output */
  169. *pDst1++ = outR;
  170. *pDst1++ = outI;
  171. /* write complex conjugate output */
  172. *pDst2-- = -outI;
  173. *pDst2-- = outR;
  174. /* update coefficient pointer */
  175. pCoefB = pCoefB + (modifier * 2U);
  176. pCoefA = pCoefA + ((modifier * 2U) - 1U);
  177. i--;
  178. }
  179. pDst[2U * fftLen] = pSrc[0] - pSrc[1];
  180. pDst[(2U * fftLen) + 1U] = 0.0f;
  181. pDst[0] = pSrc[0] + pSrc[1];
  182. pDst[1] = 0.0f;
  183. }
  184. /**
  185. @brief Core Real IFFT process
  186. @param[in] pSrc points to input buffer
  187. @param[in] fftLen length of FFT
  188. @param[in] pATable points to twiddle Coef A buffer
  189. @param[in] pBTable points to twiddle Coef B buffer
  190. @param[out] pDst points to output buffer
  191. @param[in] modifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  192. */
  193. void arm_split_rifft_f32(
  194. float32_t * pSrc,
  195. uint32_t fftLen,
  196. const float32_t * pATable,
  197. const float32_t * pBTable,
  198. float32_t * pDst,
  199. uint32_t modifier)
  200. {
  201. float32_t outR, outI; /* Temporary variables for output */
  202. const float32_t *pCoefA, *pCoefB; /* Temporary pointers for twiddle factors */
  203. float32_t CoefA1, CoefA2, CoefB1; /* Temporary variables for twiddle coefficients */
  204. float32_t *pSrc1 = &pSrc[0], *pSrc2 = &pSrc[(2U * fftLen) + 1U];
  205. pCoefA = &pATable[0];
  206. pCoefB = &pBTable[0];
  207. while (fftLen > 0U)
  208. {
  209. /*
  210. outR = ( pIn[2 * i] * pATable[2 * i]
  211. + pIn[2 * i + 1] * pATable[2 * i + 1]
  212. + pIn[2 * n - 2 * i] * pBTable[2 * i]
  213. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i + 1]);
  214. outI = ( pIn[2 * i + 1] * pATable[2 * i]
  215. - pIn[2 * i] * pATable[2 * i + 1]
  216. - pIn[2 * n - 2 * i] * pBTable[2 * i + 1]
  217. - pIn[2 * n - 2 * i + 1] * pBTable[2 * i]);
  218. */
  219. CoefA1 = *pCoefA++;
  220. CoefA2 = *pCoefA;
  221. /* outR = (pSrc[2 * i] * CoefA1 */
  222. outR = *pSrc1 * CoefA1;
  223. /* - pSrc[2 * i] * CoefA2 */
  224. outI = -(*pSrc1++) * CoefA2;
  225. /* (pSrc[2 * i + 1] + pSrc[2 * fftLen - 2 * i + 1]) * CoefA2 */
  226. outR += (*pSrc1 + *pSrc2) * CoefA2;
  227. /* pSrc[2 * i + 1] * CoefA1 */
  228. outI += (*pSrc1++) * CoefA1;
  229. CoefB1 = *pCoefB;
  230. /* - pSrc[2 * fftLen - 2 * i + 1] * CoefB1 */
  231. outI -= *pSrc2-- * CoefB1;
  232. /* pSrc[2 * fftLen - 2 * i] * CoefB1 */
  233. outR += *pSrc2 * CoefB1;
  234. /* pSrc[2 * fftLen - 2 * i] * CoefA2 */
  235. outI += *pSrc2-- * CoefA2;
  236. /* write output */
  237. *pDst++ = outR;
  238. *pDst++ = outI;
  239. /* update coefficient pointer */
  240. pCoefB = pCoefB + (modifier * 2);
  241. pCoefA = pCoefA + (modifier * 2 - 1);
  242. /* Decrement loop count */
  243. fftLen--;
  244. }
  245. }