]> git.gag.com Git - fw/stlink/blob - exampleF4/CMSIS/DSP_Lib/Examples/arm_variance_example/arm_variance_example_f32.c
Added all the F4 libraries to the project
[fw/stlink] / exampleF4 / CMSIS / DSP_Lib / Examples / arm_variance_example / arm_variance_example_f32.c
1 /* ----------------------------------------------------------------------    
2 * Copyright (C) 2010 ARM Limited. All rights reserved.     
3 *    
4 * $Date:        29. November 2010  
5 * $Revision:    V1.0.3
6 *     
7 * Project:          CMSIS DSP Library  
8 * Title:            arm_variance_example_f32.c           
9
10 * Description:  Example code demonstrating variance calculation of input sequence.
11 *     
12 * Target Processor: Cortex-M4/Cortex-M3  
13 *
14 *
15 * Version 1.0.3 2010/11/29 
16 *    Re-organized the CMSIS folders and updated documentation. 
17
18 * Version 1.0.1 2010/10/05 KK 
19 *    Production release and review comments incorporated.  
20 *
21 * Version 1.0.0 2010/09/20 KK
22 *    Production release and review comments incorporated.
23 * ------------------------------------------------------------------- */
24
25 /**
26  * @ingroup groupExamples
27  */
28
29 /**   
30  * @defgroup VarianceExample Variance Example   
31  *
32  * \par Description: 
33  * \par
34  * Demonstrates the use of Basic Math and Support Functions to calculate the variance of an 
35  * input sequence with N samples. Uniformly distributed white noise is taken as input. 
36  * 
37  * \par Algorithm:
38  * \par
39  * The variance of a sequence is the mean of the squared deviation of the sequence from its mean.
40  * \par
41  * This is denoted by the following equation: 
42  * <pre> variance = ((x[0] - x') * (x[0] - x') + (x[1] - x') * (x[1] - x') + ... + * (x[n-1] - x') * (x[n-1] - x')) / (N-1)</pre>
43  * where, <code>x[n]</code> is the input sequence, <code>N</code> is the number of input samples, and
44  * <code>x'</code> is the mean value of the input sequence, <code>x[n]</code>.
45  * \par
46  * The mean value <code>x'</code> is defined as:
47  * <pre> x' = (x[0] + x[1] + ... + x[n-1]) / N</pre>
48  *
49  * \par Block Diagram:
50  * \par
51  * \image html Variance.gif
52  *
53  *
54  * \par Variables Description:
55  * \par
56  * \li \c testInput_f32 points to the input data
57  * \li \c wire1, \c wir2, \c wire3 temporary buffers
58  * \li \c blockSize number of samples processed at a time
59  * \li \c refVarianceOut reference variance value 
60  *
61  * \par CMSIS DSP Software Library Functions Used:
62  * \par
63  * - arm_dot_prod_f32()
64  * - arm_mult_f32()
65  * - arm_sub_f32()
66  * - arm_fill_f32()
67  * - arm_copy_f32()
68  *
69  * <b> Refer  </b>
70  * \link arm_variance_example_f32.c \endlink
71  *
72  */
73
74
75 /** \example arm_variance_example_f32.c
76   */ 
77 #include <math.h>    
78 #include "arm_math.h"
79
80 /* ----------------------------------------------------------------------
81 * Defines each of the tests performed
82 * ------------------------------------------------------------------- */
83 #define MAX_BLOCKSIZE   32
84 #define DELTA           (0.000001f)
85
86
87 /* ----------------------------------------------------------------------
88 * Declare I/O buffers 
89 * ------------------------------------------------------------------- */
90 float32_t wire1[MAX_BLOCKSIZE];
91 float32_t wire2[MAX_BLOCKSIZE];
92 float32_t wire3[MAX_BLOCKSIZE];
93
94 /* ----------------------------------------------------------------------
95 * Test input data for Floating point Variance example for 32-blockSize
96 * Generated by the MATLAB randn() function
97 * ------------------------------------------------------------------- */
98
99 float32_t testInput_f32[32] = 
100
101 -0.432564811528221,     -1.665584378238097,     0.125332306474831,              0.287676420358549,      
102 -1.146471350681464,     1.190915465642999,              1.189164201652103,              -0.037633276593318,     
103 0.327292361408654,              0.174639142820925,              -0.186708577681439,     0.725790548293303,      
104 -0.588316543014189,     2.183185818197101,              -0.136395883086596,     0.113931313520810,      
105 1.066768211359189,              0.059281460523605,              -0.095648405483669,     -0.832349463650022,     
106 0.294410816392640,              -1.336181857937804,     0.714324551818952,              1.623562064446271,      
107 -0.691775701702287,     0.857996672828263,              1.254001421602532,              -1.593729576447477,     
108 -1.440964431901020,     0.571147623658178,              -0.399885577715363,     0.689997375464345
109   
110 };
111
112 /* ----------------------------------------------------------------------
113 * Declare Global variables 
114 * ------------------------------------------------------------------- */
115 uint32_t blockSize = 32;
116 float32_t  refVarianceOut = 0.903941793931839; 
117
118 /* ----------------------------------------------------------------------
119 * Variance calculation test
120 * ------------------------------------------------------------------- */
121
122 int32_t main(void)
123 {
124         arm_status status;
125         float32_t mean, oneByBlockSize;
126         float32_t variance;
127         float32_t diff;
128         
129         status = ARM_MATH_SUCCESS;
130         
131         /* Calculation of mean value of input */
132         
133         /* x' = 1/blockSize * (x(0)* 1 + x(1) * 1 + ... + x(n-1) * 1) */
134         
135         /* Fill wire1 buffer with 1.0 value */
136         arm_fill_f32(1.0,  wire1, blockSize);
137         
138         /* Calculate the dot product of wire1 and wire2 */
139         /* (x(0)* 1 + x(1) * 1 + ...+ x(n-1) * 1) */
140         arm_dot_prod_f32(testInput_f32, wire1, blockSize, &mean);
141         
142         /* Calculation of 1/blockSize */
143         oneByBlockSize = 1.0 / (blockSize);
144         
145         /* 1/blockSize * (x(0)* 1 + x(1) * 1 + ... + x(n-1) * 1)  */
146         arm_mult_f32(&mean, &oneByBlockSize, &mean, 1);
147         
148         
149         /* Calculation of variance value of input */
150         
151         /* (1/blockSize) * (x(0) - x') * (x(0) - x') + (x(1) - x') * (x(1) - x') + ... + (x(n-1) - x') * (x(n-1) - x') */
152         
153         /* Fill wire2 with mean value x' */
154         arm_fill_f32(mean,  wire2, blockSize);
155         
156         /* wire3 contains (x-x') */             
157         arm_sub_f32(testInput_f32, wire2, wire3, blockSize);
158         
159         /* wire2 contains (x-x') */                             
160         arm_copy_f32(wire3, wire2, blockSize);
161         
162         /* (x(0) - x') * (x(0) - x') + (x(1) - x') * (x(1) - x') + ... + (x(n-1) - x') * (x(n-1) - x') */
163         arm_dot_prod_f32(wire2, wire3, blockSize, &variance); 
164
165     /* Calculation of 1/blockSize */
166         oneByBlockSize = 1.0 / (blockSize - 1);
167
168         /* Calculation of variance */           
169         arm_mult_f32(&variance, &oneByBlockSize, &variance, 1);
170         
171         /* absolute value of difference between ref and test */
172         diff = fabsf(refVarianceOut - variance);
173         
174         /* Comparison of variance value with reference */
175         if(diff > DELTA)
176         {
177                 status = ARM_MATH_TEST_FAILURE;
178         }
179                 
180         if( status != ARM_MATH_SUCCESS)
181         {
182           while(1);
183         }
184
185     while(1);                             /* main function does not return */
186 }
187
188  /** \endlink */
189