annotate f103c8/Drivers/CMSIS/DSP_Lib/Source/StatisticsFunctions/arm_std_q15.c @ 6:ca42336826bd default tip

working on clock sources
author cin
date Mon, 23 Jan 2017 02:40:17 +0300
parents 0c59e7a7782a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
1 /* ----------------------------------------------------------------------
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
3 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
4 * $Date: 19. March 2015
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
5 * $Revision: V.1.4.5
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
6 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
7 * Project: CMSIS DSP Library
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
8 * Title: arm_std_q15.c
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
9 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
10 * Description: Standard deviation of an array of Q15 type.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
11 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
13 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
14 * Redistribution and use in source and binary forms, with or without
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
15 * modification, are permitted provided that the following conditions
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
16 * are met:
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
17 * - Redistributions of source code must retain the above copyright
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
18 * notice, this list of conditions and the following disclaimer.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
19 * - Redistributions in binary form must reproduce the above copyright
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
20 * notice, this list of conditions and the following disclaimer in
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
21 * the documentation and/or other materials provided with the
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
22 * distribution.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
23 * - Neither the name of ARM LIMITED nor the names of its contributors
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
24 * may be used to endorse or promote products derived from this
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
25 * software without specific prior written permission.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
26 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
38 * POSSIBILITY OF SUCH DAMAGE.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
39 * -------------------------------------------------------------------- */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
40
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
41 #include "arm_math.h"
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
42
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
43 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
44 * @ingroup groupStats
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
45 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
46
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
47 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
48 * @addtogroup STD
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
49 * @{
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
50 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
51
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
52 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
53 * @brief Standard deviation of the elements of a Q15 vector.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
54 * @param[in] *pSrc points to the input vector
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
55 * @param[in] blockSize length of the input vector
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
56 * @param[out] *pResult standard deviation value returned here
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
57 * @return none.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
58 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
59 * @details
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
60 * <b>Scaling and Overflow Behavior:</b>
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
61 *
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
62 * \par
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
63 * The function is implemented using a 64-bit internal accumulator.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
64 * The input is represented in 1.15 format.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
65 * Intermediate multiplication yields a 2.30 format, and this
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
66 * result is added without saturation to a 64-bit accumulator in 34.30 format.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
67 * With 33 guard bits in the accumulator, there is no risk of overflow, and the
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
68 * full precision of the intermediate multiplication is preserved.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
69 * Finally, the 34.30 result is truncated to 34.15 format by discarding the lower
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
70 * 15 bits, and then saturated to yield a result in 1.15 format.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
71 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
72
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
73 void arm_std_q15(
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
74 q15_t * pSrc,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
75 uint32_t blockSize,
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
76 q15_t * pResult)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
77 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
78 q31_t sum = 0; /* Accumulator */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
79 q31_t meanOfSquares, squareOfMean; /* square of mean and mean of square */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
80 uint32_t blkCnt; /* loop counter */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
81 q63_t sumOfSquares = 0; /* Accumulator */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
82
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
83 #ifndef ARM_MATH_CM0_FAMILY
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
84
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
85 /* Run the below code for Cortex-M4 and Cortex-M3 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
86
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
87 q31_t in; /* input value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
88 q15_t in1; /* input value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
89
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
90 if(blockSize == 1)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
91 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
92 *pResult = 0;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
93 return;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
94 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
95
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
96 /*loop Unrolling */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
97 blkCnt = blockSize >> 2u;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
98
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
99 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
100 ** a second loop below computes the remaining 1 to 3 samples. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
101 while(blkCnt > 0u)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
102 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
103 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
104 /* Compute Sum of squares of the input samples
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
105 * and then store the result in a temporary variable, sum. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
106 in = *__SIMD32(pSrc)++;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
107 sum += ((in << 16) >> 16);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
108 sum += (in >> 16);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
109 sumOfSquares = __SMLALD(in, in, sumOfSquares);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
110 in = *__SIMD32(pSrc)++;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
111 sum += ((in << 16) >> 16);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
112 sum += (in >> 16);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
113 sumOfSquares = __SMLALD(in, in, sumOfSquares);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
114
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
115 /* Decrement the loop counter */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
116 blkCnt--;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
117 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
118
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
119 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
120 ** No loop unrolling is used. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
121 blkCnt = blockSize % 0x4u;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
122
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
123 while(blkCnt > 0u)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
124 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
125 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
126 /* Compute Sum of squares of the input samples
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
127 * and then store the result in a temporary variable, sum. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
128 in1 = *pSrc++;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
129 sumOfSquares = __SMLALD(in1, in1, sumOfSquares);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
130 sum += in1;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
131
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
132 /* Decrement the loop counter */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
133 blkCnt--;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
134 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
135
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
136 /* Compute Mean of squares of the input samples
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
137 * and then store the result in a temporary variable, meanOfSquares. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
138 meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1));
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
139
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
140 /* Compute square of mean */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
141 squareOfMean = (q31_t) ((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1)));
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
142
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
143 /* mean of the squares minus the square of the mean. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
144 /* Compute standard deviation and store the result to the destination */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
145 arm_sqrt_q15(__SSAT((meanOfSquares - squareOfMean) >> 15, 16u), pResult);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
146
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
147 #else
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
148
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
149 /* Run the below code for Cortex-M0 */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
150 q15_t in; /* input value */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
151
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
152 if(blockSize == 1)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
153 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
154 *pResult = 0;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
155 return;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
156 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
157
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
158 /* Loop over blockSize number of values */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
159 blkCnt = blockSize;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
160
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
161 while(blkCnt > 0u)
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
162 {
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
163 /* C = (A[0] * A[0] + A[1] * A[1] + ... + A[blockSize-1] * A[blockSize-1]) */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
164 /* Compute Sum of squares of the input samples
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
165 * and then store the result in a temporary variable, sumOfSquares. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
166 in = *pSrc++;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
167 sumOfSquares += (in * in);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
168
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
169 /* C = (A[0] + A[1] + A[2] + ... + A[blockSize-1]) */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
170 /* Compute sum of all input values and then store the result in a temporary variable, sum. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
171 sum += in;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
172
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
173 /* Decrement the loop counter */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
174 blkCnt--;
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
175 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
176
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
177 /* Compute Mean of squares of the input samples
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
178 * and then store the result in a temporary variable, meanOfSquares. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
179 meanOfSquares = (q31_t)(sumOfSquares / (q63_t)(blockSize - 1));
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
180
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
181 /* Compute square of mean */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
182 squareOfMean = (q31_t) ((q63_t)sum * sum / (q63_t)(blockSize * (blockSize - 1)));
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
183
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
184 /* mean of the squares minus the square of the mean. */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
185 /* Compute standard deviation and store the result to the destination */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
186 arm_sqrt_q15(__SSAT((meanOfSquares - squareOfMean) >> 15, 16u), pResult);
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
187
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
188 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
189
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
190
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
191 }
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
192
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
193 /**
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
194 * @} end of STD group
0c59e7a7782a Working on GPIO and RCC
cin
parents:
diff changeset
195 */