1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
10 * Description: Root Mean Square of the elements of a Q31 vector.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
28 * ---------------------------------------------------------------------------- */
39 * @brief Root Mean Square of the elements of a Q31 vector.
40 * @param[in] *pSrc points to the input vector
41 * @param[in] blockSize length of the input vector
42 * @param[out] *pResult rms value returned here
46 * <b>Scaling and Overflow Behavior:</b>
49 * The function is implemented using an internal 64-bit accumulator.
50 * The input is represented in 1.31 format, and intermediate multiplication
51 * yields a 2.62 format.
52 * The accumulator maintains full precision of the intermediate multiplication results,
53 * but provides only a single guard bit.
54 * There is no saturation on intermediate additions.
55 * If the accumulator overflows, it wraps around and distorts the result.
56 * In order to avoid overflows completely, the input signal must be scaled down by
57 * log2(blockSize) bits, as a total of blockSize additions are performed internally.
58 * Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.
67 q63_t sum = 0; /* accumulator */
68 q31_t in; /* Temporary variable to store the input */
69 uint32_t blkCnt; /* loop counter */
73 /* Run the below code for Cortex-M4 and Cortex-M3 */
75 q31_t *pIn1 = pSrc; /* SrcA pointer */
78 blkCnt = blockSize >> 2u;
80 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
81 ** a second loop below computes the remaining 1 to 3 samples. */
84 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
85 /* Compute sum of the squares and then store the result in a temporary variable, sum */
87 sum += (q63_t) in *in;
89 sum += (q63_t) in *in;
91 sum += (q63_t) in *in;
93 sum += (q63_t) in *in;
95 /* Decrement the loop counter */
99 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
100 ** No loop unrolling is used. */
101 blkCnt = blockSize % 0x4u;
105 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
106 /* Compute sum of the squares and then store the results in a temporary variable, sum */
108 sum += (q63_t) in *in;
110 /* Decrement the loop counter */
116 /* Run the below code for Cortex-M0 */
118 /* Loop over blockSize number of values */
123 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
124 /* Compute sum of the squares and then store the results in a temporary variable, sum */
126 sum += (q63_t) in *in;
128 /* Decrement the loop counter */
132 #endif /* #ifndef ARM_MATH_CM0 */
134 /* Convert data in 2.62 to 1.31 by 31 right shifts */
137 /* Compute Rms and store the result in the destination vector */
138 arm_sqrt_q31((q31_t) (sum / (int32_t) blockSize), pResult);
142 * @} end of RMS group