arm_cmplx_mag_squared_q31.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_squared_q31.c
  4. * Description: Q31 complex magnitude squared
  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_squared
  34. * @{
  35. */
  36. /**
  37. * @brief Q31 complex magnitude squared
  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 3.29 format.
  46. * Input down scaling is not required.
  47. */
  48. void arm_cmplx_mag_squared_q31(
  49. q31_t * pSrc,
  50. q31_t * pDst,
  51. uint32_t numSamples)
  52. {
  53. q31_t real, imag; /* Temporary variables to store real and imaginary values */
  54. q31_t acc0, acc1; /* Accumulators */
  55. #if defined (ARM_MATH_DSP)
  56. /* Run the below code for Cortex-M4 and Cortex-M3 */
  57. uint32_t blkCnt; /* loop counter */
  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] = (A[0] * A[0] + A[1] * A[1]) */
  65. real = *pSrc++;
  66. imag = *pSrc++;
  67. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  68. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  69. /* store the result in 3.29 format in the destination buffer. */
  70. *pDst++ = acc0 + acc1;
  71. real = *pSrc++;
  72. imag = *pSrc++;
  73. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  74. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  75. /* store the result in 3.29 format in the destination buffer. */
  76. *pDst++ = acc0 + acc1;
  77. real = *pSrc++;
  78. imag = *pSrc++;
  79. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  80. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  81. /* store the result in 3.29 format in the destination buffer. */
  82. *pDst++ = acc0 + acc1;
  83. real = *pSrc++;
  84. imag = *pSrc++;
  85. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  86. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  87. /* store the result in 3.29 format in the destination buffer. */
  88. *pDst++ = acc0 + acc1;
  89. /* Decrement the loop counter */
  90. blkCnt--;
  91. }
  92. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  93. ** No loop unrolling is used. */
  94. blkCnt = numSamples % 0x4U;
  95. while (blkCnt > 0U)
  96. {
  97. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  98. real = *pSrc++;
  99. imag = *pSrc++;
  100. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  101. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  102. /* store the result in 3.29 format in the destination buffer. */
  103. *pDst++ = acc0 + acc1;
  104. /* Decrement the loop counter */
  105. blkCnt--;
  106. }
  107. #else
  108. /* Run the below code for Cortex-M0 */
  109. while (numSamples > 0U)
  110. {
  111. /* out = ((real * real) + (imag * imag)) */
  112. real = *pSrc++;
  113. imag = *pSrc++;
  114. acc0 = (q31_t) (((q63_t) real * real) >> 33);
  115. acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
  116. /* store the result in 3.29 format in the destination buffer. */
  117. *pDst++ = acc0 + acc1;
  118. /* Decrement the loop counter */
  119. numSamples--;
  120. }
  121. #endif /* #if defined (ARM_MATH_DSP) */
  122. }
  123. /**
  124. * @} end of cmplx_mag_squared group
  125. */