arm_cfft_radix2_f32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cfft_radix2_f32.c
  4. * Description: Radix-2 Decimation in Frequency CFFT & CIFFT Floating point processing 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. void arm_radix2_butterfly_f32(
  30. float32_t * pSrc,
  31. uint32_t fftLen,
  32. const float32_t * pCoef,
  33. uint16_t twidCoefModifier);
  34. void arm_radix2_butterfly_inverse_f32(
  35. float32_t * pSrc,
  36. uint32_t fftLen,
  37. const float32_t * pCoef,
  38. uint16_t twidCoefModifier,
  39. float32_t onebyfftLen);
  40. extern void arm_bitreversal_f32(
  41. float32_t * pSrc,
  42. uint16_t fftSize,
  43. uint16_t bitRevFactor,
  44. const uint16_t * pBitRevTab);
  45. /**
  46. @addtogroup ComplexFFTDeprecated
  47. @{
  48. */
  49. /**
  50. @brief Radix-2 CFFT/CIFFT.
  51. @deprecated Do not use this function. It has been superseded by \ref arm_cfft_f32 and will be removed in the future
  52. @param[in] S points to an instance of the floating-point Radix-2 CFFT/CIFFT structure
  53. @param[in,out] pSrc points to the complex data buffer of size <code>2*fftLen</code>. Processing occurs in-place
  54. */
  55. void arm_cfft_radix2_f32(
  56. const arm_cfft_radix2_instance_f32 * S,
  57. float32_t * pSrc)
  58. {
  59. if (S->ifftFlag == 1U)
  60. {
  61. /* Complex IFFT radix-2 */
  62. arm_radix2_butterfly_inverse_f32(pSrc, S->fftLen, S->pTwiddle,
  63. S->twidCoefModifier, S->onebyfftLen);
  64. }
  65. else
  66. {
  67. /* Complex FFT radix-2 */
  68. arm_radix2_butterfly_f32(pSrc, S->fftLen, S->pTwiddle,
  69. S->twidCoefModifier);
  70. }
  71. if (S->bitReverseFlag == 1U)
  72. {
  73. /* Bit Reversal */
  74. arm_bitreversal_f32(pSrc, S->fftLen, S->bitRevFactor, S->pBitRevTable);
  75. }
  76. }
  77. /**
  78. @} end of ComplexFFTDeprecated group
  79. */
  80. /* ----------------------------------------------------------------------
  81. ** Internal helper function used by the FFTs
  82. ** ------------------------------------------------------------------- */
  83. /**
  84. brief Core function for the floating-point CFFT butterfly process.
  85. param[in,out] pSrc points to in-place buffer of floating-point data type
  86. param[in] fftLen length of the FFT
  87. param[in] pCoef points to twiddle coefficient buffer
  88. param[in] twidCoefModifier twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table
  89. return none
  90. */
  91. void arm_radix2_butterfly_f32(
  92. float32_t * pSrc,
  93. uint32_t fftLen,
  94. const float32_t * pCoef,
  95. uint16_t twidCoefModifier)
  96. {
  97. uint32_t i, j, k, l;
  98. uint32_t n1, n2, ia;
  99. float32_t xt, yt, cosVal, sinVal;
  100. float32_t p0, p1, p2, p3;
  101. float32_t a0, a1;
  102. #if defined (ARM_MATH_DSP)
  103. /* Initializations for the first stage */
  104. n2 = fftLen >> 1;
  105. ia = 0;
  106. i = 0;
  107. // loop for groups
  108. for (k = n2; k > 0; k--)
  109. {
  110. cosVal = pCoef[ia * 2];
  111. sinVal = pCoef[(ia * 2) + 1];
  112. /* Twiddle coefficients index modifier */
  113. ia += twidCoefModifier;
  114. /* index calculation for the input as, */
  115. /* pSrc[i + 0], pSrc[i + fftLen/1] */
  116. l = i + n2;
  117. /* Butterfly implementation */
  118. a0 = pSrc[2 * i] + pSrc[2 * l];
  119. xt = pSrc[2 * i] - pSrc[2 * l];
  120. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  121. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  122. p0 = xt * cosVal;
  123. p1 = yt * sinVal;
  124. p2 = yt * cosVal;
  125. p3 = xt * sinVal;
  126. pSrc[2 * i] = a0;
  127. pSrc[2 * i + 1] = a1;
  128. pSrc[2 * l] = p0 + p1;
  129. pSrc[2 * l + 1] = p2 - p3;
  130. i++;
  131. } // groups loop end
  132. twidCoefModifier <<= 1U;
  133. // loop for stage
  134. for (k = n2; k > 2; k = k >> 1)
  135. {
  136. n1 = n2;
  137. n2 = n2 >> 1;
  138. ia = 0;
  139. // loop for groups
  140. j = 0;
  141. do
  142. {
  143. cosVal = pCoef[ia * 2];
  144. sinVal = pCoef[(ia * 2) + 1];
  145. ia += twidCoefModifier;
  146. // loop for butterfly
  147. i = j;
  148. do
  149. {
  150. l = i + n2;
  151. a0 = pSrc[2 * i] + pSrc[2 * l];
  152. xt = pSrc[2 * i] - pSrc[2 * l];
  153. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  154. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  155. p0 = xt * cosVal;
  156. p1 = yt * sinVal;
  157. p2 = yt * cosVal;
  158. p3 = xt * sinVal;
  159. pSrc[2 * i] = a0;
  160. pSrc[2 * i + 1] = a1;
  161. pSrc[2 * l] = p0 + p1;
  162. pSrc[2 * l + 1] = p2 - p3;
  163. i += n1;
  164. } while ( i < fftLen ); // butterfly loop end
  165. j++;
  166. } while ( j < n2); // groups loop end
  167. twidCoefModifier <<= 1U;
  168. } // stages loop end
  169. // loop for butterfly
  170. for (i = 0; i < fftLen; i += 2)
  171. {
  172. a0 = pSrc[2 * i] + pSrc[2 * i + 2];
  173. xt = pSrc[2 * i] - pSrc[2 * i + 2];
  174. yt = pSrc[2 * i + 1] - pSrc[2 * i + 3];
  175. a1 = pSrc[2 * i + 3] + pSrc[2 * i + 1];
  176. pSrc[2 * i] = a0;
  177. pSrc[2 * i + 1] = a1;
  178. pSrc[2 * i + 2] = xt;
  179. pSrc[2 * i + 3] = yt;
  180. } // groups loop end
  181. #else /* #if defined (ARM_MATH_DSP) */
  182. n2 = fftLen;
  183. // loop for stage
  184. for (k = fftLen; k > 1; k = k >> 1)
  185. {
  186. n1 = n2;
  187. n2 = n2 >> 1;
  188. ia = 0;
  189. // loop for groups
  190. j = 0;
  191. do
  192. {
  193. cosVal = pCoef[ia * 2];
  194. sinVal = pCoef[(ia * 2) + 1];
  195. ia += twidCoefModifier;
  196. // loop for butterfly
  197. i = j;
  198. do
  199. {
  200. l = i + n2;
  201. a0 = pSrc[2 * i] + pSrc[2 * l];
  202. xt = pSrc[2 * i] - pSrc[2 * l];
  203. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  204. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  205. p0 = xt * cosVal;
  206. p1 = yt * sinVal;
  207. p2 = yt * cosVal;
  208. p3 = xt * sinVal;
  209. pSrc[2 * i] = a0;
  210. pSrc[2 * i + 1] = a1;
  211. pSrc[2 * l] = p0 + p1;
  212. pSrc[2 * l + 1] = p2 - p3;
  213. i += n1;
  214. } while (i < fftLen);
  215. j++;
  216. } while (j < n2);
  217. twidCoefModifier <<= 1U;
  218. }
  219. #endif /* #if defined (ARM_MATH_DSP) */
  220. }
  221. void arm_radix2_butterfly_inverse_f32(
  222. float32_t * pSrc,
  223. uint32_t fftLen,
  224. const float32_t * pCoef,
  225. uint16_t twidCoefModifier,
  226. float32_t onebyfftLen)
  227. {
  228. uint32_t i, j, k, l;
  229. uint32_t n1, n2, ia;
  230. float32_t xt, yt, cosVal, sinVal;
  231. float32_t p0, p1, p2, p3;
  232. float32_t a0, a1;
  233. #if defined (ARM_MATH_DSP)
  234. n2 = fftLen >> 1;
  235. ia = 0;
  236. // loop for groups
  237. for (i = 0; i < n2; i++)
  238. {
  239. cosVal = pCoef[ia * 2];
  240. sinVal = pCoef[(ia * 2) + 1];
  241. ia += twidCoefModifier;
  242. l = i + n2;
  243. a0 = pSrc[2 * i] + pSrc[2 * l];
  244. xt = pSrc[2 * i] - pSrc[2 * l];
  245. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  246. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  247. p0 = xt * cosVal;
  248. p1 = yt * sinVal;
  249. p2 = yt * cosVal;
  250. p3 = xt * sinVal;
  251. pSrc[2 * i] = a0;
  252. pSrc[2 * i + 1] = a1;
  253. pSrc[2 * l] = p0 - p1;
  254. pSrc[2 * l + 1] = p2 + p3;
  255. } // groups loop end
  256. twidCoefModifier <<= 1U;
  257. // loop for stage
  258. for (k = fftLen / 2; k > 2; k = k >> 1)
  259. {
  260. n1 = n2;
  261. n2 = n2 >> 1;
  262. ia = 0;
  263. // loop for groups
  264. j = 0;
  265. do
  266. {
  267. cosVal = pCoef[ia * 2];
  268. sinVal = pCoef[(ia * 2) + 1];
  269. ia += twidCoefModifier;
  270. // loop for butterfly
  271. i = j;
  272. do
  273. {
  274. l = i + n2;
  275. a0 = pSrc[2 * i] + pSrc[2 * l];
  276. xt = pSrc[2 * i] - pSrc[2 * l];
  277. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  278. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  279. p0 = xt * cosVal;
  280. p1 = yt * sinVal;
  281. p2 = yt * cosVal;
  282. p3 = xt * sinVal;
  283. pSrc[2 * i] = a0;
  284. pSrc[2 * i + 1] = a1;
  285. pSrc[2 * l] = p0 - p1;
  286. pSrc[2 * l + 1] = p2 + p3;
  287. i += n1;
  288. } while ( i < fftLen ); // butterfly loop end
  289. j++;
  290. } while (j < n2); // groups loop end
  291. twidCoefModifier <<= 1U;
  292. } // stages loop end
  293. // loop for butterfly
  294. for (i = 0; i < fftLen; i += 2)
  295. {
  296. a0 = pSrc[2 * i] + pSrc[2 * i + 2];
  297. xt = pSrc[2 * i] - pSrc[2 * i + 2];
  298. a1 = pSrc[2 * i + 3] + pSrc[2 * i + 1];
  299. yt = pSrc[2 * i + 1] - pSrc[2 * i + 3];
  300. p0 = a0 * onebyfftLen;
  301. p2 = xt * onebyfftLen;
  302. p1 = a1 * onebyfftLen;
  303. p3 = yt * onebyfftLen;
  304. pSrc[2 * i] = p0;
  305. pSrc[2 * i + 1] = p1;
  306. pSrc[2 * i + 2] = p2;
  307. pSrc[2 * i + 3] = p3;
  308. } // butterfly loop end
  309. #else /* #if defined (ARM_MATH_DSP) */
  310. n2 = fftLen;
  311. // loop for stage
  312. for (k = fftLen; k > 2; k = k >> 1)
  313. {
  314. n1 = n2;
  315. n2 = n2 >> 1;
  316. ia = 0;
  317. // loop for groups
  318. j = 0;
  319. do
  320. {
  321. cosVal = pCoef[ia * 2];
  322. sinVal = pCoef[(ia * 2) + 1];
  323. ia = ia + twidCoefModifier;
  324. // loop for butterfly
  325. i = j;
  326. do
  327. {
  328. l = i + n2;
  329. a0 = pSrc[2 * i] + pSrc[2 * l];
  330. xt = pSrc[2 * i] - pSrc[2 * l];
  331. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  332. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  333. p0 = xt * cosVal;
  334. p1 = yt * sinVal;
  335. p2 = yt * cosVal;
  336. p3 = xt * sinVal;
  337. pSrc[2 * i] = a0;
  338. pSrc[2 * i + 1] = a1;
  339. pSrc[2 * l] = p0 - p1;
  340. pSrc[2 * l + 1] = p2 + p3;
  341. i += n1;
  342. } while ( i < fftLen ); // butterfly loop end
  343. j++;
  344. } while ( j < n2 ); // groups loop end
  345. twidCoefModifier = twidCoefModifier << 1U;
  346. } // stages loop end
  347. n1 = n2;
  348. n2 = n2 >> 1;
  349. // loop for butterfly
  350. for (i = 0; i < fftLen; i += n1)
  351. {
  352. l = i + n2;
  353. a0 = pSrc[2 * i] + pSrc[2 * l];
  354. xt = pSrc[2 * i] - pSrc[2 * l];
  355. a1 = pSrc[2 * l + 1] + pSrc[2 * i + 1];
  356. yt = pSrc[2 * i + 1] - pSrc[2 * l + 1];
  357. p0 = a0 * onebyfftLen;
  358. p2 = xt * onebyfftLen;
  359. p1 = a1 * onebyfftLen;
  360. p3 = yt * onebyfftLen;
  361. pSrc[2 * i] = p0;
  362. pSrc[2 * l] = p2;
  363. pSrc[2 * i + 1] = p1;
  364. pSrc[2 * l + 1] = p3;
  365. } // butterfly loop end
  366. #endif /* #if defined (ARM_MATH_DSP) */
  367. }