1 /******************************************************************************
2  * @file     svm_functions.h
3  * @brief    Public header file for CMSIS DSP Library
4  * @version  V1.10.0
5  * @date     08 July 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  */
arm_exponent_f32(float32_t x,int32_t nb)64 __STATIC_INLINE float32_t arm_exponent_f32(float32_t x, int32_t nb)
65 {
66     float32_t r = x;
67     nb --;
68     while(nb > 0)
69     {
70         r = r * x;
71         nb--;
72     }
73     return(r);
74 }
75 
76 
77 /**
78  * @brief Instance structure for linear SVM prediction function.
79  */
80 typedef struct
81 {
82   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
83   uint32_t        vectorDimension;        /**< Dimension of vector space */
84   float32_t       intercept;              /**< Intercept */
85   const float32_t *dualCoefficients;      /**< Dual coefficients */
86   const float32_t *supportVectors;        /**< Support vectors */
87   const int32_t   *classes;               /**< The two SVM classes */
88 } arm_svm_linear_instance_f32;
89 
90 
91 /**
92  * @brief Instance structure for polynomial SVM prediction function.
93  */
94 typedef struct
95 {
96   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
97   uint32_t        vectorDimension;        /**< Dimension of vector space */
98   float32_t       intercept;              /**< Intercept */
99   const float32_t *dualCoefficients;      /**< Dual coefficients */
100   const float32_t *supportVectors;        /**< Support vectors */
101   const int32_t   *classes;               /**< The two SVM classes */
102   int32_t         degree;                 /**< Polynomial degree */
103   float32_t       coef0;                  /**< Polynomial constant */
104   float32_t       gamma;                  /**< Gamma factor */
105 } arm_svm_polynomial_instance_f32;
106 
107 
108 /**
109  * @brief Instance structure for rbf SVM prediction function.
110  */
111 typedef struct
112 {
113   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
114   uint32_t        vectorDimension;        /**< Dimension of vector space */
115   float32_t       intercept;              /**< Intercept */
116   const float32_t *dualCoefficients;      /**< Dual coefficients */
117   const float32_t *supportVectors;        /**< Support vectors */
118   const int32_t   *classes;               /**< The two SVM classes */
119   float32_t       gamma;                  /**< Gamma factor */
120 } arm_svm_rbf_instance_f32;
121 
122 
123 /**
124  * @brief Instance structure for sigmoid SVM prediction function.
125  */
126 typedef struct
127 {
128   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
129   uint32_t        vectorDimension;        /**< Dimension of vector space */
130   float32_t       intercept;              /**< Intercept */
131   const float32_t *dualCoefficients;      /**< Dual coefficients */
132   const float32_t *supportVectors;        /**< Support vectors */
133   const int32_t   *classes;               /**< The two SVM classes */
134   float32_t       coef0;                  /**< Independent constant */
135   float32_t       gamma;                  /**< Gamma factor */
136 } arm_svm_sigmoid_instance_f32;
137 
138 
139 /**
140  * @brief        SVM linear instance init function
141  * @param[in]    S                      Parameters for SVM functions
142  * @param[in]    nbOfSupportVectors     Number of support vectors
143  * @param[in]    vectorDimension        Dimension of vector space
144  * @param[in]    intercept              Intercept
145  * @param[in]    dualCoefficients       Array of dual coefficients
146  * @param[in]    supportVectors         Array of support vectors
147  * @param[in]    classes                Array of 2 classes ID
148  */
149 void arm_svm_linear_init_f32(arm_svm_linear_instance_f32 *S,
150   uint32_t nbOfSupportVectors,
151   uint32_t vectorDimension,
152   float32_t intercept,
153   const float32_t *dualCoefficients,
154   const float32_t *supportVectors,
155   const int32_t  *classes);
156 
157 
158 /**
159  * @brief SVM linear prediction
160  * @param[in]    S          Pointer to an instance of the linear SVM structure.
161  * @param[in]    in         Pointer to input vector
162  * @param[out]   pResult    Decision value
163  */
164 void arm_svm_linear_predict_f32(const arm_svm_linear_instance_f32 *S,
165    const float32_t * in,
166    int32_t * pResult);
167 
168 
169 /**
170  * @brief        SVM polynomial instance init function
171  * @param[in]    S                      points to an instance of the polynomial SVM structure.
172  * @param[in]    nbOfSupportVectors     Number of support vectors
173  * @param[in]    vectorDimension        Dimension of vector space
174  * @param[in]    intercept              Intercept
175  * @param[in]    dualCoefficients       Array of dual coefficients
176  * @param[in]    supportVectors         Array of support vectors
177  * @param[in]    classes                Array of 2 classes ID
178  * @param[in]    degree                 Polynomial degree
179  * @param[in]    coef0                  coeff0 (scikit-learn terminology)
180  * @param[in]    gamma                  gamma (scikit-learn terminology)
181  */
182 void arm_svm_polynomial_init_f32(arm_svm_polynomial_instance_f32 *S,
183   uint32_t nbOfSupportVectors,
184   uint32_t vectorDimension,
185   float32_t intercept,
186   const float32_t *dualCoefficients,
187   const float32_t *supportVectors,
188   const int32_t   *classes,
189   int32_t      degree,
190   float32_t coef0,
191   float32_t gamma
192   );
193 
194 
195 /**
196  * @brief SVM polynomial prediction
197  * @param[in]    S          Pointer to an instance of the polynomial SVM structure.
198  * @param[in]    in         Pointer to input vector
199  * @param[out]   pResult    Decision value
200  */
201 void arm_svm_polynomial_predict_f32(const arm_svm_polynomial_instance_f32 *S,
202    const float32_t * in,
203    int32_t * pResult);
204 
205 
206 /**
207  * @brief        SVM radial basis function instance init function
208  * @param[in]    S                      points to an instance of the polynomial SVM structure.
209  * @param[in]    nbOfSupportVectors     Number of support vectors
210  * @param[in]    vectorDimension        Dimension of vector space
211  * @param[in]    intercept              Intercept
212  * @param[in]    dualCoefficients       Array of dual coefficients
213  * @param[in]    supportVectors         Array of support vectors
214  * @param[in]    classes                Array of 2 classes ID
215  * @param[in]    gamma                  gamma (scikit-learn terminology)
216  */
217 void arm_svm_rbf_init_f32(arm_svm_rbf_instance_f32 *S,
218   uint32_t nbOfSupportVectors,
219   uint32_t vectorDimension,
220   float32_t intercept,
221   const float32_t *dualCoefficients,
222   const float32_t *supportVectors,
223   const int32_t   *classes,
224   float32_t gamma
225   );
226 
227 
228 /**
229  * @brief SVM rbf prediction
230  * @param[in]    S         Pointer to an instance of the rbf SVM structure.
231  * @param[in]    in        Pointer to input vector
232  * @param[out]   pResult   decision value
233  */
234 void arm_svm_rbf_predict_f32(const arm_svm_rbf_instance_f32 *S,
235    const float32_t * in,
236    int32_t * pResult);
237 
238 
239 /**
240  * @brief        SVM sigmoid instance init function
241  * @param[in]    S                      points to an instance of the rbf SVM structure.
242  * @param[in]    nbOfSupportVectors     Number of support vectors
243  * @param[in]    vectorDimension        Dimension of vector space
244  * @param[in]    intercept              Intercept
245  * @param[in]    dualCoefficients       Array of dual coefficients
246  * @param[in]    supportVectors         Array of support vectors
247  * @param[in]    classes                Array of 2 classes ID
248  * @param[in]    coef0                  coeff0 (scikit-learn terminology)
249  * @param[in]    gamma                  gamma (scikit-learn terminology)
250  */
251 void arm_svm_sigmoid_init_f32(arm_svm_sigmoid_instance_f32 *S,
252   uint32_t nbOfSupportVectors,
253   uint32_t vectorDimension,
254   float32_t intercept,
255   const float32_t *dualCoefficients,
256   const float32_t *supportVectors,
257   const int32_t   *classes,
258   float32_t coef0,
259   float32_t gamma
260   );
261 
262 
263 /**
264  * @brief SVM sigmoid prediction
265  * @param[in]    S        Pointer to an instance of the rbf SVM structure.
266  * @param[in]    in       Pointer to input vector
267  * @param[out]   pResult  Decision value
268  */
269 void arm_svm_sigmoid_predict_f32(const arm_svm_sigmoid_instance_f32 *S,
270    const float32_t * in,
271    int32_t * pResult);
272 
273 
274 
275 
276 #ifdef   __cplusplus
277 }
278 #endif
279 
280 #endif /* ifndef _SVM_FUNCTIONS_H_ */
281