arm_cmplx_mag_f32.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_f32.c
  4. * Description: Floating-point complex magnitude
  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/complex_math_functions.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. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  62. #include "arm_vec_math.h"
  63. #endif
  64. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  65. #include "arm_helium_utils.h"
  66. void arm_cmplx_mag_f32(
  67. const float32_t * pSrc,
  68. float32_t * pDst,
  69. uint32_t numSamples)
  70. {
  71. int32_t blockSize = numSamples; /* loop counters */
  72. uint32_t blkCnt; /* loop counters */
  73. f32x4x2_t vecSrc;
  74. f32x4_t sum;
  75. float32_t real, imag; /* Temporary variables to hold input values */
  76. /* Compute 4 complex samples at a time */
  77. blkCnt = blockSize >> 2;
  78. while (blkCnt > 0U)
  79. {
  80. q31x4_t newtonStartVec;
  81. f32x4_t sumHalf, invSqrt;
  82. vecSrc = vld2q(pSrc);
  83. pSrc += 8;
  84. sum = vmulq(vecSrc.val[0], vecSrc.val[0]);
  85. sum = vfmaq(sum, vecSrc.val[1], vecSrc.val[1]);
  86. /*
  87. * inlined Fast SQRT using inverse SQRT newton-raphson method
  88. */
  89. /* compute initial value */
  90. newtonStartVec = vdupq_n_s32(INVSQRT_MAGIC_F32) - vshrq((q31x4_t) sum, 1);
  91. sumHalf = sum * 0.5f;
  92. /*
  93. * compute 3 x iterations
  94. *
  95. * The more iterations, the more accuracy.
  96. * If you need to trade a bit of accuracy for more performance,
  97. * you can comment out the 3rd use of the macro.
  98. */
  99. INVSQRT_NEWTON_MVE_F32(invSqrt, sumHalf, (f32x4_t) newtonStartVec);
  100. INVSQRT_NEWTON_MVE_F32(invSqrt, sumHalf, invSqrt);
  101. INVSQRT_NEWTON_MVE_F32(invSqrt, sumHalf, invSqrt);
  102. /*
  103. * set negative values to 0
  104. */
  105. invSqrt = vdupq_m(invSqrt, 0.0f, vcmpltq(invSqrt, 0.0f));
  106. /*
  107. * sqrt(x) = x * invSqrt(x)
  108. */
  109. sum = vmulq(sum, invSqrt);
  110. vst1q(pDst, sum);
  111. pDst += 4;
  112. /*
  113. * Decrement the blockSize loop counter
  114. */
  115. blkCnt--;
  116. }
  117. /*
  118. * tail
  119. */
  120. blkCnt = blockSize & 3;
  121. while (blkCnt > 0U)
  122. {
  123. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  124. real = *pSrc++;
  125. imag = *pSrc++;
  126. /* store result in destination buffer. */
  127. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  128. /* Decrement loop counter */
  129. blkCnt--;
  130. }
  131. }
  132. #else
  133. void arm_cmplx_mag_f32(
  134. const float32_t * pSrc,
  135. float32_t * pDst,
  136. uint32_t numSamples)
  137. {
  138. uint32_t blkCnt; /* loop counter */
  139. float32_t real, imag; /* Temporary variables to hold input values */
  140. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  141. float32x4x2_t vecA;
  142. float32x4_t vRealA;
  143. float32x4_t vImagA;
  144. float32x4_t vMagSqA;
  145. float32x4x2_t vecB;
  146. float32x4_t vRealB;
  147. float32x4_t vImagB;
  148. float32x4_t vMagSqB;
  149. /* Loop unrolling: Compute 8 outputs at a time */
  150. blkCnt = numSamples >> 3;
  151. while (blkCnt > 0U)
  152. {
  153. /* out = sqrt((real * real) + (imag * imag)) */
  154. vecA = vld2q_f32(pSrc);
  155. pSrc += 8;
  156. vecB = vld2q_f32(pSrc);
  157. pSrc += 8;
  158. vRealA = vmulq_f32(vecA.val[0], vecA.val[0]);
  159. vImagA = vmulq_f32(vecA.val[1], vecA.val[1]);
  160. vMagSqA = vaddq_f32(vRealA, vImagA);
  161. vRealB = vmulq_f32(vecB.val[0], vecB.val[0]);
  162. vImagB = vmulq_f32(vecB.val[1], vecB.val[1]);
  163. vMagSqB = vaddq_f32(vRealB, vImagB);
  164. /* Store the result in the destination buffer. */
  165. vst1q_f32(pDst, __arm_vec_sqrt_f32_neon(vMagSqA));
  166. pDst += 4;
  167. vst1q_f32(pDst, __arm_vec_sqrt_f32_neon(vMagSqB));
  168. pDst += 4;
  169. /* Decrement the loop counter */
  170. blkCnt--;
  171. }
  172. blkCnt = numSamples & 7;
  173. #else
  174. #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
  175. /* Loop unrolling: Compute 4 outputs at a time */
  176. blkCnt = numSamples >> 2U;
  177. while (blkCnt > 0U)
  178. {
  179. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  180. real = *pSrc++;
  181. imag = *pSrc++;
  182. /* store result in destination buffer. */
  183. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  184. real = *pSrc++;
  185. imag = *pSrc++;
  186. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  187. real = *pSrc++;
  188. imag = *pSrc++;
  189. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  190. real = *pSrc++;
  191. imag = *pSrc++;
  192. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  193. /* Decrement loop counter */
  194. blkCnt--;
  195. }
  196. /* Loop unrolling: Compute remaining outputs */
  197. blkCnt = numSamples % 0x4U;
  198. #else
  199. /* Initialize blkCnt with number of samples */
  200. blkCnt = numSamples;
  201. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  202. #endif /* #if defined(ARM_MATH_NEON) */
  203. while (blkCnt > 0U)
  204. {
  205. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  206. real = *pSrc++;
  207. imag = *pSrc++;
  208. /* store result in destination buffer. */
  209. arm_sqrt_f32((real * real) + (imag * imag), pDst++);
  210. /* Decrement loop counter */
  211. blkCnt--;
  212. }
  213. }
  214. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  215. /**
  216. @} end of cmplx_mag group
  217. */