1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_cmplx_mag_q31.c
10 * Description: Q31 complex magnitude
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * ---------------------------------------------------------------------------- */
33 * @ingroup groupCmplxMath
37 * @addtogroup cmplx_mag
42 * @brief Q31 complex magnitude
43 * @param *pSrc points to the complex input vector
44 * @param *pDst points to the real output vector
45 * @param numSamples number of complex samples in the input vector
48 * <b>Scaling and Overflow Behavior:</b>
50 * The function implements 1.31 by 1.31 multiplications and finally output is converted into 2.30 format.
51 * Input down scaling is not required.
54 void arm_cmplx_mag_q31(
59 q31_t real, imag; /* Temporary variables to hold input values */
60 q31_t acc0, acc1; /* Accumulators */
64 /* Run the below code for Cortex-M4 and Cortex-M3 */
65 uint32_t blkCnt; /* loop counter */
69 blkCnt = numSamples >> 2u;
71 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
72 ** a second loop below computes the remaining 1 to 3 samples. */
76 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
79 acc0 = (q31_t) (((q63_t) real * real) >> 33);
80 acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
81 /* store the result in 2.30 format in the destination buffer. */
82 arm_sqrt_q31(acc0 + acc1, pDst++);
86 acc0 = (q31_t) (((q63_t) real * real) >> 33);
87 acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
88 /* store the result in 2.30 format in the destination buffer. */
89 arm_sqrt_q31(acc0 + acc1, pDst++);
93 acc0 = (q31_t) (((q63_t) real * real) >> 33);
94 acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
95 /* store the result in 2.30 format in the destination buffer. */
96 arm_sqrt_q31(acc0 + acc1, pDst++);
100 acc0 = (q31_t) (((q63_t) real * real) >> 33);
101 acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
102 /* store the result in 2.30 format in the destination buffer. */
103 arm_sqrt_q31(acc0 + acc1, pDst++);
105 /* Decrement the loop counter */
109 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
110 ** No loop unrolling is used. */
111 blkCnt = numSamples % 0x4u;
115 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
118 acc0 = (q31_t) (((q63_t) real * real) >> 33);
119 acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
120 /* store the result in 2.30 format in the destination buffer. */
121 arm_sqrt_q31(acc0 + acc1, pDst++);
123 /* Decrement the loop counter */
129 /* Run the below code for Cortex-M0 */
131 while(numSamples > 0u)
133 /* out = sqrt((real * real) + (imag * imag)) */
136 acc0 = (q31_t) (((q63_t) real * real) >> 33);
137 acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
138 /* store the result in 2.30 format in the destination buffer. */
139 arm_sqrt_q31(acc0 + acc1, pDst++);
141 /* Decrement the loop counter */
145 #endif /* #ifndef ARM_MATH_CM0 */
150 * @} end of cmplx_mag group