1 /******************************************************************************
2  * @file     svm_functions_f16.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_F16_H_
28 #define _SVM_FUNCTIONS_F16_H_
29 
30 #include "arm_math_types_f16.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 #if defined(ARM_FLOAT16_SUPPORTED)
43 
44 #define STEP(x) (x) <= 0 ? 0 : 1
45 
46 /**
47  * @defgroup groupSVM SVM Functions
48  * This set of functions is implementing SVM classification on 2 classes.
49  * The training must be done from scikit-learn. The parameters can be easily
50  * generated from the scikit-learn object. Some examples are given in
51  * DSP/Testing/PatternGeneration/SVM.py
52  *
53  * If more than 2 classes are needed, the functions in this folder
54  * will have to be used, as building blocks, to do multi-class classification.
55  *
56  * No multi-class classification is provided in this SVM folder.
57  *
58  */
59 
60 /**
61  * @brief Integer exponentiation
62  * @param[in]    x           value
63  * @param[in]    nb          integer exponent >= 1
64  * @return x^nb
65  *
66  */
arm_exponent_f16(float16_t x,int32_t nb)67 __STATIC_INLINE float16_t arm_exponent_f16(float16_t x, int32_t nb)
68 {
69     float16_t r = x;
70     nb --;
71     while(nb > 0)
72     {
73         r = r * x;
74         nb--;
75     }
76     return(r);
77 }
78 
79 
80 /**
81  * @brief Instance structure for linear SVM prediction function.
82  */
83 typedef struct
84 {
85   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
86   uint32_t        vectorDimension;        /**< Dimension of vector space */
87   float16_t       intercept;              /**< Intercept */
88   const float16_t *dualCoefficients;      /**< Dual coefficients */
89   const float16_t *supportVectors;        /**< Support vectors */
90   const int32_t   *classes;               /**< The two SVM classes */
91 } arm_svm_linear_instance_f16;
92 
93 
94 /**
95  * @brief Instance structure for polynomial SVM prediction function.
96  */
97 typedef struct
98 {
99   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
100   uint32_t        vectorDimension;        /**< Dimension of vector space */
101   float16_t       intercept;              /**< Intercept */
102   const float16_t *dualCoefficients;      /**< Dual coefficients */
103   const float16_t *supportVectors;        /**< Support vectors */
104   const int32_t   *classes;               /**< The two SVM classes */
105   int32_t         degree;                 /**< Polynomial degree */
106   float16_t       coef0;                  /**< Polynomial constant */
107   float16_t       gamma;                  /**< Gamma factor */
108 } arm_svm_polynomial_instance_f16;
109 
110 /**
111  * @brief Instance structure for rbf SVM prediction function.
112  */
113 typedef struct
114 {
115   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
116   uint32_t        vectorDimension;        /**< Dimension of vector space */
117   float16_t       intercept;              /**< Intercept */
118   const float16_t *dualCoefficients;      /**< Dual coefficients */
119   const float16_t *supportVectors;        /**< Support vectors */
120   const int32_t   *classes;               /**< The two SVM classes */
121   float16_t       gamma;                  /**< Gamma factor */
122 } arm_svm_rbf_instance_f16;
123 
124 /**
125  * @brief Instance structure for sigmoid SVM prediction function.
126  */
127 typedef struct
128 {
129   uint32_t        nbOfSupportVectors;     /**< Number of support vectors */
130   uint32_t        vectorDimension;        /**< Dimension of vector space */
131   float16_t       intercept;              /**< Intercept */
132   const float16_t *dualCoefficients;      /**< Dual coefficients */
133   const float16_t *supportVectors;        /**< Support vectors */
134   const int32_t   *classes;               /**< The two SVM classes */
135   float16_t       coef0;                  /**< Independent constant */
136   float16_t       gamma;                  /**< Gamma factor */
137 } arm_svm_sigmoid_instance_f16;
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  * @return none.
149  *
150  */
151 
152 
153 void arm_svm_linear_init_f16(arm_svm_linear_instance_f16 *S,
154   uint32_t nbOfSupportVectors,
155   uint32_t vectorDimension,
156   float16_t intercept,
157   const float16_t *dualCoefficients,
158   const float16_t *supportVectors,
159   const int32_t  *classes);
160 
161 /**
162  * @brief SVM linear prediction
163  * @param[in]    S          Pointer to an instance of the linear SVM structure.
164  * @param[in]    in         Pointer to input vector
165  * @param[out]   pResult    Decision value
166  * @return none.
167  *
168  */
169 
170 void arm_svm_linear_predict_f16(const arm_svm_linear_instance_f16 *S,
171    const float16_t * in,
172    int32_t * pResult);
173 
174 
175 /**
176  * @brief        SVM polynomial instance init function
177  * @param[in]    S                      points to an instance of the polynomial SVM structure.
178  * @param[in]    nbOfSupportVectors     Number of support vectors
179  * @param[in]    vectorDimension        Dimension of vector space
180  * @param[in]    intercept              Intercept
181  * @param[in]    dualCoefficients       Array of dual coefficients
182  * @param[in]    supportVectors         Array of support vectors
183  * @param[in]    classes                Array of 2 classes ID
184  * @param[in]    degree                 Polynomial degree
185  * @param[in]    coef0                  coeff0 (scikit-learn terminology)
186  * @param[in]    gamma                  gamma (scikit-learn terminology)
187  * @return none.
188  *
189  */
190 
191 
192 void arm_svm_polynomial_init_f16(arm_svm_polynomial_instance_f16 *S,
193   uint32_t nbOfSupportVectors,
194   uint32_t vectorDimension,
195   float16_t intercept,
196   const float16_t *dualCoefficients,
197   const float16_t *supportVectors,
198   const int32_t   *classes,
199   int32_t      degree,
200   float16_t coef0,
201   float16_t gamma
202   );
203 
204 /**
205  * @brief SVM polynomial prediction
206  * @param[in]    S          Pointer to an instance of the polynomial SVM structure.
207  * @param[in]    in         Pointer to input vector
208  * @param[out]   pResult    Decision value
209  * @return none.
210  *
211  */
212 void arm_svm_polynomial_predict_f16(const arm_svm_polynomial_instance_f16 *S,
213    const float16_t * in,
214    int32_t * pResult);
215 
216 
217 /**
218  * @brief        SVM radial basis function instance init function
219  * @param[in]    S                      points to an instance of the polynomial SVM structure.
220  * @param[in]    nbOfSupportVectors     Number of support vectors
221  * @param[in]    vectorDimension        Dimension of vector space
222  * @param[in]    intercept              Intercept
223  * @param[in]    dualCoefficients       Array of dual coefficients
224  * @param[in]    supportVectors         Array of support vectors
225  * @param[in]    classes                Array of 2 classes ID
226  * @param[in]    gamma                  gamma (scikit-learn terminology)
227  * @return none.
228  *
229  */
230 
231 void arm_svm_rbf_init_f16(arm_svm_rbf_instance_f16 *S,
232   uint32_t nbOfSupportVectors,
233   uint32_t vectorDimension,
234   float16_t intercept,
235   const float16_t *dualCoefficients,
236   const float16_t *supportVectors,
237   const int32_t   *classes,
238   float16_t gamma
239   );
240 
241 /**
242  * @brief SVM rbf prediction
243  * @param[in]    S         Pointer to an instance of the rbf SVM structure.
244  * @param[in]    in        Pointer to input vector
245  * @param[out]   pResult   decision value
246  * @return none.
247  *
248  */
249 void arm_svm_rbf_predict_f16(const arm_svm_rbf_instance_f16 *S,
250    const float16_t * in,
251    int32_t * pResult);
252 
253 /**
254  * @brief        SVM sigmoid instance init function
255  * @param[in]    S                      points to an instance of the rbf SVM structure.
256  * @param[in]    nbOfSupportVectors     Number of support vectors
257  * @param[in]    vectorDimension        Dimension of vector space
258  * @param[in]    intercept              Intercept
259  * @param[in]    dualCoefficients       Array of dual coefficients
260  * @param[in]    supportVectors         Array of support vectors
261  * @param[in]    classes                Array of 2 classes ID
262  * @param[in]    coef0                  coeff0 (scikit-learn terminology)
263  * @param[in]    gamma                  gamma (scikit-learn terminology)
264  * @return none.
265  *
266  */
267 
268 void arm_svm_sigmoid_init_f16(arm_svm_sigmoid_instance_f16 *S,
269   uint32_t nbOfSupportVectors,
270   uint32_t vectorDimension,
271   float16_t intercept,
272   const float16_t *dualCoefficients,
273   const float16_t *supportVectors,
274   const int32_t   *classes,
275   float16_t coef0,
276   float16_t gamma
277   );
278 
279 /**
280  * @brief SVM sigmoid prediction
281  * @param[in]    S        Pointer to an instance of the rbf SVM structure.
282  * @param[in]    in       Pointer to input vector
283  * @param[out]   pResult  Decision value
284  * @return none.
285  *
286  */
287 void arm_svm_sigmoid_predict_f16(const arm_svm_sigmoid_instance_f16 *S,
288    const float16_t * in,
289    int32_t * pResult);
290 
291 
292 
293 #endif /*defined(ARM_FLOAT16_SUPPORTED)*/
294 #ifdef   __cplusplus
295 }
296 #endif
297 
298 #endif /* ifndef _SVM_FUNCTIONS_F16_H_ */
299