Aded CMSIS5 DSP and NN folder. Needs some work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_cmplx_mag_q31.c Source File

arm_cmplx_mag_q31.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_cmplx_mag_q31.c
00004  * Description:  Q31 complex magnitude
00005  *
00006  * $Date:        27. January 2017
00007  * $Revision:    V.1.5.1
00008  *
00009  * Target Processor: Cortex-M cores
00010  * -------------------------------------------------------------------- */
00011 /*
00012  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
00013  *
00014  * SPDX-License-Identifier: Apache-2.0
00015  *
00016  * Licensed under the Apache License, Version 2.0 (the License); you may
00017  * not use this file except in compliance with the License.
00018  * You may obtain a copy of the License at
00019  *
00020  * www.apache.org/licenses/LICENSE-2.0
00021  *
00022  * Unless required by applicable law or agreed to in writing, software
00023  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00024  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00025  * See the License for the specific language governing permissions and
00026  * limitations under the License.
00027  */
00028 
00029 #include "arm_math.h"
00030 
00031 /**
00032  * @ingroup groupCmplxMath
00033  */
00034 
00035 /**
00036  * @addtogroup cmplx_mag
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief  Q31 complex magnitude
00042  * @param  *pSrc points to the complex input vector
00043  * @param  *pDst points to the real output vector
00044  * @param  numSamples number of complex samples in the input vector
00045  * @return none.
00046  *
00047  * <b>Scaling and Overflow Behavior:</b>
00048  * \par
00049  * The function implements 1.31 by 1.31 multiplications and finally output is converted into 2.30 format.
00050  * Input down scaling is not required.
00051  */
00052 
00053 void arm_cmplx_mag_q31(
00054   q31_t * pSrc,
00055   q31_t * pDst,
00056   uint32_t numSamples)
00057 {
00058   q31_t real, imag;                              /* Temporary variables to hold input values */
00059   q31_t acc0, acc1;                              /* Accumulators */
00060   uint32_t blkCnt;                               /* loop counter */
00061 
00062 #if defined (ARM_MATH_DSP)
00063 
00064   /* Run the below code for Cortex-M4 and Cortex-M3 */
00065   q31_t real1, real2, imag1, imag2;              /* Temporary variables to hold input values */
00066   q31_t out1, out2, out3, out4;                  /* Accumulators */
00067   q63_t mul1, mul2, mul3, mul4;                  /* Temporary variables */
00068 
00069 
00070   /*loop Unrolling */
00071   blkCnt = numSamples >> 2U;
00072 
00073   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00074    ** a second loop below computes the remaining 1 to 3 samples. */
00075   while (blkCnt > 0U)
00076   {
00077     /* read complex input from source buffer */
00078     real1 = pSrc[0];
00079     imag1 = pSrc[1];
00080     real2 = pSrc[2];
00081     imag2 = pSrc[3];
00082 
00083     /* calculate power of input values */
00084     mul1 = (q63_t) real1 *real1;
00085     mul2 = (q63_t) imag1 *imag1;
00086     mul3 = (q63_t) real2 *real2;
00087     mul4 = (q63_t) imag2 *imag2;
00088 
00089     /* get the result to 3.29 format */
00090     out1 = (q31_t) (mul1 >> 33);
00091     out2 = (q31_t) (mul2 >> 33);
00092     out3 = (q31_t) (mul3 >> 33);
00093     out4 = (q31_t) (mul4 >> 33);
00094 
00095     /* add real and imaginary accumulators */
00096     out1 = out1 + out2;
00097     out3 = out3 + out4;
00098 
00099     /* read complex input from source buffer */
00100     real1 = pSrc[4];
00101     imag1 = pSrc[5];
00102     real2 = pSrc[6];
00103     imag2 = pSrc[7];
00104 
00105     /* calculate square root */
00106     arm_sqrt_q31(out1, &pDst[0]);
00107 
00108     /* calculate power of input values */
00109     mul1 = (q63_t) real1 *real1;
00110 
00111     /* calculate square root */
00112     arm_sqrt_q31(out3, &pDst[1]);
00113 
00114     /* calculate power of input values */
00115     mul2 = (q63_t) imag1 *imag1;
00116     mul3 = (q63_t) real2 *real2;
00117     mul4 = (q63_t) imag2 *imag2;
00118 
00119     /* get the result to 3.29 format */
00120     out1 = (q31_t) (mul1 >> 33);
00121     out2 = (q31_t) (mul2 >> 33);
00122     out3 = (q31_t) (mul3 >> 33);
00123     out4 = (q31_t) (mul4 >> 33);
00124 
00125     /* add real and imaginary accumulators */
00126     out1 = out1 + out2;
00127     out3 = out3 + out4;
00128 
00129     /* calculate square root */
00130     arm_sqrt_q31(out1, &pDst[2]);
00131 
00132     /* increment destination by 8 to process next samples */
00133     pSrc += 8U;
00134 
00135     /* calculate square root */
00136     arm_sqrt_q31(out3, &pDst[3]);
00137 
00138     /* increment destination by 4 to process next samples */
00139     pDst += 4U;
00140 
00141     /* Decrement the loop counter */
00142     blkCnt--;
00143   }
00144 
00145   /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
00146    ** No loop unrolling is used. */
00147   blkCnt = numSamples % 0x4U;
00148 
00149 #else
00150 
00151   /* Run the below code for Cortex-M0 */
00152   blkCnt = numSamples;
00153 
00154 #endif /* #if defined (ARM_MATH_DSP) */
00155 
00156   while (blkCnt > 0U)
00157   {
00158     /* C[0] = sqrt(A[0] * A[0] + A[1] * A[1]) */
00159     real = *pSrc++;
00160     imag = *pSrc++;
00161     acc0 = (q31_t) (((q63_t) real * real) >> 33);
00162     acc1 = (q31_t) (((q63_t) imag * imag) >> 33);
00163     /* store the result in 2.30 format in the destination buffer. */
00164     arm_sqrt_q31(acc0 + acc1, pDst++);
00165 
00166     /* Decrement the loop counter */
00167     blkCnt--;
00168   }
00169 }
00170 
00171 /**
00172  * @} end of cmplx_mag group
00173  */
00174