Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_cmplx_mag_squared_f32.c Source File

arm_cmplx_mag_squared_f32.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_cmplx_mag_squared_f32.c
00004  * Description:  Floating-point complex magnitude squared
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  * @defgroup cmplx_mag_squared Complex Magnitude Squared
00037  *
00038  * Computes the magnitude squared of the elements of a complex data vector.
00039  *
00040  * The <code>pSrc</code> points to the source data and
00041  * <code>pDst</code> points to the where the result should be written.
00042  * <code>numSamples</code> specifies the number of complex samples
00043  * in the input array and the data is stored in an interleaved fashion
00044  * (real, imag, real, imag, ...).
00045  * The input array has a total of <code>2*numSamples</code> values;
00046  * the output array has a total of <code>numSamples</code> values.
00047  *
00048  * The underlying algorithm is used:
00049  *
00050  * <pre>
00051  * for(n=0; n<numSamples; n++) {
00052  *     pDst[n] = pSrc[(2*n)+0]^2 + pSrc[(2*n)+1]^2;
00053  * }
00054  * </pre>
00055  *
00056  * There are separate functions for floating-point, Q15, and Q31 data types.
00057  */
00058 
00059 /**
00060  * @addtogroup cmplx_mag_squared
00061  * @{
00062  */
00063 
00064 
00065 /**
00066  * @brief  Floating-point complex magnitude squared
00067  * @param[in]  *pSrc points to the complex input vector
00068  * @param[out]  *pDst points to the real output vector
00069  * @param[in]  numSamples number of complex samples in the input vector
00070  * @return none.
00071  */
00072 
00073 void arm_cmplx_mag_squared_f32(
00074   float32_t * pSrc,
00075   float32_t * pDst,
00076   uint32_t numSamples)
00077 {
00078   float32_t real, imag;                          /* Temporary variables to store real and imaginary values */
00079   uint32_t blkCnt;                               /* loop counter */
00080 
00081 #if defined (ARM_MATH_DSP)
00082   float32_t real1, real2, real3, real4;          /* Temporary variables to hold real values */
00083   float32_t imag1, imag2, imag3, imag4;          /* Temporary variables to hold imaginary values */
00084   float32_t mul1, mul2, mul3, mul4;              /* Temporary variables */
00085   float32_t mul5, mul6, mul7, mul8;              /* Temporary variables */
00086   float32_t out1, out2, out3, out4;              /* Temporary variables to hold output values */
00087 
00088   /*loop Unrolling */
00089   blkCnt = numSamples >> 2U;
00090 
00091   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00092    ** a second loop below computes the remaining 1 to 3 samples. */
00093   while (blkCnt > 0U)
00094   {
00095     /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
00096     /* read real input sample from source buffer */
00097     real1 = pSrc[0];
00098     /* read imaginary input sample from source buffer */
00099     imag1 = pSrc[1];
00100 
00101     /* calculate power of real value */
00102     mul1 = real1 * real1;
00103 
00104     /* read real input sample from source buffer */
00105     real2 = pSrc[2];
00106 
00107     /* calculate power of imaginary value */
00108     mul2 = imag1 * imag1;
00109 
00110     /* read imaginary input sample from source buffer */
00111     imag2 = pSrc[3];
00112 
00113     /* calculate power of real value */
00114     mul3 = real2 * real2;
00115 
00116     /* read real input sample from source buffer */
00117     real3 = pSrc[4];
00118 
00119     /* calculate power of imaginary value */
00120     mul4 = imag2 * imag2;
00121 
00122     /* read imaginary input sample from source buffer */
00123     imag3 = pSrc[5];
00124 
00125     /* calculate power of real value */
00126     mul5 = real3 * real3;
00127     /* calculate power of imaginary value */
00128     mul6 = imag3 * imag3;
00129 
00130     /* read real input sample from source buffer */
00131     real4 = pSrc[6];
00132 
00133     /* accumulate real and imaginary powers */
00134     out1 = mul1 + mul2;
00135 
00136     /* read imaginary input sample from source buffer */
00137     imag4 = pSrc[7];
00138 
00139     /* accumulate real and imaginary powers */
00140     out2 = mul3 + mul4;
00141 
00142     /* calculate power of real value */
00143     mul7 = real4 * real4;
00144     /* calculate power of imaginary value */
00145     mul8 = imag4 * imag4;
00146 
00147     /* store output to destination */
00148     pDst[0] = out1;
00149 
00150     /* accumulate real and imaginary powers */
00151     out3 = mul5 + mul6;
00152 
00153     /* store output to destination */
00154     pDst[1] = out2;
00155 
00156     /* accumulate real and imaginary powers */
00157     out4 = mul7 + mul8;
00158 
00159     /* store output to destination */
00160     pDst[2] = out3;
00161 
00162     /* increment destination pointer by 8 to process next samples */
00163     pSrc += 8U;
00164 
00165     /* store output to destination */
00166     pDst[3] = out4;
00167 
00168     /* increment destination pointer by 4 to process next samples */
00169     pDst += 4U;
00170 
00171     /* Decrement the loop counter */
00172     blkCnt--;
00173   }
00174 
00175   /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
00176    ** No loop unrolling is used. */
00177   blkCnt = numSamples % 0x4U;
00178 
00179 #else
00180 
00181   /* Run the below code for Cortex-M0 */
00182 
00183   blkCnt = numSamples;
00184 
00185 #endif /* #if defined (ARM_MATH_DSP) */
00186 
00187   while (blkCnt > 0U)
00188   {
00189     /* C[0] = (A[0] * A[0] + A[1] * A[1]) */
00190     real = *pSrc++;
00191     imag = *pSrc++;
00192 
00193     /* out = (real * real) + (imag * imag) */
00194     /* store the result in the destination buffer. */
00195     *pDst++ = (real * real) + (imag * imag);
00196 
00197     /* Decrement the loop counter */
00198     blkCnt--;
00199   }
00200 }
00201 
00202 /**
00203  * @} end of cmplx_mag_squared group
00204  */
00205