00001 /* ---------------------------------------------------------------------- 00002 * Copyright (C) 2011 ARM Limited. All rights reserved. 00003 * 00004 * $Date: 15. December 2011 00005 * $Revision: V2.0.0 00006 * 00007 * Project: Cortex-R DSP Library 00008 * Title: arm_rms_q31.c 00009 * 00010 * Description: Root Mean Square of the elements of a Q31 vector. 00011 * 00012 * Target Processor: Cortex-R4/R5 00013 * 00014 * Version 1.0.0 2011/03/08 00015 * Alpha release. 00016 * 00017 * Version 1.0.1 2011/09/30 00018 * Beta release. 00019 * 00020 * Version 2.0.0 2011/12/15 00021 * Final release. 00022 * 00023 * ---------------------------------------------------------------------------- */ 00024 #include "arm_math.h" 00025 00056 void arm_rms_q31( 00057 q31_t * pSrc, 00058 uint32_t blockSize, 00059 q31_t * pResult) 00060 { 00061 q31_t *pIn1 = pSrc; /* SrcA pointer */ 00062 q63_t sum = 0; /* accumulator */ 00063 q31_t in; /* Temporary variable to store the input */ 00064 uint32_t blkCnt; /* loop counter */ 00065 q31_t in1, in2, in3, in4; /* Temporary input variables */ 00066 00067 /*loop Unrolling */ 00068 blkCnt = blockSize >> 3u; 00069 00070 /* First part of the processing with loop unrolling. Compute 8 outputs at a time. 00071 ** a second loop below computes the remaining 1 to 7 samples. */ 00072 while(blkCnt > 0u) 00073 { 00074 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */ 00075 /* Compute sum of the squares and then store the result in a temporary variable, sum */ 00076 /* read two samples from source buffer */ 00077 in1 = pIn1[0]; 00078 in2 = pIn1[1]; 00079 00080 /* calculate power and accumulate to accumulator */ 00081 sum += (q63_t)in1 * in1; 00082 sum += (q63_t)in2 * in2; 00083 00084 /* read two samples from source buffer */ 00085 in3 = pIn1[2]; 00086 in4 = pIn1[3]; 00087 00088 /* calculate power and accumulate to accumulator */ 00089 sum += (q63_t)in3 * in3; 00090 sum += (q63_t)in4 * in4; 00091 00092 /* read two samples from source buffer */ 00093 in1 = pIn1[4]; 00094 in2 = pIn1[5]; 00095 00096 /* calculate power and accumulate to accumulator */ 00097 sum += (q63_t)in1 * in1; 00098 sum += (q63_t)in2 * in2; 00099 00100 /* read two samples from source buffer */ 00101 in3 = pIn1[6]; 00102 in4 = pIn1[7]; 00103 00104 /* calculate power and accumulate to accumulator */ 00105 sum += (q63_t)in3 * in3; 00106 sum += (q63_t)in4 * in4; 00107 00108 /* update source buffer to process next samples */ 00109 pIn1 += 8u; 00110 00111 /* Decrement the loop counter */ 00112 blkCnt--; 00113 } 00114 00115 /* If the blockSize is not a multiple of 8, compute any remaining output samples here. 00116 ** No loop unrolling is used. */ 00117 blkCnt = blockSize % 0x8u; 00118 00119 while(blkCnt > 0u) 00120 { 00121 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */ 00122 /* Compute sum of the squares and then store the results in a temporary variable, sum */ 00123 in = *pIn1++; 00124 sum += (q63_t) in *in; 00125 00126 /* Decrement the loop counter */ 00127 blkCnt--; 00128 } 00129 00130 /* Convert data in 2.62 to 1.31 by 31 right shifts and saturate */ 00131 #ifdef CCS 00132 00133 sum = __SSATA(sum, 31, 31); 00134 00135 #else 00136 00137 sum = __SSAT(sum >> 31, 31); 00138 00139 #endif 00140 00141 /* Compute Rms and store the result in the destination vector */ 00142 arm_sqrt_q31((q31_t) ((q31_t)sum / (int32_t) blockSize), pResult); 00143 } 00144