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