arm_cmplx_mag_f32.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_f32.c
  4. * Description: Floating-point 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. * @defgroup cmplx_mag Complex Magnitude
  34. *
  35. * Computes the magnitude of the elements of a complex data vector.
  36. *
  37. * The <code>pSrc</code> points to the source data and
  38. * <code>pDst</code> points to the where the result should be written.
  39. * <code>numSamples</code> specifies the number of complex samples
  40. * in the input array and the data is stored in an interleaved fashion
  41. * (real, imag, real, imag, ...).
  42. * The input array has a total of <code>2*numSamples</code> values;
  43. * the output array has a total of <code>numSamples</code> values.
  44. * The underlying algorithm is used:
  45. *
  46. * <pre>
  47. * for(n=0; n<numSamples; n++) {
  48. * pDst[n] = sqrt(pSrc[(2*n)+0]^2 + pSrc[(2*n)+1]^2);
  49. * }
  50. * </pre>
  51. *
  52. * There are separate functions for floating-point, Q15, and Q31 data types.
  53. */
  54. /**
  55. * @addtogroup cmplx_mag
  56. * @{
  57. */
  58. /**
  59. * @brief Floating-point complex magnitude.
  60. * @param[in] *pSrc points to complex input buffer
  61. * @param[out] *pDst points to real output buffer
  62. * @param[in] numSamples number of complex samples in the input vector
  63. * @return none.
  64. *
  65. */
  66. void arm_cmplx_mag_f32(
  67. float32_t * pSrc,
  68. float32_t * pDst,
  69. uint32_t numSamples)
  70. {
  71. float32_t realIn, imagIn; /* Temporary variables to hold input values */
  72. #if defined (ARM_MATH_DSP)
  73. /* Run the below code for Cortex-M4 and Cortex-M3 */
  74. uint32_t blkCnt; /* loop counter */
  75. /*loop Unrolling */
  76. blkCnt = numSamples >> 2U;
  77. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  78. ** a second loop below computes the remaining 1 to 3 samples. */
  79. while (blkCnt > 0U)
  80. {
  81. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  82. realIn = *pSrc++;
  83. imagIn = *pSrc++;
  84. /* store the result in the destination buffer. */
  85. arm_sqrt_f32((realIn * realIn) + (imagIn * imagIn), pDst++);
  86. realIn = *pSrc++;
  87. imagIn = *pSrc++;
  88. arm_sqrt_f32((realIn * realIn) + (imagIn * imagIn), pDst++);
  89. realIn = *pSrc++;
  90. imagIn = *pSrc++;
  91. arm_sqrt_f32((realIn * realIn) + (imagIn * imagIn), pDst++);
  92. realIn = *pSrc++;
  93. imagIn = *pSrc++;
  94. arm_sqrt_f32((realIn * realIn) + (imagIn * imagIn), pDst++);
  95. /* Decrement the loop counter */
  96. blkCnt--;
  97. }
  98. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  99. ** No loop unrolling is used. */
  100. blkCnt = numSamples % 0x4U;
  101. while (blkCnt > 0U)
  102. {
  103. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  104. realIn = *pSrc++;
  105. imagIn = *pSrc++;
  106. /* store the result in the destination buffer. */
  107. arm_sqrt_f32((realIn * realIn) + (imagIn * imagIn), pDst++);
  108. /* Decrement the loop counter */
  109. blkCnt--;
  110. }
  111. #else
  112. /* Run the below code for Cortex-M0 */
  113. while (numSamples > 0U)
  114. {
  115. /* out = sqrt((real * real) + (imag * imag)) */
  116. realIn = *pSrc++;
  117. imagIn = *pSrc++;
  118. /* store the result in the destination buffer. */
  119. arm_sqrt_f32((realIn * realIn) + (imagIn * imagIn), pDst++);
  120. /* Decrement the loop counter */
  121. numSamples--;
  122. }
  123. #endif /* #if defined (ARM_MATH_DSP) */
  124. }
  125. /**
  126. * @} end of cmplx_mag group
  127. */