Aded CMSIS5 DSP and NN folder. Needs some work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_std_q31.c Source File

arm_std_q31.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_std_q31.c
00004  * Description:  Standard deviation of an array of Q31 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 STD
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Standard deviation of the elements of a Q31 vector.
00042  * @param[in]       *pSrc points to the input vector
00043  * @param[in]       blockSize length of the input vector
00044  * @param[out]      *pResult standard deviation value returned here
00045  * @return none.
00046  * @details
00047  * <b>Scaling and Overflow Behavior:</b>
00048  *
00049  *\par
00050  * The function is implemented using an internal 64-bit accumulator.
00051  * The input is represented in 1.31 format, which is then downshifted by 8 bits
00052  * which yields 1.23, and intermediate multiplication yields a 2.46 format.
00053  * The accumulator maintains full precision of the intermediate multiplication results,
00054  * but provides only a 16 guard bits.
00055  * There is no saturation on intermediate additions.
00056  * If the accumulator overflows it wraps around and distorts the result.
00057  * In order to avoid overflows completely the input signal must be scaled down by
00058  * log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.
00059  * After division, internal variables should be Q18.46
00060  * Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value.
00061  *
00062  */
00063 
00064 void arm_std_q31(
00065   q31_t * pSrc,
00066   uint32_t blockSize,
00067   q31_t * pResult)
00068 {
00069   q63_t sum = 0;                                 /* Accumulator */
00070   q63_t meanOfSquares, squareOfMean;             /* square of mean and mean of square */
00071   q31_t in;                                      /* input value */
00072   uint32_t blkCnt;                               /* loop counter */
00073   q63_t sumOfSquares = 0;                        /* Accumulator */
00074 
00075   if (blockSize == 1U)
00076   {
00077     *pResult = 0;
00078     return;
00079   }
00080 
00081 #if defined (ARM_MATH_DSP)
00082   /* Run the below code for Cortex-M4 and Cortex-M3 */
00083 
00084   /*loop Unrolling */
00085   blkCnt = blockSize >> 2U;
00086 
00087   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00088    ** a second loop below computes the remaining 1 to 3 samples. */
00089   while (blkCnt > 0U)
00090   {
00091     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
00092     /* Compute Sum of squares of the input samples
00093      * and then store the result in a temporary variable, sum. */
00094     in = *pSrc++ >> 8U;
00095     sum += in;
00096     sumOfSquares += ((q63_t) (in) * (in));
00097     in = *pSrc++ >> 8U;
00098     sum += in;
00099     sumOfSquares += ((q63_t) (in) * (in));
00100     in = *pSrc++ >> 8U;
00101     sum += in;
00102     sumOfSquares += ((q63_t) (in) * (in));
00103     in = *pSrc++ >> 8U;
00104     sum += in;
00105     sumOfSquares += ((q63_t) (in) * (in));
00106 
00107     /* Decrement the loop counter */
00108     blkCnt--;
00109   }
00110 
00111   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00112    ** No loop unrolling is used. */
00113   blkCnt = blockSize % 0x4U;
00114 
00115   while (blkCnt > 0U)
00116   {
00117     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00118     /* Compute Sum of squares of the input samples
00119      * and then store the result in a temporary variable, sum. */
00120     in = *pSrc++ >> 8U;
00121     sum += in;
00122     sumOfSquares += ((q63_t) (in) * (in));
00123 
00124     /* Decrement the loop counter */
00125     blkCnt--;
00126   }
00127 
00128   /* Compute Mean of squares of the input samples
00129    * and then store the result in a temporary variable, meanOfSquares. */
00130   meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1U);
00131 
00132 #else
00133   /* Run the below code for Cortex-M0 */
00134 
00135   /* Loop over blockSize number of values */
00136   blkCnt = blockSize;
00137 
00138   while (blkCnt > 0U)
00139   {
00140     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00141     /* Compute Sum of squares of the input samples
00142      * and then store the result in a temporary variable, sumOfSquares. */
00143     in = *pSrc++ >> 8U;
00144     sumOfSquares += ((q63_t) (in) * (in));
00145 
00146     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
00147     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
00148     sum += in;
00149 
00150     /* Decrement the loop counter */
00151     blkCnt--;
00152   }
00153 
00154   /* Compute Mean of squares of the input samples
00155    * and then store the result in a temporary variable, meanOfSquares. */
00156   meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1U);
00157 
00158 #endif /* #if defined (ARM_MATH_DSP) */
00159 
00160   /* Compute square of mean */
00161   squareOfMean = sum * sum / (q63_t)(blockSize * (blockSize - 1U));
00162 
00163   /* Compute standard deviation and then store the result to the destination */
00164   arm_sqrt_q31((meanOfSquares - squareOfMean) >> 15U, pResult);
00165 }
00166 
00167 /**
00168  * @} end of STD group
00169  */
00170