Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_var_q15.c Source File

arm_var_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_var_q15.c
00004  * Description:  Variance of an array of Q15 type
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 groupStats
00033  */
00034 
00035 /**
00036  * @addtogroup variance
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Variance of the elements of a Q15 vector.
00042  * @param[in]       *pSrc points to the input vector
00043  * @param[in]       blockSize length of the input vector
00044  * @param[out]      *pResult variance value returned here
00045  * @return none.
00046  * @details
00047  * <b>Scaling and Overflow Behavior:</b>
00048  *
00049  * \par
00050  * The function is implemented using a 64-bit internal accumulator.
00051  * The input is represented in 1.15 format.
00052  * Intermediate multiplication yields a 2.30 format, and this
00053  * result is added without saturation to a 64-bit accumulator in 34.30 format.
00054  * With 33 guard bits in the accumulator, there is no risk of overflow, and the
00055  * full precision of the intermediate multiplication is preserved.
00056  * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
00057  * 15 bits, and then saturated to yield a result in 1.15 format.
00058  */
00059 
00060 void arm_var_q15(
00061   q15_t * pSrc,
00062   uint32_t blockSize,
00063   q15_t * pResult)
00064 {
00065   q31_t sum = 0;                                 /* Accumulator */
00066   q31_t meanOfSquares, squareOfMean;             /* square of mean and mean of square */
00067   uint32_t blkCnt;                               /* loop counter */
00068   q63_t sumOfSquares = 0;                        /* Accumulator */
00069 #if defined (ARM_MATH_DSP)
00070   q31_t in;                                      /* input value */
00071   q15_t in1;                                     /* input value */
00072 #else
00073   q15_t in;                                      /* input value */
00074 #endif
00075 
00076   if (blockSize == 1U)
00077   {
00078     *pResult = 0;
00079     return;
00080   }
00081 
00082 #if defined (ARM_MATH_DSP)
00083   /* Run the below code for Cortex-M4 and Cortex-M3 */
00084 
00085   /*loop Unrolling */
00086   blkCnt = blockSize >> 2U;
00087 
00088   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00089    ** a second loop below computes the remaining 1 to 3 samples. */
00090   while (blkCnt > 0U)
00091   {
00092     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
00093     /* Compute Sum of squares of the input samples
00094      * and then store the result in a temporary variable, sum. */
00095     in = *__SIMD32(pSrc)++;
00096     sum += ((in << 16U) >> 16U);
00097     sum +=  (in >> 16U);
00098     sumOfSquares = __SMLALD(in, in, sumOfSquares);
00099     in = *__SIMD32(pSrc)++;
00100     sum += ((in << 16U) >> 16U);
00101     sum +=  (in >> 16U);
00102     sumOfSquares = __SMLALD(in, in, sumOfSquares);
00103 
00104     /* Decrement the loop counter */
00105     blkCnt--;
00106   }
00107 
00108   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00109    ** No loop unrolling is used. */
00110   blkCnt = blockSize % 0x4U;
00111 
00112   while (blkCnt > 0U)
00113   {
00114     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00115     /* Compute Sum of squares of the input samples
00116      * and then store the result in a temporary variable, sum. */
00117     in1 = *pSrc++;
00118     sumOfSquares = __SMLALD(in1, in1, sumOfSquares);
00119     sum += in1;
00120 
00121     /* Decrement the loop counter */
00122     blkCnt--;
00123   }
00124 
00125   /* Compute Mean of squares of the input samples
00126    * and then store the result in a temporary variable, meanOfSquares. */
00127   meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1U));
00128 
00129   /* Compute square of mean */
00130   squareOfMean = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
00131 
00132   /* mean of the squares minus the square of the mean. */
00133   *pResult = (meanOfSquares - squareOfMean) >> 15U;
00134 
00135 #else
00136   /* Run the below code for Cortex-M0 */
00137 
00138   /* Loop over blockSize number of values */
00139   blkCnt = blockSize;
00140 
00141   while (blkCnt > 0U)
00142   {
00143     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00144     /* Compute Sum of squares of the input samples
00145      * and then store the result in a temporary variable, sumOfSquares. */
00146     in = *pSrc++;
00147     sumOfSquares += (in * in);
00148 
00149     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
00150     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
00151     sum += in;
00152 
00153     /* Decrement the loop counter */
00154     blkCnt--;
00155   }
00156 
00157   /* Compute Mean of squares of the input samples
00158    * and then store the result in a temporary variable, meanOfSquares. */
00159   meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1U));
00160 
00161   /* Compute square of mean */
00162   squareOfMean = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1U)));
00163 
00164   /* mean of the squares minus the square of the mean. */
00165   *pResult = (meanOfSquares - squareOfMean) >> 15;
00166 
00167 #endif /* #if defined (ARM_MATH_DSP) */
00168 }
00169 
00170 /**
00171  * @} end of variance group
00172  */
00173