Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-dsp by
arm_std_q31.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_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, and intermediate multiplication 00065 * yields a 2.62 format. 00066 * The accumulator maintains full precision of the intermediate multiplication results, 00067 * but provides only a single guard bit. 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) bits, as a total of blockSize additions are performed internally. 00072 * Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value. 00073 * 00074 */ 00075 00076 00077 void arm_std_q31( 00078 q31_t * pSrc, 00079 uint32_t blockSize, 00080 q31_t * pResult) 00081 { 00082 q63_t sum = 0; /* Accumulator */ 00083 q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */ 00084 q31_t mean; /* mean */ 00085 q31_t in; /* input value */ 00086 q31_t t; /* Temporary variable */ 00087 uint32_t blkCnt; /* loop counter */ 00088 q63_t sumOfSquares = 0; /* Accumulator */ 00089 00090 #ifndef ARM_MATH_CM0_FAMILY 00091 00092 /* Run the below code for Cortex-M4 and Cortex-M3 */ 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 = *pSrc++; 00105 sum += in; 00106 sumOfSquares += ((q63_t) (in) * (in)); 00107 in = *pSrc++; 00108 sum += in; 00109 sumOfSquares += ((q63_t) (in) * (in)); 00110 in = *pSrc++; 00111 sum += in; 00112 sumOfSquares += ((q63_t) (in) * (in)); 00113 in = *pSrc++; 00114 sum += in; 00115 sumOfSquares += ((q63_t) (in) * (in)); 00116 00117 /* Decrement the loop counter */ 00118 blkCnt--; 00119 } 00120 00121 /* If the blockSize is not a multiple of 4, compute any remaining output samples here. 00122 ** No loop unrolling is used. */ 00123 blkCnt = blockSize % 0x4u; 00124 00125 while(blkCnt > 0u) 00126 { 00127 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00128 /* Compute Sum of squares of the input samples 00129 * and then store the result in a temporary variable, sum. */ 00130 in = *pSrc++; 00131 sum += in; 00132 sumOfSquares += ((q63_t) (in) * (in)); 00133 00134 /* Decrement the loop counter */ 00135 blkCnt--; 00136 } 00137 00138 t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f); 00139 00140 /* Compute Mean of squares of the input samples 00141 * and then store the result in a temporary variable, meanOfSquares. */ 00142 sumOfSquares = (sumOfSquares >> 31); 00143 meanOfSquares = (q31_t) ((sumOfSquares * t) >> 30); 00144 00145 #else 00146 00147 /* Run the below code for Cortex-M0 */ 00148 00149 /* Loop over blockSize number of values */ 00150 blkCnt = blockSize; 00151 00152 while(blkCnt > 0u) 00153 { 00154 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ 00155 /* Compute Sum of squares of the input samples 00156 * and then store the result in a temporary variable, sumOfSquares. */ 00157 in = *pSrc++; 00158 sumOfSquares += ((q63_t) (in) * (in)); 00159 00160 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ 00161 /* Compute sum of all input values and then store the result in a temporary variable, sum. */ 00162 sum += in; 00163 00164 /* Decrement the loop counter */ 00165 blkCnt--; 00166 } 00167 00168 /* Compute Mean of squares of the input samples 00169 * and then store the result in a temporary variable, meanOfSquares. */ 00170 t = (q31_t) ((1.0f / (float32_t) (blockSize - 1u)) * 1073741824.0f); 00171 sumOfSquares = (sumOfSquares >> 31); 00172 meanOfSquares = (q31_t) ((sumOfSquares * t) >> 30); 00173 00174 #endif /* #ifndef ARM_MATH_CM0_FAMILY */ 00175 00176 /* Compute mean of all input values */ 00177 t = (q31_t) ((1.0f / (blockSize * (blockSize - 1u))) * 2147483648.0f); 00178 mean = (q31_t) (sum); 00179 00180 /* Compute square of mean */ 00181 squareOfMean = (q31_t) (((q63_t) mean * mean) >> 31); 00182 squareOfMean = (q31_t) (((q63_t) squareOfMean * t) >> 31); 00183 00184 00185 /* Compute standard deviation and then store the result to the destination */ 00186 arm_sqrt_q31(meanOfSquares - squareOfMean, pResult); 00187 00188 } 00189 00190 /** 00191 * @} end of STD group 00192 */
Generated on Tue Jul 12 2022 18:44:10 by
1.7.2
