• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_mean_q31.c
4  * Description:  Mean value of a Q31 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 Q31 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 64-bit internal accumulator.
49                    The input is represented in 1.31 format and is accumulated in a 64-bit
50                    accumulator in 33.31 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.31 format.
54  */
55 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
arm_mean_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)56 void arm_mean_q31(
57   const q31_t * pSrc,
58         uint32_t blockSize,
59         q31_t * pResult)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q31x4_t vecSrc;
63     q63_t     sum = 0LL;
64 
65 
66     /* Compute 4 outputs at a time */
67     blkCnt = blockSize >> 2U;
68     while (blkCnt > 0U)
69     {
70 
71         vecSrc = vldrwq_s32(pSrc);
72         /*
73          * sum lanes
74          */
75         sum = vaddlvaq(sum, vecSrc);
76 
77         blkCnt --;
78         pSrc += 4;
79     }
80 
81     /* Tail */
82     blkCnt = blockSize & 0x3;
83 
84     while (blkCnt > 0U)
85     {
86       /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
87       sum += *pSrc++;
88       blkCnt --;
89     }
90 
91     *pResult = arm_div_q63_to_q31(sum, blockSize);
92 }
93 #else
arm_mean_q31(const q31_t * pSrc,uint32_t blockSize,q31_t * pResult)94 void arm_mean_q31(
95   const q31_t * pSrc,
96         uint32_t blockSize,
97         q31_t * pResult)
98 {
99         uint32_t blkCnt;                               /* Loop counter */
100         q63_t sum = 0;                                 /* Temporary result storage */
101 
102 #if defined (ARM_MATH_LOOPUNROLL)
103 
104   /* Loop unrolling: Compute 4 outputs at a time */
105   blkCnt = blockSize >> 2U;
106 
107   while (blkCnt > 0U)
108   {
109     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
110     sum += *pSrc++;
111 
112     sum += *pSrc++;
113 
114     sum += *pSrc++;
115 
116     sum += *pSrc++;
117 
118     /* Decrement the loop counter */
119     blkCnt--;
120   }
121 
122   /* Loop unrolling: Compute remaining outputs */
123   blkCnt = blockSize % 0x4U;
124 
125 #else
126 
127   /* Initialize blkCnt with number of samples */
128   blkCnt = blockSize;
129 
130 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
131 
132   while (blkCnt > 0U)
133   {
134     /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
135     sum += *pSrc++;
136 
137     /* Decrement loop counter */
138     blkCnt--;
139   }
140 
141   /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) / blockSize  */
142   /* Store result to destination */
143   *pResult = (q31_t) (sum / blockSize);
144 }
145 #endif /* defined(ARM_MATH_MVEI) */
146 
147 /**
148   @} end of mean group
149  */
150