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