arm_cmplx_mag_q15.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_q15.c
  4. * Description: Q15 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 Q15 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.15 by 1.15 multiplications and finally output is converted into 2.14 format.
  46. */
  47. void arm_cmplx_mag_q15(
  48. q15_t * pSrc,
  49. q15_t * pDst,
  50. uint32_t numSamples)
  51. {
  52. q31_t acc0, acc1; /* Accumulators */
  53. #if defined (ARM_MATH_DSP)
  54. /* Run the below code for Cortex-M4 and Cortex-M3 */
  55. uint32_t blkCnt; /* loop counter */
  56. q31_t in1, in2, in3, in4;
  57. q31_t acc2, acc3;
  58. /*loop Unrolling */
  59. blkCnt = numSamples >> 2U;
  60. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  61. ** a second loop below computes the remaining 1 to 3 samples. */
  62. while (blkCnt > 0U)
  63. {
  64. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  65. in1 = *__SIMD32(pSrc)++;
  66. in2 = *__SIMD32(pSrc)++;
  67. in3 = *__SIMD32(pSrc)++;
  68. in4 = *__SIMD32(pSrc)++;
  69. acc0 = __SMUAD(in1, in1);
  70. acc1 = __SMUAD(in2, in2);
  71. acc2 = __SMUAD(in3, in3);
  72. acc3 = __SMUAD(in4, in4);
  73. /* store the result in 2.14 format in the destination buffer. */
  74. arm_sqrt_q15((q15_t) ((acc0) >> 17), pDst++);
  75. arm_sqrt_q15((q15_t) ((acc1) >> 17), pDst++);
  76. arm_sqrt_q15((q15_t) ((acc2) >> 17), pDst++);
  77. arm_sqrt_q15((q15_t) ((acc3) >> 17), pDst++);
  78. /* Decrement the loop counter */
  79. blkCnt--;
  80. }
  81. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  82. ** No loop unrolling is used. */
  83. blkCnt = numSamples % 0x4U;
  84. while (blkCnt > 0U)
  85. {
  86. /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
  87. in1 = *__SIMD32(pSrc)++;
  88. acc0 = __SMUAD(in1, in1);
  89. /* store the result in 2.14 format in the destination buffer. */
  90. arm_sqrt_q15((q15_t) (acc0 >> 17), pDst++);
  91. /* Decrement the loop counter */
  92. blkCnt--;
  93. }
  94. #else
  95. /* Run the below code for Cortex-M0 */
  96. q15_t real, imag; /* Temporary variables to hold input values */
  97. while (numSamples > 0U)
  98. {
  99. /* out = sqrt(real * real + imag * imag) */
  100. real = *pSrc++;
  101. imag = *pSrc++;
  102. acc0 = (real * real);
  103. acc1 = (imag * imag);
  104. /* store the result in 2.14 format in the destination buffer. */
  105. arm_sqrt_q15((q15_t) (((q63_t) acc0 + acc1) >> 17), pDst++);
  106. /* Decrement the loop counter */
  107. numSamples--;
  108. }
  109. #endif /* #if defined (ARM_MATH_DSP) */
  110. }
  111. /**
  112. * @} end of cmplx_mag group
  113. */