Merge pull request #11270 from haslinghuis/rename_attr
[betaflight.git] / lib / main / CMSIS / DSP / Source / StatisticsFunctions / arm_rms_q31.c
blobcb3c58e984082b8ebfbdd194ac7b03f5e2480850
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_rms_q31.c
4 * Description: Root Mean Square of the elements of a Q31 vector
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
29 #include "arm_math.h"
31 /**
32 * @addtogroup RMS
33 * @{
37 /**
38 * @brief Root Mean Square of the elements of a Q31 vector.
39 * @param[in] *pSrc points to the input vector
40 * @param[in] blockSize length of the input vector
41 * @param[out] *pResult rms value returned here
42 * @return none.
44 * @details
45 * <b>Scaling and Overflow Behavior:</b>
47 *\par
48 * The function is implemented using an internal 64-bit accumulator.
49 * The input is represented in 1.31 format, and intermediate multiplication
50 * yields a 2.62 format.
51 * The accumulator maintains full precision of the intermediate multiplication results,
52 * but provides only a single guard bit.
53 * There is no saturation on intermediate additions.
54 * If the accumulator overflows, it wraps around and distorts the result.
55 * In order to avoid overflows completely, the input signal must be scaled down by
56 * log2(blockSize) bits, as a total of blockSize additions are performed internally.
57 * Finally, the 2.62 accumulator is right shifted by 31 bits to yield a 1.31 format value.
61 void arm_rms_q31(
62 q31_t * pSrc,
63 uint32_t blockSize,
64 q31_t * pResult)
66 q63_t sum = 0; /* accumulator */
67 q31_t in; /* Temporary variable to store the input */
68 uint32_t blkCnt; /* loop counter */
70 #if defined (ARM_MATH_DSP)
71 /* Run the below code for Cortex-M4 and Cortex-M3 */
73 q31_t in1, in2, in3, in4; /* Temporary input variables */
75 /*loop Unrolling */
76 blkCnt = blockSize >> 2U;
78 /* First part of the processing with loop unrolling. Compute 8 outputs at a time.
79 ** a second loop below computes the remaining 1 to 7 samples. */
80 while (blkCnt > 0U)
82 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
83 /* Compute sum of the squares and then store the result in a temporary variable, sum */
84 /* read two samples from source buffer */
85 in1 = pSrc[0];
86 in2 = pSrc[1];
88 /* calculate power and accumulate to accumulator */
89 sum += (q63_t) in1 *in1;
90 sum += (q63_t) in2 *in2;
92 /* read two samples from source buffer */
93 in3 = pSrc[2];
94 in4 = pSrc[3];
96 /* calculate power and accumulate to accumulator */
97 sum += (q63_t) in3 *in3;
98 sum += (q63_t) in4 *in4;
101 /* update source buffer to process next samples */
102 pSrc += 4U;
104 /* Decrement the loop counter */
105 blkCnt--;
108 /* If the blockSize is not a multiple of 8, compute any remaining output samples here.
109 ** No loop unrolling is used. */
110 blkCnt = blockSize % 0x4U;
112 #else
113 /* Run the below code for Cortex-M0 */
115 blkCnt = blockSize;
117 #endif /* #if defined (ARM_MATH_DSP) */
119 while (blkCnt > 0U)
121 /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
122 /* Compute sum of the squares and then store the results in a temporary variable, sum */
123 in = *pSrc++;
124 sum += (q63_t) in *in;
126 /* Decrement the loop counter */
127 blkCnt--;
130 /* Convert data in 2.62 to 1.31 by 31 right shifts and saturate */
131 /* Compute Rms and store the result in the destination vector */
132 arm_sqrt_q31(clip_q63_to_q31((sum / (q63_t) blockSize) >> 31), pResult);
136 * @} end of RMS group