arm_bitreversal.c 6.8 KB

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