1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_rms_f32.c
4  * Description:  Root mean square value of the elements of a floating-point 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   @defgroup RMS Root mean square (RMS)
37 
38   Calculates the Root Mean Square of the elements in the input vector.
39   The underlying algorithm is used:
40 
41   <pre>
42       Result = sqrt(((pSrc[0] * pSrc[0] + pSrc[1] * pSrc[1] + ... + pSrc[blockSize-1] * pSrc[blockSize-1]) / blockSize));
43   </pre>
44 
45   There are separate functions for floating point, Q31, and Q15 data types.
46  */
47 
48 /**
49   @addtogroup RMS
50   @{
51  */
52 
53 /**
54   @brief         Root Mean Square of the elements of a floating-point vector.
55   @param[in]     pSrc       points to the input vector
56   @param[in]     blockSize  number of samples in input vector
57   @param[out]    pResult    root mean square value returned here
58  */
59 
60 #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_rms_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)61 ARM_DSP_ATTRIBUTE void arm_rms_f32(
62   const float32_t * pSrc,
63   uint32_t blockSize,
64   float32_t * pResult)
65 {
66     float32_t pow = 0.0f;
67 
68     arm_power_f32(pSrc, blockSize, &pow);
69 
70     /* Compute Rms and store the result in the destination */
71     arm_sqrt_f32(pow / (float32_t) blockSize, pResult);
72 }
73 #else
74 #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_rms_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)75 ARM_DSP_ATTRIBUTE void arm_rms_f32(
76   const float32_t * pSrc,
77   uint32_t blockSize,
78   float32_t * pResult)
79 {
80   float32_t sum = 0.0f;                          /* accumulator */
81   float32_t in;                                  /* Temporary variable to store input value */
82   uint32_t blkCnt;                               /* loop counter */
83 
84   float32x4_t sumV = vdupq_n_f32(0.0f);                          /* Temporary result storage */
85   float32x2_t sumV2;
86   float32x4_t inV;
87 
88   blkCnt = blockSize >> 2U;
89 
90   /* Compute 4 outputs at a time.
91    ** a second loop below computes the remaining 1 to 3 samples. */
92   while (blkCnt > 0U)
93   {
94     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
95     /* Compute Power and then store the result in a temporary variable, sum. */
96     inV = vld1q_f32(pSrc);
97     sumV = vmlaq_f32(sumV, inV, inV);
98     pSrc += 4;
99 
100     /* Decrement the loop counter */
101     blkCnt--;
102   }
103 
104   sumV2 = vpadd_f32(vget_low_f32(sumV),vget_high_f32(sumV));
105   sum = vget_lane_f32(sumV2, 0) + vget_lane_f32(sumV2, 1);
106 
107   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
108    ** No loop unrolling is used. */
109   blkCnt = blockSize % 0x4U;
110 
111   while (blkCnt > 0U)
112   {
113     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
114     /* compute power and then store the result in a temporary variable, sum. */
115     in = *pSrc++;
116     sum += in * in;
117 
118     /* Decrement the loop counter */
119     blkCnt--;
120   }
121 
122   /* Compute Rms and store the result in the destination */
123   arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
124 }
125 #else
arm_rms_f32(const float32_t * pSrc,uint32_t blockSize,float32_t * pResult)126 ARM_DSP_ATTRIBUTE void arm_rms_f32(
127   const float32_t * pSrc,
128         uint32_t blockSize,
129         float32_t * pResult)
130 {
131         uint32_t blkCnt;                               /* Loop counter */
132         float32_t sum = 0.0f;                          /* Temporary result storage */
133         float32_t in;                                  /* Temporary variable to store input value */
134 
135 #if defined (ARM_MATH_LOOPUNROLL) && !defined(ARM_MATH_AUTOVECTORIZE)
136 
137   /* Loop unrolling: Compute 4 outputs at a time */
138   blkCnt = blockSize >> 2U;
139 
140   while (blkCnt > 0U)
141   {
142     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
143 
144     in = *pSrc++;
145     /* Compute sum of squares and store result in a temporary variable, sum. */
146     sum += in * in;
147 
148     in = *pSrc++;
149     sum += in * in;
150 
151     in = *pSrc++;
152     sum += in * in;
153 
154     in = *pSrc++;
155     sum += in * in;
156 
157     /* Decrement loop counter */
158     blkCnt--;
159   }
160 
161   /* Loop unrolling: Compute remaining outputs */
162   blkCnt = blockSize % 0x4U;
163 
164 #else
165 
166   /* Initialize blkCnt with number of samples */
167   blkCnt = blockSize;
168 
169 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
170 
171   while (blkCnt > 0U)
172   {
173     /* C = A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1] */
174 
175     in = *pSrc++;
176     /* Compute sum of squares and store result in a temporary variable. */
177     sum += ( in * in);
178 
179     /* Decrement loop counter */
180     blkCnt--;
181   }
182 
183   /* Compute Rms and store result in destination */
184   arm_sqrt_f32(sum / (float32_t) blockSize, pResult);
185 }
186 #endif /* #if defined(ARM_MATH_NEON) */
187 #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
188 
189 /**
190   @} end of RMS group
191  */
192