arm_cmplx_mag_f32.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_f32.c
  4. * Description: Floating-point 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. @defgroup cmplx_mag Complex Magnitude
  34. Computes the magnitude of the elements of a complex data vector.
  35. The <code>pSrc</code> points to the source data and
  36. <code>pDst</code> points to the where the result should be written.
  37. <code>numSamples</code> specifies the number of complex samples
  38. in the input array and the data is stored in an interleaved fashion
  39. (real, imag, real, imag, ...).
  40. The input array has a total of <code>2*numSamples</code> values;
  41. the output array has a total of <code>numSamples</code> values.
  42. The underlying algorithm is used:
  43. <pre>
  44. for (n = 0; n < numSamples; n++) {
  45. pDst[n] = sqrt(pSrc[(2*n)+0]^2 + pSrc[(2*n)+1]^2);
  46. }
  47. </pre>
  48. There are separate functions for floating-point, Q15, and Q31 data types.
  49. */
  50. /**
  51. @addtogroup cmplx_mag
  52. @{
  53. */
  54. /**
  55. @brief Floating-point complex magnitude.
  56. @param[in] pSrc points to input vector
  57. @param[out] pDst points to output vector
  58. @param[in] numSamples number of samples in each vector
  59. @return none
  60. */
  61. void arm_cmplx_mag_f32(
  62. const float32_t * pSrc,
  63. float32_t * pDst,
  64. uint32_t numSamples)
  65. {
  66. uint32_t blkCnt; /* loop counter */
  67. float32_t real, imag; /* Temporary variables to hold input values */
  68. #if defined(ARM_MATH_NEON)
  69. float32x4x2_t vecA;
  70. float32x4_t vRealA;
  71. float32x4_t vImagA;
  72. float32x4_t vMagSqA;
  73. float32x4x2_t vecB;
  74. float32x4_t vRealB;
  75. float32x4_t vImagB;
  76. float32x4_t vMagSqB;
  77. /* Loop unrolling: Compute 8 outputs at a time */
  78. blkCnt = numSamples >> 3;
  79. while (blkCnt > 0U)
  80. {
  81. /* out = sqrt((real * real) + (imag * imag)) */
  82. vecA = vld2q_f32(pSrc);
  83. pSrc += 8;
  84. vecB = vld2q_f32(pSrc);
  85. pSrc += 8;
  86. vRealA = vmulq_f32(vecA.val[0], vecA.val[0]);
  87. vImagA = vmulq_f32(vecA.val[1], vecA.val[1]);
  88. vMagSqA = vaddq_f32(vRealA, vImagA);
  89. vRealB = vmulq_f32(vecB.val[0], vecB.val[0]);
  90. vImagB = vmulq_f32(vecB.val[1], vecB.val[1]);
  91. vMagSqB = vaddq_f32(vRealB, vImagB);
  92. /* Store the result in the destination buffer. */
  93. vst1q_f32(pDst, __arm_vec_sqrt_f32_neon(vMagSqA));
  94. pDst += 4;
  95. vst1q_f32(pDst, __arm_vec_sqrt_f32_neon(vMagSqB));
  96. pDst += 4;
  97. /* Decrement the loop counter */
  98. blkCnt--;
  99. }
  100. blkCnt = numSamples & 7;
  101. #else
  102. #if defined (ARM_MATH_LOOPUNROLL)
  103. /* Loop unrolling: Compute 4 outputs at a time */
  104. blkCnt = numSamples >> 2U;
  105. while (blkCnt > 0U)
  106. {
  107. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  108. real = *pSrc++;
  109. imag = *pSrc++;
  110. /* store result in destination buffer. */
  111. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  112. real = *pSrc++;
  113. imag = *pSrc++;
  114. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  115. real = *pSrc++;
  116. imag = *pSrc++;
  117. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  118. real = *pSrc++;
  119. imag = *pSrc++;
  120. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  121. /* Decrement loop counter */
  122. blkCnt--;
  123. }
  124. /* Loop unrolling: Compute remaining outputs */
  125. blkCnt = numSamples % 0x4U;
  126. #else
  127. /* Initialize blkCnt with number of samples */
  128. blkCnt = numSamples;
  129. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  130. #endif /* #if defined(ARM_MATH_NEON) */
  131. while (blkCnt > 0U)
  132. {
  133. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  134. real = *pSrc++;
  135. imag = *pSrc++;
  136. /* store result in destination buffer. */
  137. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  138. /* Decrement loop counter */
  139. blkCnt--;
  140. }
  141. }
  142. /**
  143. @} end of cmplx_mag group
  144. */