arm_cmplx_mag_q15.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_q15.c
  4. * Description: Q15 complex magnitude
  5. *
  6. * $Date: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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. /**
  30. @ingroup groupCmplxMath
  31. */
  32. /**
  33. @addtogroup cmplx_mag
  34. @{
  35. */
  36. /**
  37. @brief Q15 complex magnitude.
  38. @param[in] pSrc points to input vector
  39. @param[out] pDst points to output vector
  40. @param[in] numSamples number of samples in each vector
  41. @return none
  42. @par Scaling and Overflow Behavior
  43. The function implements 1.15 by 1.15 multiplications and finally output is converted into 2.14 format.
  44. */
  45. void arm_cmplx_mag_q15(
  46. const q15_t * pSrc,
  47. q15_t * pDst,
  48. uint32_t numSamples)
  49. {
  50. uint32_t blkCnt; /* Loop counter */
  51. #if defined (ARM_MATH_DSP)
  52. q31_t in;
  53. q31_t acc0; /* Accumulators */
  54. #else
  55. q15_t real, imag; /* Temporary input variables */
  56. q31_t acc0, acc1; /* Accumulators */
  57. #endif
  58. #if defined (ARM_MATH_LOOPUNROLL)
  59. /* Loop unrolling: Compute 4 outputs at a time */
  60. blkCnt = numSamples >> 2U;
  61. while (blkCnt > 0U)
  62. {
  63. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  64. #if defined (ARM_MATH_DSP)
  65. in = read_q15x2_ia ((q15_t **) &pSrc);
  66. acc0 = __SMUAD(in, in);
  67. /* store result in 2.14 format in destination buffer. */
  68. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  69. in = read_q15x2_ia ((q15_t **) &pSrc);
  70. acc0 = __SMUAD(in, in);
  71. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  72. in = read_q15x2_ia ((q15_t **) &pSrc);
  73. acc0 = __SMUAD(in, in);
  74. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  75. in = read_q15x2_ia ((q15_t **) &pSrc);
  76. acc0 = __SMUAD(in, in);
  77. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  78. #else
  79. real = *pSrc++;
  80. imag = *pSrc++;
  81. acc0 = ((q31_t) real * real);
  82. acc1 = ((q31_t) imag * imag);
  83. /* store result in 2.14 format in destination buffer. */
  84. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  85. real = *pSrc++;
  86. imag = *pSrc++;
  87. acc0 = ((q31_t) real * real);
  88. acc1 = ((q31_t) imag * imag);
  89. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  90. real = *pSrc++;
  91. imag = *pSrc++;
  92. acc0 = ((q31_t) real * real);
  93. acc1 = ((q31_t) imag * imag);
  94. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  95. real = *pSrc++;
  96. imag = *pSrc++;
  97. acc0 = ((q31_t) real * real);
  98. acc1 = ((q31_t) imag * imag);
  99. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  100. #endif /* #if defined (ARM_MATH_DSP) */
  101. /* Decrement loop counter */
  102. blkCnt--;
  103. }
  104. /* Loop unrolling: Compute remaining outputs */
  105. blkCnt = numSamples % 0x4U;
  106. #else
  107. /* Initialize blkCnt with number of samples */
  108. blkCnt = numSamples;
  109. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  110. while (blkCnt > 0U)
  111. {
  112. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  113. #if defined (ARM_MATH_DSP)
  114. in = read_q15x2_ia ((q15_t **) &pSrc);
  115. acc0 = __SMUAD(in, in);
  116. /* store result in 2.14 format in destination buffer. */
  117. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  118. #else
  119. real = *pSrc++;
  120. imag = *pSrc++;
  121. acc0 = ((q31_t) real * real);
  122. acc1 = ((q31_t) imag * imag);
  123. /* store result in 2.14 format in destination buffer. */
  124. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  125. #endif
  126. /* Decrement loop counter */
  127. blkCnt--;
  128. }
  129. }
  130. /**
  131. @} end of cmplx_mag group
  132. */