1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_cmplx_mag_q31.c
4 * Description: Q31 complex magnitude
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
32 * @ingroup groupCmplxMath
36 * @addtogroup cmplx_mag
41 * @brief Q31 complex magnitude
42 * @param *pSrc points to the complex input vector
43 * @param *pDst points to the real output vector
44 * @param numSamples number of complex samples in the input vector
47 * <b>Scaling and Overflow Behavior:</b>
49 * The function implements 1.31 by 1.31 multiplications and finally output is converted into 2.30 format.
50 * Input down scaling is not required.
53 void arm_cmplx_mag_q31(
58 q31_t real
, imag
; /* Temporary variables to hold input values */
59 q31_t acc0
, acc1
; /* Accumulators */
60 uint32_t blkCnt
; /* loop counter */
62 #if defined (ARM_MATH_DSP)
64 /* Run the below code for Cortex-M4 and Cortex-M3 */
65 q31_t real1
, real2
, imag1
, imag2
; /* Temporary variables to hold input values */
66 q31_t out1
, out2
, out3
, out4
; /* Accumulators */
67 q63_t mul1
, mul2
, mul3
, mul4
; /* Temporary variables */
71 blkCnt
= numSamples
>> 2U;
73 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
74 ** a second loop below computes the remaining 1 to 3 samples. */
77 /* read complex input from source buffer */
83 /* calculate power of input values */
84 mul1
= (q63_t
) real1
*real1
;
85 mul2
= (q63_t
) imag1
*imag1
;
86 mul3
= (q63_t
) real2
*real2
;
87 mul4
= (q63_t
) imag2
*imag2
;
89 /* get the result to 3.29 format */
90 out1
= (q31_t
) (mul1
>> 33);
91 out2
= (q31_t
) (mul2
>> 33);
92 out3
= (q31_t
) (mul3
>> 33);
93 out4
= (q31_t
) (mul4
>> 33);
95 /* add real and imaginary accumulators */
99 /* read complex input from source buffer */
105 /* calculate square root */
106 arm_sqrt_q31(out1
, &pDst
[0]);
108 /* calculate power of input values */
109 mul1
= (q63_t
) real1
*real1
;
111 /* calculate square root */
112 arm_sqrt_q31(out3
, &pDst
[1]);
114 /* calculate power of input values */
115 mul2
= (q63_t
) imag1
*imag1
;
116 mul3
= (q63_t
) real2
*real2
;
117 mul4
= (q63_t
) imag2
*imag2
;
119 /* get the result to 3.29 format */
120 out1
= (q31_t
) (mul1
>> 33);
121 out2
= (q31_t
) (mul2
>> 33);
122 out3
= (q31_t
) (mul3
>> 33);
123 out4
= (q31_t
) (mul4
>> 33);
125 /* add real and imaginary accumulators */
129 /* calculate square root */
130 arm_sqrt_q31(out1
, &pDst
[2]);
132 /* increment destination by 8 to process next samples */
135 /* calculate square root */
136 arm_sqrt_q31(out3
, &pDst
[3]);
138 /* increment destination by 4 to process next samples */
141 /* Decrement the loop counter */
145 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
146 ** No loop unrolling is used. */
147 blkCnt
= numSamples
% 0x4U
;
151 /* Run the below code for Cortex-M0 */
154 #endif /* #if defined (ARM_MATH_DSP) */
158 /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
161 acc0
= (q31_t
) (((q63_t
) real
* real
) >> 33);
162 acc1
= (q31_t
) (((q63_t
) imag
* imag
) >> 33);
163 /* store the result in 2.30 format in the destination buffer. */
164 arm_sqrt_q31(acc0
+ acc1
, pDst
++);
166 /* Decrement the loop counter */
172 * @} end of cmplx_mag group