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 
46   @par           Scaling and Overflow Behavior
47                    The function is implemented using a 64-bit internal accumulator.
48                    The input is represented in 1.15 format.
49                    Intermediate multiplication yields a 2.30 format, and this
50                    result is added without saturation to a 64-bit accumulator in 34.30 format.
51                    With 33 guard bits in the accumulator, there is no risk of overflow, and the
52                    full precision of the intermediate multiplication is preserved.
53                    Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
54                    15 bits, and then saturated to yield a result in 1.15 format.
55  */
56 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_rms_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)57 ARM_DSP_ATTRIBUTE void arm_rms_q15(
58   const q15_t * pSrc,
59         uint32_t blockSize,
60         q15_t * pResult)
61 {
62     q63_t pow = 0.0f;
63     q15_t normalizedPower;
64 
65     arm_power_q15(pSrc, blockSize, &pow);
66 
67     normalizedPower=__SSAT((pow / (q63_t) blockSize) >> 15,16);
68     arm_sqrt_q15(normalizedPower, pResult);
69 }
70 #else
arm_rms_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)71 ARM_DSP_ATTRIBUTE void arm_rms_q15(
72   const q15_t * pSrc,
73         uint32_t blockSize,
74         q15_t * pResult)
75 {
76         uint32_t blkCnt;                               /* Loop counter */
77         q63_t sum = 0;                                 /* Temporary result storage */
78         q15_t in;                                      /* Temporary variable to store input value */
79 
80 #if defined (ARM_MATH_LOOPUNROLL) && defined (ARM_MATH_DSP)
81         q31_t in32;                                    /* Temporary variable to store input value */
82 #endif
83 
84 #if defined (ARM_MATH_LOOPUNROLL)
85 
86   /* Loop unrolling: Compute 4 outputs at a time */
87   blkCnt = blockSize >> 2U;
88 
89   while (blkCnt > 0U)
90   {
91     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
92 
93     /* Compute sum of squares and store result in a temporary variable. */
94 #if defined (ARM_MATH_DSP)
95     in32 = read_q15x2_ia (&pSrc);
96     sum = __SMLALD(in32, in32, sum);
97 
98     in32 = read_q15x2_ia (&pSrc);
99     sum = __SMLALD(in32, in32, sum);
100 #else
101     in = *pSrc++;
102     sum += ((q31_t) in * in);
103 
104     in = *pSrc++;
105     sum += ((q31_t) in * in);
106 
107     in = *pSrc++;
108     sum += ((q31_t) in * in);
109 
110     in = *pSrc++;
111     sum += ((q31_t) in * in);
112 #endif /* #if defined (ARM_MATH_DSP) */
113 
114     /* Decrement loop counter */
115     blkCnt--;
116   }
117 
118   /* Loop unrolling: Compute remaining outputs */
119   blkCnt = blockSize % 0x4U;
120 
121 #else
122 
123   /* Initialize blkCnt with number of samples */
124   blkCnt = blockSize;
125 
126 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
127 
128   while (blkCnt > 0U)
129   {
130     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
131 
132     in = *pSrc++;
133     /* Compute sum of squares and store result in a temporary variable. */
134     sum += ((q31_t) in * in);
135 
136     /* Decrement loop counter */
137     blkCnt--;
138   }
139 
140   /* Truncating and saturating the accumulator to 1.15 format */
141   /* Store result in destination */
142   arm_sqrt_q15(__SSAT((sum / (q63_t)blockSize) >> 15, 16), pResult);
143 }
144 #endif /* defined(ARM_MATH_MVEI) */
145 
146 /**
147   @} end of RMS group
148  */
149