CMSIS DSP library

Dependents:   performance_timer Surfboard_ gps2rtty Capstone ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_std_q31.c Source File

arm_std_q31.c

00001 /* ----------------------------------------------------------------------    
00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.    
00003 *    
00004 * $Date:        19. March 2015
00005 * $Revision:    V.1.4.5  
00006 *    
00007 * Project:      CMSIS DSP Library    
00008 * Title:        arm_std_q31.c    
00009 *    
00010 * Description:  Standard deviation of an array of Q31 type.    
00011 *    
00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
00013 *  
00014 * Redistribution and use in source and binary forms, with or without 
00015 * modification, are permitted provided that the following conditions
00016 * are met:
00017 *   - Redistributions of source code must retain the above copyright
00018 *     notice, this list of conditions and the following disclaimer.
00019 *   - Redistributions in binary form must reproduce the above copyright
00020 *     notice, this list of conditions and the following disclaimer in
00021 *     the documentation and/or other materials provided with the 
00022 *     distribution.
00023 *   - Neither the name of ARM LIMITED nor the names of its contributors
00024 *     may be used to endorse or promote products derived from this
00025 *     software without specific prior written permission.
00026 *
00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038 * POSSIBILITY OF SUCH DAMAGE.    
00039 * -------------------------------------------------------------------- */
00040 
00041 #include "arm_math.h"
00042 
00043 /**    
00044  * @ingroup groupStats    
00045  */
00046 
00047 /**    
00048  * @addtogroup STD    
00049  * @{    
00050  */
00051 
00052 
00053 /**    
00054  * @brief Standard deviation of the elements of a Q31 vector.    
00055  * @param[in]       *pSrc points to the input vector    
00056  * @param[in]       blockSize length of the input vector    
00057  * @param[out]      *pResult standard deviation value returned here    
00058  * @return none.    
00059  * @details    
00060  * <b>Scaling and Overflow Behavior:</b>    
00061  *    
00062  *\par    
00063  * The function is implemented using an internal 64-bit accumulator.        
00064  * The input is represented in 1.31 format, which is then downshifted by 8 bits
00065  * which yields 1.23, and intermediate multiplication yields a 2.46 format.        
00066  * The accumulator maintains full precision of the intermediate multiplication results,         
00067  * but provides only a 16 guard bits.        
00068  * There is no saturation on intermediate additions.        
00069  * If the accumulator overflows it wraps around and distorts the result.        
00070  * In order to avoid overflows completely the input signal must be scaled down by         
00071  * log2(blockSize)-8 bits, as a total of blockSize additions are performed internally.  
00072  * After division, internal variables should be Q18.46 
00073  * Finally, the 18.46 accumulator is right shifted by 15 bits to yield a 1.31 format value. 
00074  *    
00075  */
00076 
00077 
00078 void arm_std_q31(
00079   q31_t * pSrc,
00080   uint32_t blockSize,
00081   q31_t * pResult)
00082 {
00083   q63_t sum = 0;                                 /* Accumulator */
00084   q63_t meanOfSquares, squareOfMean;             /* square of mean and mean of square */
00085   q31_t in;                                      /* input value */
00086   uint32_t blkCnt;                               /* loop counter */
00087   q63_t sumOfSquares = 0;                        /* Accumulator */
00088 
00089     if(blockSize == 1)
00090     {
00091         *pResult = 0;
00092         return;
00093     }
00094    
00095 #ifndef ARM_MATH_CM0_FAMILY
00096 
00097   /* Run the below code for Cortex-M4 and Cortex-M3 */
00098 
00099   /*loop Unrolling */
00100   blkCnt = blockSize >> 2u;
00101 
00102   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.    
00103    ** a second loop below computes the remaining 1 to 3 samples. */
00104   while(blkCnt > 0u)
00105   {
00106     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1])  */
00107     /* Compute Sum of squares of the input samples    
00108      * and then store the result in a temporary variable, sum. */
00109     in = *pSrc++ >> 8;
00110     sum += in;
00111     sumOfSquares += ((q63_t) (in) * (in));
00112     in = *pSrc++ >> 8;
00113     sum += in;
00114     sumOfSquares += ((q63_t) (in) * (in));
00115     in = *pSrc++ >> 8;
00116     sum += in;
00117     sumOfSquares += ((q63_t) (in) * (in));
00118     in = *pSrc++ >> 8;
00119     sum += in;
00120     sumOfSquares += ((q63_t) (in) * (in));
00121 
00122     /* Decrement the loop counter */
00123     blkCnt--;
00124   }
00125 
00126   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.    
00127    ** No loop unrolling is used. */
00128   blkCnt = blockSize % 0x4u;
00129 
00130   while(blkCnt > 0u)
00131   {
00132     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00133     /* Compute Sum of squares of the input samples    
00134      * and then store the result in a temporary variable, sum. */
00135     in = *pSrc++ >> 8;
00136     sum += in;
00137     sumOfSquares += ((q63_t) (in) * (in));
00138 
00139     /* Decrement the loop counter */
00140     blkCnt--;
00141   }
00142 
00143   /* Compute Mean of squares of the input samples    
00144    * and then store the result in a temporary variable, meanOfSquares. */
00145   meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1);
00146 
00147 #else
00148 
00149   /* Run the below code for Cortex-M0 */
00150 
00151   /* Loop over blockSize number of values */
00152   blkCnt = blockSize;
00153 
00154   while(blkCnt > 0u)
00155   {
00156     /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
00157     /* Compute Sum of squares of the input samples     
00158      * and then store the result in a temporary variable, sumOfSquares. */
00159     in = *pSrc++ >> 8;
00160     sumOfSquares += ((q63_t) (in) * (in));
00161 
00162     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
00163     /* Compute sum of all input values and then store the result in a temporary variable, sum. */
00164     sum += in;
00165 
00166     /* Decrement the loop counter */
00167     blkCnt--;
00168   }
00169 
00170   /* Compute Mean of squares of the input samples     
00171    * and then store the result in a temporary variable, meanOfSquares. */
00172   meanOfSquares = sumOfSquares / (q63_t)(blockSize - 1);
00173 
00174 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
00175 
00176   /* Compute square of mean */
00177   squareOfMean = sum * sum / (q63_t)(blockSize * (blockSize - 1u));
00178 
00179   /* Compute standard deviation and then store the result to the destination */
00180   arm_sqrt_q31((meanOfSquares - squareOfMean) >> 15, pResult);
00181 
00182 }
00183 
00184 /**    
00185  * @} end of STD group    
00186  */