arm_cmplx_mag_squared_f32.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_cmplx_mag_squared_f32.c
  4. * Description: Floating-point 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. * @defgroup cmplx_mag_squared Complex Magnitude Squared
  34. *
  35. * Computes the magnitude squared 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. *
  45. * The underlying algorithm is used:
  46. *
  47. * <pre>
  48. * for(n=0; n<numSamples; n++) {
  49. * pDst[n] = pSrc[(2*n)+0]^2 + pSrc[(2*n)+1]^2;
  50. * }
  51. * </pre>
  52. *
  53. * There are separate functions for floating-point, Q15, and Q31 data types.
  54. */
  55. /**
  56. * @addtogroup cmplx_mag_squared
  57. * @{
  58. */
  59. /**
  60. * @brief Floating-point complex magnitude squared
  61. * @param[in] *pSrc points to the complex input vector
  62. * @param[out] *pDst points to the real output vector
  63. * @param[in] numSamples number of complex samples in the input vector
  64. * @return none.
  65. */
  66. void arm_cmplx_mag_squared_f32(
  67. float32_t * pSrc,
  68. float32_t * pDst,
  69. uint32_t numSamples)
  70. {
  71. float32_t real, imag; /* Temporary variables to store real and imaginary values */
  72. uint32_t blkCnt; /* loop counter */
  73. #if defined (ARM_MATH_DSP)
  74. float32_t real1, real2, real3, real4; /* Temporary variables to hold real values */
  75. float32_t imag1, imag2, imag3, imag4; /* Temporary variables to hold imaginary values */
  76. float32_t mul1, mul2, mul3, mul4; /* Temporary variables */
  77. float32_t mul5, mul6, mul7, mul8; /* Temporary variables */
  78. float32_t out1, out2, out3, out4; /* Temporary variables to hold output values */
  79. /*loop Unrolling */
  80. blkCnt = numSamples >> 2U;
  81. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  82. ** a second loop below computes the remaining 1 to 3 samples. */
  83. while (blkCnt > 0U)
  84. {
  85. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  86. /* read real input sample from source buffer */
  87. real1 = pSrc[0];
  88. /* read imaginary input sample from source buffer */
  89. imag1 = pSrc[1];
  90. /* calculate power of real value */
  91. mul1 = real1 * real1;
  92. /* read real input sample from source buffer */
  93. real2 = pSrc[2];
  94. /* calculate power of imaginary value */
  95. mul2 = imag1 * imag1;
  96. /* read imaginary input sample from source buffer */
  97. imag2 = pSrc[3];
  98. /* calculate power of real value */
  99. mul3 = real2 * real2;
  100. /* read real input sample from source buffer */
  101. real3 = pSrc[4];
  102. /* calculate power of imaginary value */
  103. mul4 = imag2 * imag2;
  104. /* read imaginary input sample from source buffer */
  105. imag3 = pSrc[5];
  106. /* calculate power of real value */
  107. mul5 = real3 * real3;
  108. /* calculate power of imaginary value */
  109. mul6 = imag3 * imag3;
  110. /* read real input sample from source buffer */
  111. real4 = pSrc[6];
  112. /* accumulate real and imaginary powers */
  113. out1 = mul1 + mul2;
  114. /* read imaginary input sample from source buffer */
  115. imag4 = pSrc[7];
  116. /* accumulate real and imaginary powers */
  117. out2 = mul3 + mul4;
  118. /* calculate power of real value */
  119. mul7 = real4 * real4;
  120. /* calculate power of imaginary value */
  121. mul8 = imag4 * imag4;
  122. /* store output to destination */
  123. pDst[0] = out1;
  124. /* accumulate real and imaginary powers */
  125. out3 = mul5 + mul6;
  126. /* store output to destination */
  127. pDst[1] = out2;
  128. /* accumulate real and imaginary powers */
  129. out4 = mul7 + mul8;
  130. /* store output to destination */
  131. pDst[2] = out3;
  132. /* increment destination pointer by 8 to process next samples */
  133. pSrc += 8U;
  134. /* store output to destination */
  135. pDst[3] = out4;
  136. /* increment destination pointer by 4 to process next samples */
  137. pDst += 4U;
  138. /* Decrement the loop counter */
  139. blkCnt--;
  140. }
  141. /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
  142. ** No loop unrolling is used. */
  143. blkCnt = numSamples % 0x4U;
  144. #else
  145. /* Run the below code for Cortex-M0 */
  146. blkCnt = numSamples;
  147. #endif /* #if defined (ARM_MATH_DSP) */
  148. while (blkCnt > 0U)
  149. {
  150. /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
  151. real = *pSrc++;
  152. imag = *pSrc++;
  153. /* out = (real * real) + (imag * imag) */
  154. /* store the result in the destination buffer. */
  155. *pDst++ = (real * real) + (imag * imag);
  156. /* Decrement the loop counter */
  157. blkCnt--;
  158. }
  159. }
  160. /**
  161. * @} end of cmplx_mag_squared group
  162. */