Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* ---------------------------------------------------------------------- |
2 | * Project: CMSIS DSP Library |
||
3 | * Title: arm_var_q15.c |
||
4 | * Description: Variance of an array of Q15 type |
||
5 | * |
||
6 | * $Date: 27. January 2017 |
||
7 | * $Revision: V.1.5.1 |
||
8 | * |
||
9 | * Target Processor: Cortex-M cores |
||
10 | * -------------------------------------------------------------------- */ |
||
11 | /* |
||
12 | * Copyright (C) 2010-2017 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 "arm_math.h" |
||
30 | |||
31 | /** |
||
32 | * @ingroup groupStats |
||
33 | */ |
||
34 | |||
35 | /** |
||
36 | * @addtogroup variance |
||
37 | * @{ |
||
38 | */ |
||
39 | |||
40 | /** |
||
41 | * @brief Variance of the elements of a Q15 vector. |
||
42 | * @param[in] *pSrc points to the input vector |
||
43 | * @param[in] blockSize length of the input vector |
||
44 | * @param[out] *pResult variance value returned here |
||
45 | * @return none. |
||
46 | * @details |
||
47 | * <b>Scaling and Overflow Behavior:</b> |
||
48 | * |
||
49 | * \par |
||
50 | * The function is implemented using a 64-bit internal accumulator. |
||
51 | * The input is represented in 1.15 format. |
||
52 | * Intermediate multiplication yields a 2.30 format, and this |
||
53 | * result is added without saturation to a 64-bit accumulator in 34.30 format. |
||
54 | * With 33 guard bits in the accumulator, there is no risk of overflow, and the |
||
55 | * full precision of the intermediate multiplication is preserved. |
||
56 | * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower |
||
57 | * 15 bits, and then saturated to yield a result in 1.15 format. |
||
58 | */ |
||
59 | |||
60 | void arm_var_q15( |
||
61 | q15_t * pSrc, |
||
62 | uint32_t blockSize, |
||
63 | q15_t * pResult) |
||
64 | { |
||
65 | q31_t sum = 0; /* Accumulator */ |
||
66 | q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */ |
||
67 | uint32_t blkCnt; /* loop counter */ |
||
68 | q63_t sumOfSquares = 0; /* Accumulator */ |
||
69 | #if defined (ARM_MATH_DSP) |
||
70 | q31_t in; /* input value */ |
||
71 | q15_t in1; /* input value */ |
||
72 | #else |
||
73 | q15_t in; /* input value */ |
||
74 | #endif |
||
75 | |||
76 | if (blockSize == 1U) |
||
77 | { |
||
78 | *pResult = 0; |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | #if defined (ARM_MATH_DSP) |
||
83 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
||
84 | |||
85 | /*loop Unrolling */ |
||
86 | blkCnt = blockSize >> 2U; |
||
87 | |||
88 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
||
89 | ** a second loop below computes the remaining 1 to 3 samples. */ |
||
90 | while (blkCnt > 0U) |
||
91 | { |
||
92 | /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ |
||
93 | /* Compute Sum of squares of the input samples |
||
94 | * and then store the result in a temporary variable, sum. */ |
||
95 | in = *__SIMD32(pSrc)++; |
||
96 | sum += ((in << 16U) >> 16U); |
||
97 | sum += (in >> 16U); |
||
98 | sumOfSquares = __SMLALD(in, in, sumOfSquares); |
||
99 | in = *__SIMD32(pSrc)++; |
||
100 | sum += ((in << 16U) >> 16U); |
||
101 | sum += (in >> 16U); |
||
102 | sumOfSquares = __SMLALD(in, in, sumOfSquares); |
||
103 | |||
104 | /* Decrement the loop counter */ |
||
105 | blkCnt--; |
||
106 | } |
||
107 | |||
108 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
||
109 | ** No loop unrolling is used. */ |
||
110 | blkCnt = blockSize % 0x4U; |
||
111 | |||
112 | while (blkCnt > 0U) |
||
113 | { |
||
114 | /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ |
||
115 | /* Compute Sum of squares of the input samples |
||
116 | * and then store the result in a temporary variable, sum. */ |
||
117 | in1 = *pSrc++; |
||
118 | sumOfSquares = __SMLALD(in1, in1, sumOfSquares); |
||
119 | sum += in1; |
||
120 | |||
121 | /* Decrement the loop counter */ |
||
122 | blkCnt--; |
||
123 | } |
||
124 | |||
125 | /* Compute Mean of squares of the input samples |
||
126 | * and then store the result in a temporary variable, meanOfSquares. */ |
||
127 | meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1U)); |
||
128 | |||
129 | /* Compute square of mean */ |
||
130 | squareOfMean = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1U))); |
||
131 | |||
132 | /* mean of the squares minus the square of the mean. */ |
||
133 | *pResult = (meanOfSquares - squareOfMean) >> 15U; |
||
134 | |||
135 | #else |
||
136 | /* Run the below code for Cortex-M0 */ |
||
137 | |||
138 | /* Loop over blockSize number of values */ |
||
139 | blkCnt = blockSize; |
||
140 | |||
141 | while (blkCnt > 0U) |
||
142 | { |
||
143 | /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */ |
||
144 | /* Compute Sum of squares of the input samples |
||
145 | * and then store the result in a temporary variable, sumOfSquares. */ |
||
146 | in = *pSrc++; |
||
147 | sumOfSquares += (in * in); |
||
148 | |||
149 | /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */ |
||
150 | /* Compute sum of all input values and then store the result in a temporary variable, sum. */ |
||
151 | sum += in; |
||
152 | |||
153 | /* Decrement the loop counter */ |
||
154 | blkCnt--; |
||
155 | } |
||
156 | |||
157 | /* Compute Mean of squares of the input samples |
||
158 | * and then store the result in a temporary variable, meanOfSquares. */ |
||
159 | meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1U)); |
||
160 | |||
161 | /* Compute square of mean */ |
||
162 | squareOfMean = (q31_t)((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1U))); |
||
163 | |||
164 | /* mean of the squares minus the square of the mean. */ |
||
165 | *pResult = (meanOfSquares - squareOfMean) >> 15; |
||
166 | |||
167 | #endif /* #if defined (ARM_MATH_DSP) */ |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @} end of variance group |
||
172 | */ |