CMSIS DSP library

Dependents:   KL25Z_FFT_Demo Hat_Board_v5_1 KL25Z_FFT_Demo_tony KL25Z_FFT_Demo_tony ... more

Fork of mbed-dsp by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_var_q15.c Source File

arm_var_q15.c

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