1 /******************************************************************************
2 * @file svm_functions.h
3 * @brief Public header file for CMSIS DSP Library
4 * @version V1.9.0
5 * @date 23 April 2021
6 * Target Processor: Cortex-M and Cortex-A cores
7 ******************************************************************************/
8 /*
9 * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
10 *
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the License); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 */
25
26
27 #ifndef _SVM_FUNCTIONS_H_
28 #define _SVM_FUNCTIONS_H_
29
30 #include "arm_math_types.h"
31 #include "arm_math_memory.h"
32
33 #include "dsp/none.h"
34 #include "dsp/utils.h"
35 #include "dsp/svm_defines.h"
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41
42 #define STEP(x) (x) <= 0 ? 0 : 1
43
44 /**
45 * @defgroup groupSVM SVM Functions
46 * This set of functions is implementing SVM classification on 2 classes.
47 * The training must be done from scikit-learn. The parameters can be easily
48 * generated from the scikit-learn object. Some examples are given in
49 * DSP/Testing/PatternGeneration/SVM.py
50 *
51 * If more than 2 classes are needed, the functions in this folder
52 * will have to be used, as building blocks, to do multi-class classification.
53 *
54 * No multi-class classification is provided in this SVM folder.
55 *
56 */
57
58 /**
59 * @brief Integer exponentiation
60 * @param[in] x value
61 * @param[in] nb integer exponent >= 1
62 * @return x^nb
63 *
64 */
arm_exponent_f32(float32_t x,int32_t nb)65 __STATIC_INLINE float32_t arm_exponent_f32(float32_t x, int32_t nb)
66 {
67 float32_t r = x;
68 nb --;
69 while(nb > 0)
70 {
71 r = r * x;
72 nb--;
73 }
74 return(r);
75 }
76
77
78
79
80
81 /**
82 * @brief Instance structure for linear SVM prediction function.
83 */
84 typedef struct
85 {
86 uint32_t nbOfSupportVectors; /**< Number of support vectors */
87 uint32_t vectorDimension; /**< Dimension of vector space */
88 float32_t intercept; /**< Intercept */
89 const float32_t *dualCoefficients; /**< Dual coefficients */
90 const float32_t *supportVectors; /**< Support vectors */
91 const int32_t *classes; /**< The two SVM classes */
92 } arm_svm_linear_instance_f32;
93
94
95 /**
96 * @brief Instance structure for polynomial SVM prediction function.
97 */
98 typedef struct
99 {
100 uint32_t nbOfSupportVectors; /**< Number of support vectors */
101 uint32_t vectorDimension; /**< Dimension of vector space */
102 float32_t intercept; /**< Intercept */
103 const float32_t *dualCoefficients; /**< Dual coefficients */
104 const float32_t *supportVectors; /**< Support vectors */
105 const int32_t *classes; /**< The two SVM classes */
106 int32_t degree; /**< Polynomial degree */
107 float32_t coef0; /**< Polynomial constant */
108 float32_t gamma; /**< Gamma factor */
109 } arm_svm_polynomial_instance_f32;
110
111 /**
112 * @brief Instance structure for rbf SVM prediction function.
113 */
114 typedef struct
115 {
116 uint32_t nbOfSupportVectors; /**< Number of support vectors */
117 uint32_t vectorDimension; /**< Dimension of vector space */
118 float32_t intercept; /**< Intercept */
119 const float32_t *dualCoefficients; /**< Dual coefficients */
120 const float32_t *supportVectors; /**< Support vectors */
121 const int32_t *classes; /**< The two SVM classes */
122 float32_t gamma; /**< Gamma factor */
123 } arm_svm_rbf_instance_f32;
124
125 /**
126 * @brief Instance structure for sigmoid SVM prediction function.
127 */
128 typedef struct
129 {
130 uint32_t nbOfSupportVectors; /**< Number of support vectors */
131 uint32_t vectorDimension; /**< Dimension of vector space */
132 float32_t intercept; /**< Intercept */
133 const float32_t *dualCoefficients; /**< Dual coefficients */
134 const float32_t *supportVectors; /**< Support vectors */
135 const int32_t *classes; /**< The two SVM classes */
136 float32_t coef0; /**< Independent constant */
137 float32_t gamma; /**< Gamma factor */
138 } arm_svm_sigmoid_instance_f32;
139
140 /**
141 * @brief SVM linear instance init function
142 * @param[in] S Parameters for SVM functions
143 * @param[in] nbOfSupportVectors Number of support vectors
144 * @param[in] vectorDimension Dimension of vector space
145 * @param[in] intercept Intercept
146 * @param[in] dualCoefficients Array of dual coefficients
147 * @param[in] supportVectors Array of support vectors
148 * @param[in] classes Array of 2 classes ID
149 * @return none.
150 *
151 */
152
153
154 void arm_svm_linear_init_f32(arm_svm_linear_instance_f32 *S,
155 uint32_t nbOfSupportVectors,
156 uint32_t vectorDimension,
157 float32_t intercept,
158 const float32_t *dualCoefficients,
159 const float32_t *supportVectors,
160 const int32_t *classes);
161
162 /**
163 * @brief SVM linear prediction
164 * @param[in] S Pointer to an instance of the linear SVM structure.
165 * @param[in] in Pointer to input vector
166 * @param[out] pResult Decision value
167 * @return none.
168 *
169 */
170
171 void arm_svm_linear_predict_f32(const arm_svm_linear_instance_f32 *S,
172 const float32_t * in,
173 int32_t * pResult);
174
175
176 /**
177 * @brief SVM polynomial instance init function
178 * @param[in] S points to an instance of the polynomial SVM structure.
179 * @param[in] nbOfSupportVectors Number of support vectors
180 * @param[in] vectorDimension Dimension of vector space
181 * @param[in] intercept Intercept
182 * @param[in] dualCoefficients Array of dual coefficients
183 * @param[in] supportVectors Array of support vectors
184 * @param[in] classes Array of 2 classes ID
185 * @param[in] degree Polynomial degree
186 * @param[in] coef0 coeff0 (scikit-learn terminology)
187 * @param[in] gamma gamma (scikit-learn terminology)
188 * @return none.
189 *
190 */
191
192
193 void arm_svm_polynomial_init_f32(arm_svm_polynomial_instance_f32 *S,
194 uint32_t nbOfSupportVectors,
195 uint32_t vectorDimension,
196 float32_t intercept,
197 const float32_t *dualCoefficients,
198 const float32_t *supportVectors,
199 const int32_t *classes,
200 int32_t degree,
201 float32_t coef0,
202 float32_t gamma
203 );
204
205 /**
206 * @brief SVM polynomial prediction
207 * @param[in] S Pointer to an instance of the polynomial SVM structure.
208 * @param[in] in Pointer to input vector
209 * @param[out] pResult Decision value
210 * @return none.
211 *
212 */
213 void arm_svm_polynomial_predict_f32(const arm_svm_polynomial_instance_f32 *S,
214 const float32_t * in,
215 int32_t * pResult);
216
217
218 /**
219 * @brief SVM radial basis function instance init function
220 * @param[in] S points to an instance of the polynomial SVM structure.
221 * @param[in] nbOfSupportVectors Number of support vectors
222 * @param[in] vectorDimension Dimension of vector space
223 * @param[in] intercept Intercept
224 * @param[in] dualCoefficients Array of dual coefficients
225 * @param[in] supportVectors Array of support vectors
226 * @param[in] classes Array of 2 classes ID
227 * @param[in] gamma gamma (scikit-learn terminology)
228 * @return none.
229 *
230 */
231
232 void arm_svm_rbf_init_f32(arm_svm_rbf_instance_f32 *S,
233 uint32_t nbOfSupportVectors,
234 uint32_t vectorDimension,
235 float32_t intercept,
236 const float32_t *dualCoefficients,
237 const float32_t *supportVectors,
238 const int32_t *classes,
239 float32_t gamma
240 );
241
242 /**
243 * @brief SVM rbf prediction
244 * @param[in] S Pointer to an instance of the rbf SVM structure.
245 * @param[in] in Pointer to input vector
246 * @param[out] pResult decision value
247 * @return none.
248 *
249 */
250 void arm_svm_rbf_predict_f32(const arm_svm_rbf_instance_f32 *S,
251 const float32_t * in,
252 int32_t * pResult);
253
254 /**
255 * @brief SVM sigmoid instance init function
256 * @param[in] S points to an instance of the rbf SVM structure.
257 * @param[in] nbOfSupportVectors Number of support vectors
258 * @param[in] vectorDimension Dimension of vector space
259 * @param[in] intercept Intercept
260 * @param[in] dualCoefficients Array of dual coefficients
261 * @param[in] supportVectors Array of support vectors
262 * @param[in] classes Array of 2 classes ID
263 * @param[in] coef0 coeff0 (scikit-learn terminology)
264 * @param[in] gamma gamma (scikit-learn terminology)
265 * @return none.
266 *
267 */
268
269 void arm_svm_sigmoid_init_f32(arm_svm_sigmoid_instance_f32 *S,
270 uint32_t nbOfSupportVectors,
271 uint32_t vectorDimension,
272 float32_t intercept,
273 const float32_t *dualCoefficients,
274 const float32_t *supportVectors,
275 const int32_t *classes,
276 float32_t coef0,
277 float32_t gamma
278 );
279
280 /**
281 * @brief SVM sigmoid prediction
282 * @param[in] S Pointer to an instance of the rbf SVM structure.
283 * @param[in] in Pointer to input vector
284 * @param[out] pResult Decision value
285 * @return none.
286 *
287 */
288 void arm_svm_sigmoid_predict_f32(const arm_svm_sigmoid_instance_f32 *S,
289 const float32_t * in,
290 int32_t * pResult);
291
292
293
294
295 #ifdef __cplusplus
296 }
297 #endif
298
299 #endif /* ifndef _SVM_FUNCTIONS_H_ */
300