arm_bitreversal.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_bitreversal.c
  4. * Description: Bitreversal functions
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 "arm_math.h"
  29. #include "arm_common_tables.h"
  30. /*
  31. * @brief In-place bit reversal function.
  32. * @param[in, out] *pSrc points to the in-place buffer of floating-point data type.
  33. * @param[in] fftSize length of the FFT.
  34. * @param[in] bitRevFactor bit reversal modifier that supports different size FFTs with the same bit reversal table.
  35. * @param[in] *pBitRevTab points to the bit reversal table.
  36. * @return none.
  37. */
  38. void arm_bitreversal_f32(
  39. float32_t * pSrc,
  40. uint16_t fftSize,
  41. uint16_t bitRevFactor,
  42. uint16_t * pBitRevTab)
  43. {
  44. uint16_t fftLenBy2, fftLenBy2p1;
  45. uint16_t i, j;
  46. float32_t in;
  47. /* Initializations */
  48. j = 0U;
  49. fftLenBy2 = fftSize >> 1U;
  50. fftLenBy2p1 = (fftSize >> 1U) + 1U;
  51. /* Bit Reversal Implementation */
  52. for (i = 0U; i <= (fftLenBy2 - 2U); i += 2U)
  53. {
  54. if (i < j)
  55. {
  56. /* pSrc[i] <-> pSrc[j]; */
  57. in = pSrc[2U * i];
  58. pSrc[2U * i] = pSrc[2U * j];
  59. pSrc[2U * j] = in;
  60. /* pSrc[i+1U] <-> pSrc[j+1U] */
  61. in = pSrc[(2U * i) + 1U];
  62. pSrc[(2U * i) + 1U] = pSrc[(2U * j) + 1U];
  63. pSrc[(2U * j) + 1U] = in;
  64. /* pSrc[i+fftLenBy2p1] <-> pSrc[j+fftLenBy2p1] */
  65. in = pSrc[2U * (i + fftLenBy2p1)];
  66. pSrc[2U * (i + fftLenBy2p1)] = pSrc[2U * (j + fftLenBy2p1)];
  67. pSrc[2U * (j + fftLenBy2p1)] = in;
  68. /* pSrc[i+fftLenBy2p1+1U] <-> pSrc[j+fftLenBy2p1+1U] */
  69. in = pSrc[(2U * (i + fftLenBy2p1)) + 1U];
  70. pSrc[(2U * (i + fftLenBy2p1)) + 1U] =
  71. pSrc[(2U * (j + fftLenBy2p1)) + 1U];
  72. pSrc[(2U * (j + fftLenBy2p1)) + 1U] = in;
  73. }
  74. /* pSrc[i+1U] <-> pSrc[j+1U] */
  75. in = pSrc[2U * (i + 1U)];
  76. pSrc[2U * (i + 1U)] = pSrc[2U * (j + fftLenBy2)];
  77. pSrc[2U * (j + fftLenBy2)] = in;
  78. /* pSrc[i+2U] <-> pSrc[j+2U] */
  79. in = pSrc[(2U * (i + 1U)) + 1U];
  80. pSrc[(2U * (i + 1U)) + 1U] = pSrc[(2U * (j + fftLenBy2)) + 1U];
  81. pSrc[(2U * (j + fftLenBy2)) + 1U] = in;
  82. /* Reading the index for the bit reversal */
  83. j = *pBitRevTab;
  84. /* Updating the bit reversal index depending on the fft length */
  85. pBitRevTab += bitRevFactor;
  86. }
  87. }
  88. /*
  89. * @brief In-place bit reversal function.
  90. * @param[in, out] *pSrc points to the in-place buffer of Q31 data type.
  91. * @param[in] fftLen length of the FFT.
  92. * @param[in] bitRevFactor bit reversal modifier that supports different size FFTs with the same bit reversal table
  93. * @param[in] *pBitRevTab points to bit reversal table.
  94. * @return none.
  95. */
  96. void arm_bitreversal_q31(
  97. q31_t * pSrc,
  98. uint32_t fftLen,
  99. uint16_t bitRevFactor,
  100. uint16_t * pBitRevTable)
  101. {
  102. uint32_t fftLenBy2, fftLenBy2p1, i, j;
  103. q31_t in;
  104. /* Initializations */
  105. j = 0U;
  106. fftLenBy2 = fftLen / 2U;
  107. fftLenBy2p1 = (fftLen / 2U) + 1U;
  108. /* Bit Reversal Implementation */
  109. for (i = 0U; i <= (fftLenBy2 - 2U); i += 2U)
  110. {
  111. if (i < j)
  112. {
  113. /* pSrc[i] <-> pSrc[j]; */
  114. in = pSrc[2U * i];
  115. pSrc[2U * i] = pSrc[2U * j];
  116. pSrc[2U * j] = in;
  117. /* pSrc[i+1U] <-> pSrc[j+1U] */
  118. in = pSrc[(2U * i) + 1U];
  119. pSrc[(2U * i) + 1U] = pSrc[(2U * j) + 1U];
  120. pSrc[(2U * j) + 1U] = in;
  121. /* pSrc[i+fftLenBy2p1] <-> pSrc[j+fftLenBy2p1] */
  122. in = pSrc[2U * (i + fftLenBy2p1)];
  123. pSrc[2U * (i + fftLenBy2p1)] = pSrc[2U * (j + fftLenBy2p1)];
  124. pSrc[2U * (j + fftLenBy2p1)] = in;
  125. /* pSrc[i+fftLenBy2p1+1U] <-> pSrc[j+fftLenBy2p1+1U] */
  126. in = pSrc[(2U * (i + fftLenBy2p1)) + 1U];
  127. pSrc[(2U * (i + fftLenBy2p1)) + 1U] =
  128. pSrc[(2U * (j + fftLenBy2p1)) + 1U];
  129. pSrc[(2U * (j + fftLenBy2p1)) + 1U] = in;
  130. }
  131. /* pSrc[i+1U] <-> pSrc[j+1U] */
  132. in = pSrc[2U * (i + 1U)];
  133. pSrc[2U * (i + 1U)] = pSrc[2U * (j + fftLenBy2)];
  134. pSrc[2U * (j + fftLenBy2)] = in;
  135. /* pSrc[i+2U] <-> pSrc[j+2U] */
  136. in = pSrc[(2U * (i + 1U)) + 1U];
  137. pSrc[(2U * (i + 1U)) + 1U] = pSrc[(2U * (j + fftLenBy2)) + 1U];
  138. pSrc[(2U * (j + fftLenBy2)) + 1U] = in;
  139. /* Reading the index for the bit reversal */
  140. j = *pBitRevTable;
  141. /* Updating the bit reversal index depending on the fft length */
  142. pBitRevTable += bitRevFactor;
  143. }
  144. }
  145. /*
  146. * @brief In-place bit reversal function.
  147. * @param[in, out] *pSrc points to the in-place buffer of Q15 data type.
  148. * @param[in] fftLen length of the FFT.
  149. * @param[in] bitRevFactor bit reversal modifier that supports different size FFTs with the same bit reversal table
  150. * @param[in] *pBitRevTab points to bit reversal table.
  151. * @return none.
  152. */
  153. void arm_bitreversal_q15(
  154. q15_t * pSrc16,
  155. uint32_t fftLen,
  156. uint16_t bitRevFactor,
  157. uint16_t * pBitRevTab)
  158. {
  159. q31_t *pSrc = (q31_t *) pSrc16;
  160. q31_t in;
  161. uint32_t fftLenBy2, fftLenBy2p1;
  162. uint32_t i, j;
  163. /* Initializations */
  164. j = 0U;
  165. fftLenBy2 = fftLen / 2U;
  166. fftLenBy2p1 = (fftLen / 2U) + 1U;
  167. /* Bit Reversal Implementation */
  168. for (i = 0U; i <= (fftLenBy2 - 2U); i += 2U)
  169. {
  170. if (i < j)
  171. {
  172. /* pSrc[i] <-> pSrc[j]; */
  173. /* pSrc[i+1U] <-> pSrc[j+1U] */
  174. in = pSrc[i];
  175. pSrc[i] = pSrc[j];
  176. pSrc[j] = in;
  177. /* pSrc[i + fftLenBy2p1] <-> pSrc[j + fftLenBy2p1]; */
  178. /* pSrc[i + fftLenBy2p1+1U] <-> pSrc[j + fftLenBy2p1+1U] */
  179. in = pSrc[i + fftLenBy2p1];
  180. pSrc[i + fftLenBy2p1] = pSrc[j + fftLenBy2p1];
  181. pSrc[j + fftLenBy2p1] = in;
  182. }
  183. /* pSrc[i+1U] <-> pSrc[j+fftLenBy2]; */
  184. /* pSrc[i+2] <-> pSrc[j+fftLenBy2+1U] */
  185. in = pSrc[i + 1U];
  186. pSrc[i + 1U] = pSrc[j + fftLenBy2];
  187. pSrc[j + fftLenBy2] = in;
  188. /* Reading the index for the bit reversal */
  189. j = *pBitRevTab;
  190. /* Updating the bit reversal index depending on the fft length */
  191. pBitRevTab += bitRevFactor;
  192. }
  193. }