1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
3 *
4 * $Date:         17. January 2013
5 * $Revision:     V1.4.0
6 *
7 * Project:       CMSIS DSP Library
8 * Title:         arm_class_marks_example_f32.c
9 *
10 * Description:   Example code to calculate Minimum, Maximum
11 *                Mean, std and variance of marks obtained in a class
12 *
13 * Target Processor: Cortex-M4/Cortex-M3
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *   - Redistributions of source code must retain the above copyright
19 *     notice, this list of conditions and the following disclaimer.
20 *   - Redistributions in binary form must reproduce the above copyright
21 *     notice, this list of conditions and the following disclaimer in
22 *     the documentation and/or other materials provided with the
23 *     distribution.
24 *   - Neither the name of ARM LIMITED nor the names of its contributors
25 *     may be used to endorse or promote products derived from this
26 *     software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 * -------------------------------------------------------------------- */
41 
42 /**
43  * @addtogroup groupExamples
44  * @{
45  *
46  * @defgroup ClassMarks Class Marks Example
47  *
48  * \par Description:
49  * \par
50  * Demonstrates the use the Maximum, Minimum, Mean, Standard Deviation, Variance
51  * and Matrix functions to calculate statistical values of marks obtained in a class.
52  *
53  * \note This example also demonstrates the usage of static initialization.
54  *
55  * \par Variables Description:
56  * \par
57  * \li \c testMarks_f32 points to the marks scored by 20 students in 4 subjects
58  * \li \c max_marks     Maximum of all marks
59  * \li \c min_marks     Minimum of all marks
60  * \li \c mean          Mean of all marks
61  * \li \c var           Variance of the marks
62  * \li \c std           Standard deviation of the marks
63  * \li \c numStudents   Total number of students in the class
64  *
65  * \par CMSIS DSP Software Library Functions Used:
66  * \par
67  * - arm_mat_init_f32()
68  * - arm_mat_mult_f32()
69  * - arm_max_f32()
70  * - arm_min_f32()
71  * - arm_mean_f32()
72  * - arm_std_f32()
73  * - arm_var_f32()
74  *
75  * <b> Refer  </b>
76  * \link arm_class_marks_example_f32.c \endlink
77  *
78  * \example arm_class_marks_example_f32.c
79  *
80  * @} */
81 
82 #include "arm_math.h"
83 
84 #if defined(SEMIHOSTING)
85 #include <stdio.h>
86 #endif
87 
88 #define USE_STATIC_INIT
89 
90  /* ----------------------------------------------------------------------
91 ** Global defines
92 ** ------------------------------------------------------------------- */
93 
94 #define TEST_LENGTH_SAMPLES   (20*4)
95 
96 /* ----------------------------------------------------------------------
97 ** List of Marks scored by 20 students for 4 subjects
98 ** ------------------------------------------------------------------- */
99 const float32_t testMarks_f32[TEST_LENGTH_SAMPLES] =
100 {
101   42.000000,  37.000000,  81.000000,  28.000000,
102   83.000000,  72.000000,  36.000000,  38.000000,
103   32.000000,  51.000000,  63.000000,  64.000000,
104   97.000000,  82.000000,  95.000000,  90.000000,
105   66.000000,  51.000000,  54.000000,  42.000000,
106   67.000000,  56.000000,  45.000000,  57.000000,
107   67.000000,  69.000000,  35.000000,  52.000000,
108   29.000000,  81.000000,  58.000000,  47.000000,
109   38.000000,  76.000000, 100.000000,  29.000000,
110   33.000000,  47.000000,  29.000000,  50.000000,
111   34.000000,  41.000000,  61.000000,  46.000000,
112   52.000000,  50.000000,  48.000000,  36.000000,
113   47.000000,  55.000000,  44.000000,  40.000000,
114  100.000000,  94.000000,  84.000000,  37.000000,
115   32.000000,  71.000000,  47.000000,  77.000000,
116   31.000000,  50.000000,  49.000000,  35.000000,
117   63.000000,  67.000000,  40.000000,  31.000000,
118   29.000000,  68.000000,  61.000000,  38.000000,
119   31.000000,  28.000000,  28.000000,  76.000000,
120   55.000000,  33.000000,  29.000000,  39.000000
121 };
122 
123 
124 /* ----------------------------------------------------------------------
125 * Number of subjects X 1
126 * ------------------------------------------------------------------- */
127 const float32_t testUnity_f32[4] =
128 {
129   1.000,  1.000,   1.000,  1.000
130 };
131 
132 
133 /* ----------------------------------------------------------------------
134 ** f32 Output buffer
135 ** ------------------------------------------------------------------- */
136 static float32_t testOutput[TEST_LENGTH_SAMPLES];
137 
138 
139 /* ------------------------------------------------------------------
140 * Global defines
141 *------------------------------------------------------------------- */
142 #define   NUMSTUDENTS  20
143 #define     NUMSUBJECTS  4
144 
145 /* ------------------------------------------------------------------
146 * Global variables
147 *------------------------------------------------------------------- */
148 
149  uint32_t    numStudents = 20;
150  uint32_t    numSubjects = 4;
151 float32_t    max_marks, min_marks, mean, std, var;
152  uint32_t    student_num;
153 
154 /* ----------------------------------------------------------------------------------
155 * Main f32 test function.  It returns maximum marks secured and student number
156 * ------------------------------------------------------------------------------- */
157 
main()158 int32_t main()
159 {
160 
161 #ifndef  USE_STATIC_INIT
162 
163   arm_matrix_instance_f32 srcA;
164   arm_matrix_instance_f32 srcB;
165   arm_matrix_instance_f32 dstC;
166 
167   /* Input and output matrices initializations */
168   arm_mat_init_f32(&srcA, numStudents, numSubjects, (float32_t *)testMarks_f32);
169   arm_mat_init_f32(&srcB, numSubjects, 1, (float32_t *)testUnity_f32);
170   arm_mat_init_f32(&dstC, numStudents, 1, testOutput);
171 
172 #else
173 
174   /* Static Initializations of Input and output matrix sizes and array */
175   arm_matrix_instance_f32 srcA = {NUMSTUDENTS, NUMSUBJECTS, (float32_t *)testMarks_f32};
176   arm_matrix_instance_f32 srcB = {NUMSUBJECTS, 1, (float32_t *)testUnity_f32};
177   arm_matrix_instance_f32 dstC = {NUMSTUDENTS, 1, testOutput};
178 
179 #endif
180 
181 
182   /* ----------------------------------------------------------------------
183   *Call the Matrix multiplication process function
184   * ------------------------------------------------------------------- */
185   arm_mat_mult_f32(&srcA, &srcB, &dstC);
186 
187   /* ----------------------------------------------------------------------
188   ** Call the Max function to calculate max marks among numStudents
189   ** ------------------------------------------------------------------- */
190   arm_max_f32(testOutput, numStudents, &max_marks, &student_num);
191 
192   /* ----------------------------------------------------------------------
193   ** Call the Min function to calculate min marks among numStudents
194   ** ------------------------------------------------------------------- */
195   arm_min_f32(testOutput, numStudents, &min_marks, &student_num);
196 
197   /* ----------------------------------------------------------------------
198   ** Call the Mean function to calculate mean
199   ** ------------------------------------------------------------------- */
200   arm_mean_f32(testOutput, numStudents, &mean);
201 
202   /* ----------------------------------------------------------------------
203   ** Call the std function to calculate standard deviation
204   ** ------------------------------------------------------------------- */
205   arm_std_f32(testOutput, numStudents, &std);
206 
207   /* ----------------------------------------------------------------------
208   ** Call the var function to calculate variance
209   ** ------------------------------------------------------------------- */
210   arm_var_f32(testOutput, numStudents, &var);
211 
212 #if defined(SEMIHOSTING)
213   printf("mean = %f, std = %f\n",(double)mean,(double)std);
214 #endif
215 
216 #if !defined(SEMIHOSTING)
217   while (1);                             /* main function does not return */
218 #endif
219 }
220