arm_cmplx_mag_q31.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_q31.c
  4. * Description: Q31 complex magnitude
  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. /**
  30. * @ingroup groupCmplxMath
  31. */
  32. /**
  33. * @addtogroup cmplx_mag
  34. * @{
  35. */
  36. /**
  37. * @brief Q31 complex magnitude
  38. * @param *pSrc points to the complex input vector
  39. * @param *pDst points to the real output vector
  40. * @param numSamples number of complex samples in the input vector
  41. * @return none.
  42. *
  43. * <b>Scaling and Overflow Behavior:</b>
  44. * \par
  45. * The function implements 1.31 by 1.31 multiplications and finally output is converted into 2.30 format.
  46. * Input down scaling is not required.
  47. */
  48. void arm_cmplx_mag_q31(
  49. q31_t * pSrc,
  50. q31_t * pDst,
  51. uint32_t numSamples)
  52. {
  53. q31_t real, imag; /* Temporary variables to hold input values */
  54. q31_t acc0, acc1; /* Accumulators */
  55. uint32_t blkCnt; /* loop counter */
  56. #if defined (ARM_MATH_DSP)
  57. /* Run the below code for Cortex-M4 and Cortex-M3 */
  58. q31_t real1, real2, imag1, imag2; /* Temporary variables to hold input values */
  59. q31_t out1, out2, out3, out4; /* Accumulators */
  60. q63_t mul1, mul2, mul3, mul4; /* Temporary variables */
  61. /*loop Unrolling */
  62. blkCnt = numSamples >> 2U;
  63. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  64. ** a second loop below computes the remaining 1 to 3 samples. */
  65. while (blkCnt > 0U)
  66. {
  67. /* read complex input from source buffer */
  68. real1 = pSrc[0];
  69. imag1 = pSrc[1];
  70. real2 = pSrc[2];
  71. imag2 = pSrc[3];
  72. /* calculate power of input values */
  73. mul1 = (q63_t) real1 *real1;
  74. mul2 = (q63_t) imag1 *imag1;
  75. mul3 = (q63_t) real2 *real2;
  76. mul4 = (q63_t) imag2 *imag2;
  77. /* get the result to 3.29 format */
  78. out1 = (q31_t) (mul1 >> 33);
  79. out2 = (q31_t) (mul2 >> 33);
  80. out3 = (q31_t) (mul3 >> 33);
  81. out4 = (q31_t) (mul4 >> 33);
  82. /* add real and imaginary accumulators */
  83. out1 = out1 + out2;
  84. out3 = out3 + out4;
  85. /* read complex input from source buffer */
  86. real1 = pSrc[4];
  87. imag1 = pSrc[5];
  88. real2 = pSrc[6];
  89. imag2 = pSrc[7];
  90. /* calculate square root */
  91. arm_sqrt_q31(out1, &pDst[0]);
  92. /* calculate power of input values */
  93. mul1 = (q63_t) real1 *real1;
  94. /* calculate square root */
  95. arm_sqrt_q31(out3, &pDst[1]);
  96. /* calculate power of input values */
  97. mul2 = (q63_t) imag1 *imag1;
  98. mul3 = (q63_t) real2 *real2;
  99. mul4 = (q63_t) imag2 *imag2;
  100. /* get the result to 3.29 format */
  101. out1 = (q31_t) (mul1 >> 33);
  102. out2 = (q31_t) (mul2 >> 33);
  103. out3 = (q31_t) (mul3 >> 33);
  104. out4 = (q31_t) (mul4 >> 33);
  105. /* add real and imaginary accumulators */
  106. out1 = out1 + out2;
  107. out3 = out3 + out4;
  108. /* calculate square root */
  109. arm_sqrt_q31(out1, &pDst[2]);
  110. /* increment destination by 8 to process next samples */
  111. pSrc += 8U;
  112. /* calculate square root */
  113. arm_sqrt_q31(out3, &pDst[3]);
  114. /* increment destination by 4 to process next samples */
  115. pDst += 4U;
  116. /* Decrement the loop counter */
  117. blkCnt--;
  118. }
  119. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  120. ** No loop unrolling is used. */
  121. blkCnt = numSamples % 0x4U;
  122. #else
  123. /* Run the below code for Cortex-M0 */
  124. blkCnt = numSamples;
  125. #endif /* #if defined (ARM_MATH_DSP) */
  126. while (blkCnt > 0U)
  127. {
  128. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  129. real = *pSrc++;
  130. imag = *pSrc++;
  131. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  132. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  133. /* store the result in 2.30 format in the destination buffer. */
  134. arm_sqrt_q31(acc0 + acc1, pDst++);
  135. /* Decrement the loop counter */
  136. blkCnt--;
  137. }
  138. }
  139. /**
  140. * @} end of cmplx_mag group
  141. */