1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_rms_q15.c
4 * Description: Root Mean Square of the elements of a Q15 vector
5 *
6 * $Date: 23 April 2021
7 * $Revision: V1.9.0
8 *
9 * Target Processor: Cortex-M and Cortex-A cores
10 * -------------------------------------------------------------------- */
11 /*
12 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28
29 #include "dsp/statistics_functions.h"
30
31 /**
32 @ingroup groupStats
33 */
34
35 /**
36 @addtogroup RMS
37 @{
38 */
39
40 /**
41 @brief Root Mean Square of the elements of a Q15 vector.
42 @param[in] pSrc points to the input vector
43 @param[in] blockSize number of samples in input vector
44 @param[out] pResult root mean square value returned here
45 @return none
46
47 @par Scaling and Overflow Behavior
48 The function is implemented using a 64-bit internal accumulator.
49 The input is represented in 1.15 format.
50 Intermediate multiplication yields a 2.30 format, and this
51 result is added without saturation to a 64-bit accumulator in 34.30 format.
52 With 33 guard bits in the accumulator, there is no risk of overflow, and the
53 full precision of the intermediate multiplication is preserved.
54 Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
55 15 bits, and then saturated to yield a result in 1.15 format.
56 */
57 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_rms_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)58 void arm_rms_q15(
59 const q15_t * pSrc,
60 uint32_t blockSize,
61 q15_t * pResult)
62 {
63 q63_t pow = 0.0f;
64 q15_t normalizedPower;
65
66 arm_power_q15(pSrc, blockSize, &pow);
67
68 normalizedPower=__SSAT((pow / (q63_t) blockSize) >> 15,16);
69 arm_sqrt_q15(normalizedPower, pResult);
70 }
71 #else
arm_rms_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)72 void arm_rms_q15(
73 const q15_t * pSrc,
74 uint32_t blockSize,
75 q15_t * pResult)
76 {
77 uint32_t blkCnt; /* Loop counter */
78 q63_t sum = 0; /* Temporary result storage */
79 q15_t in; /* Temporary variable to store input value */
80
81 #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
82 q31_t in32; /* Temporary variable to store input value */
83 #endif
84
85 #if defined (ARM_MATH_LOOPUNROLL)
86
87 /* Loop unrolling: Compute 4 outputs at a time */
88 blkCnt = blockSize >> 2U;
89
90 while (blkCnt > 0U)
91 {
92 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
93
94 /* Compute sum of squares and store result in a temporary variable. */
95 #if defined (ARM_MATH_DSP)
96 in32 = read_q15x2_ia ((q15_t **) &pSrc);
97 sum = __SMLALD(in32, in32, sum);
98
99 in32 = read_q15x2_ia ((q15_t **) &pSrc);
100 sum = __SMLALD(in32, in32, sum);
101 #else
102 in = *pSrc++;
103 sum += ((q31_t) in * in);
104
105 in = *pSrc++;
106 sum += ((q31_t) in * in);
107
108 in = *pSrc++;
109 sum += ((q31_t) in * in);
110
111 in = *pSrc++;
112 sum += ((q31_t) in * in);
113 #endif /* #if defined (ARM_MATH_DSP) */
114
115 /* Decrement loop counter */
116 blkCnt--;
117 }
118
119 /* Loop unrolling: Compute remaining outputs */
120 blkCnt = blockSize % 0x4U;
121
122 #else
123
124 /* Initialize blkCnt with number of samples */
125 blkCnt = blockSize;
126
127 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
128
129 while (blkCnt > 0U)
130 {
131 /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
132
133 in = *pSrc++;
134 /* Compute sum of squares and store result in a temporary variable. */
135 sum += ((q31_t) in * in);
136
137 /* Decrement loop counter */
138 blkCnt--;
139 }
140
141 /* Truncating and saturating the accumulator to 1.15 format */
142 /* Store result in destination */
143 arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
144 }
145 #endif /* defined(ARM_MATH_MVEI) */
146
147 /**
148 @} end of RMS group
149 */
150