1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mean_q15.c
4 * Description: Mean value 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 mean
37 @{
38 */
39
40 /**
41 @brief Mean value 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 mean value returned here
45
46 @par Scaling and Overflow Behavior
47 The function is implemented using a 32-bit internal accumulator.
48 The input is represented in 1.15 format and is accumulated in a 32-bit
49 accumulator in 17.15 format.
50 There is no risk of internal overflow with this approach, and the
51 full precision of intermediate result is preserved.
52 Finally, the accumulator is truncated to yield a result of 1.15 format.
53 */
54
55 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mean_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)56 ARM_DSP_ATTRIBUTE void arm_mean_q15(
57 const q15_t * pSrc,
58 uint32_t blockSize,
59 q15_t * pResult)
60 {
61 uint32_t blkCnt; /* loop counters */
62 q15x8_t vecSrc;
63 q31_t sum = 0L;
64
65 /* Compute 8 outputs at a time */
66 blkCnt = blockSize >> 3U;
67 while (blkCnt > 0U)
68 {
69 vecSrc = vldrhq_s16(pSrc);
70 /*
71 * sum lanes
72 */
73 sum = vaddvaq(sum, vecSrc);
74
75 blkCnt--;
76 pSrc += 8;
77 }
78
79 /* Tail */
80 blkCnt = blockSize & 0x7;
81
82 while (blkCnt > 0U)
83 {
84 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
85 sum += *pSrc++;
86
87 /* Decrement loop counter */
88 blkCnt--;
89 }
90
91 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
92 /* Store the result to the destination */
93 *pResult = (q15_t) (sum / (int32_t) blockSize);
94 }
95 #else
arm_mean_q15(const q15_t * pSrc,uint32_t blockSize,q15_t * pResult)96 ARM_DSP_ATTRIBUTE void arm_mean_q15(
97 const q15_t * pSrc,
98 uint32_t blockSize,
99 q15_t * pResult)
100 {
101 uint32_t blkCnt; /* Loop counter */
102 q31_t sum = 0; /* Temporary result storage */
103
104 #if defined (ARM_MATH_LOOPUNROLL)
105 q31_t in;
106 #endif
107
108 #if defined (ARM_MATH_LOOPUNROLL)
109
110 /* Loop unrolling: Compute 4 outputs at a time */
111 blkCnt = blockSize >> 2U;
112
113 while (blkCnt > 0U)
114 {
115 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
116 in = read_q15x2_ia (&pSrc);
117 sum += ((in << 16U) >> 16U);
118 sum += (in >> 16U);
119
120 in = read_q15x2_ia (&pSrc);
121 sum += ((in << 16U) >> 16U);
122 sum += (in >> 16U);
123
124 /* Decrement the loop counter */
125 blkCnt--;
126 }
127
128 /* Loop unrolling: Compute remaining outputs */
129 blkCnt = blockSize % 0x4U;
130
131 #else
132
133 /* Initialize blkCnt with number of samples */
134 blkCnt = blockSize;
135
136 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
137
138 while (blkCnt > 0U)
139 {
140 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
141 sum += *pSrc++;
142
143 /* Decrement loop counter */
144 blkCnt--;
145 }
146
147 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize */
148 /* Store result to destination */
149 *pResult = (q15_t) (sum / (int32_t) blockSize);
150 }
151 #endif /* defined(ARM_MATH_MVEI) */
152
153 /**
154 @} end of mean group
155 */
156