arm_rfft_f32.c 9.9 KB

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