svm_functions.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #ifndef _SVM_FUNCTIONS_H_
  26. #define _SVM_FUNCTIONS_H_
  27. #include "arm_math_types.h"
  28. #include "arm_math_memory.h"
  29. #include "dsp/none.h"
  30. #include "dsp/utils.h"
  31. #include "dsp/svm_defines.h"
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. #define STEP(x) (x) <= 0 ? 0 : 1
  37. /**
  38. * @defgroup groupSVM SVM Functions
  39. * This set of functions is implementing SVM classification on 2 classes.
  40. * The training must be done from scikit-learn. The parameters can be easily
  41. * generated from the scikit-learn object. Some examples are given in
  42. * DSP/Testing/PatternGeneration/SVM.py
  43. *
  44. * If more than 2 classes are needed, the functions in this folder
  45. * will have to be used, as building blocks, to do multi-class classification.
  46. *
  47. * No multi-class classification is provided in this SVM folder.
  48. *
  49. */
  50. /**
  51. * @brief Integer exponentiation
  52. * @param[in] x value
  53. * @param[in] nb integer exponent >= 1
  54. * @return x^nb
  55. */
  56. __STATIC_INLINE float32_t arm_exponent_f32(float32_t x, int32_t nb)
  57. {
  58. float32_t r = x;
  59. nb --;
  60. while(nb > 0)
  61. {
  62. r = r * x;
  63. nb--;
  64. }
  65. return(r);
  66. }
  67. /**
  68. * @brief Instance structure for linear SVM prediction function.
  69. */
  70. typedef struct
  71. {
  72. uint32_t nbOfSupportVectors; /**< Number of support vectors */
  73. uint32_t vectorDimension; /**< Dimension of vector space */
  74. float32_t intercept; /**< Intercept */
  75. const float32_t *dualCoefficients; /**< Dual coefficients */
  76. const float32_t *supportVectors; /**< Support vectors */
  77. const int32_t *classes; /**< The two SVM classes */
  78. } arm_svm_linear_instance_f32;
  79. /**
  80. * @brief Instance structure for polynomial SVM prediction function.
  81. */
  82. typedef struct
  83. {
  84. uint32_t nbOfSupportVectors; /**< Number of support vectors */
  85. uint32_t vectorDimension; /**< Dimension of vector space */
  86. float32_t intercept; /**< Intercept */
  87. const float32_t *dualCoefficients; /**< Dual coefficients */
  88. const float32_t *supportVectors; /**< Support vectors */
  89. const int32_t *classes; /**< The two SVM classes */
  90. int32_t degree; /**< Polynomial degree */
  91. float32_t coef0; /**< Polynomial constant */
  92. float32_t gamma; /**< Gamma factor */
  93. } arm_svm_polynomial_instance_f32;
  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. float32_t intercept; /**< Intercept */
  102. const float32_t *dualCoefficients; /**< Dual coefficients */
  103. const float32_t *supportVectors; /**< Support vectors */
  104. const int32_t *classes; /**< The two SVM classes */
  105. float32_t gamma; /**< Gamma factor */
  106. } arm_svm_rbf_instance_f32;
  107. /**
  108. * @brief Instance structure for sigmoid SVM prediction function.
  109. */
  110. typedef struct
  111. {
  112. uint32_t nbOfSupportVectors; /**< Number of support vectors */
  113. uint32_t vectorDimension; /**< Dimension of vector space */
  114. float32_t intercept; /**< Intercept */
  115. const float32_t *dualCoefficients; /**< Dual coefficients */
  116. const float32_t *supportVectors; /**< Support vectors */
  117. const int32_t *classes; /**< The two SVM classes */
  118. float32_t coef0; /**< Independent constant */
  119. float32_t gamma; /**< Gamma factor */
  120. } arm_svm_sigmoid_instance_f32;
  121. /**
  122. * @brief SVM linear instance init function
  123. * @param[in] S Parameters for SVM functions
  124. * @param[in] nbOfSupportVectors Number of support vectors
  125. * @param[in] vectorDimension Dimension of vector space
  126. * @param[in] intercept Intercept
  127. * @param[in] dualCoefficients Array of dual coefficients
  128. * @param[in] supportVectors Array of support vectors
  129. * @param[in] classes Array of 2 classes ID
  130. */
  131. void arm_svm_linear_init_f32(arm_svm_linear_instance_f32 *S,
  132. uint32_t nbOfSupportVectors,
  133. uint32_t vectorDimension,
  134. float32_t intercept,
  135. const float32_t *dualCoefficients,
  136. const float32_t *supportVectors,
  137. const int32_t *classes);
  138. /**
  139. * @brief SVM linear prediction
  140. * @param[in] S Pointer to an instance of the linear SVM structure.
  141. * @param[in] in Pointer to input vector
  142. * @param[out] pResult Decision value
  143. */
  144. void arm_svm_linear_predict_f32(const arm_svm_linear_instance_f32 *S,
  145. const float32_t * in,
  146. int32_t * pResult);
  147. /**
  148. * @brief SVM polynomial instance init function
  149. * @param[in] S points to an instance of the polynomial SVM structure.
  150. * @param[in] nbOfSupportVectors Number of support vectors
  151. * @param[in] vectorDimension Dimension of vector space
  152. * @param[in] intercept Intercept
  153. * @param[in] dualCoefficients Array of dual coefficients
  154. * @param[in] supportVectors Array of support vectors
  155. * @param[in] classes Array of 2 classes ID
  156. * @param[in] degree Polynomial degree
  157. * @param[in] coef0 coeff0 (scikit-learn terminology)
  158. * @param[in] gamma gamma (scikit-learn terminology)
  159. */
  160. void arm_svm_polynomial_init_f32(arm_svm_polynomial_instance_f32 *S,
  161. uint32_t nbOfSupportVectors,
  162. uint32_t vectorDimension,
  163. float32_t intercept,
  164. const float32_t *dualCoefficients,
  165. const float32_t *supportVectors,
  166. const int32_t *classes,
  167. int32_t degree,
  168. float32_t coef0,
  169. float32_t gamma
  170. );
  171. /**
  172. * @brief SVM polynomial prediction
  173. * @param[in] S Pointer to an instance of the polynomial SVM structure.
  174. * @param[in] in Pointer to input vector
  175. * @param[out] pResult Decision value
  176. */
  177. void arm_svm_polynomial_predict_f32(const arm_svm_polynomial_instance_f32 *S,
  178. const float32_t * in,
  179. int32_t * pResult);
  180. /**
  181. * @brief SVM radial basis function instance init function
  182. * @param[in] S points to an instance of the polynomial SVM structure.
  183. * @param[in] nbOfSupportVectors Number of support vectors
  184. * @param[in] vectorDimension Dimension of vector space
  185. * @param[in] intercept Intercept
  186. * @param[in] dualCoefficients Array of dual coefficients
  187. * @param[in] supportVectors Array of support vectors
  188. * @param[in] classes Array of 2 classes ID
  189. * @param[in] gamma gamma (scikit-learn terminology)
  190. */
  191. void arm_svm_rbf_init_f32(arm_svm_rbf_instance_f32 *S,
  192. uint32_t nbOfSupportVectors,
  193. uint32_t vectorDimension,
  194. float32_t intercept,
  195. const float32_t *dualCoefficients,
  196. const float32_t *supportVectors,
  197. const int32_t *classes,
  198. float32_t gamma
  199. );
  200. /**
  201. * @brief SVM rbf prediction
  202. * @param[in] S Pointer to an instance of the rbf SVM structure.
  203. * @param[in] in Pointer to input vector
  204. * @param[out] pResult decision value
  205. */
  206. void arm_svm_rbf_predict_f32(const arm_svm_rbf_instance_f32 *S,
  207. const float32_t * in,
  208. int32_t * pResult);
  209. /**
  210. * @brief SVM sigmoid instance init function
  211. * @param[in] S points to an instance of the rbf SVM structure.
  212. * @param[in] nbOfSupportVectors Number of support vectors
  213. * @param[in] vectorDimension Dimension of vector space
  214. * @param[in] intercept Intercept
  215. * @param[in] dualCoefficients Array of dual coefficients
  216. * @param[in] supportVectors Array of support vectors
  217. * @param[in] classes Array of 2 classes ID
  218. * @param[in] coef0 coeff0 (scikit-learn terminology)
  219. * @param[in] gamma gamma (scikit-learn terminology)
  220. */
  221. void arm_svm_sigmoid_init_f32(arm_svm_sigmoid_instance_f32 *S,
  222. uint32_t nbOfSupportVectors,
  223. uint32_t vectorDimension,
  224. float32_t intercept,
  225. const float32_t *dualCoefficients,
  226. const float32_t *supportVectors,
  227. const int32_t *classes,
  228. float32_t coef0,
  229. float32_t gamma
  230. );
  231. /**
  232. * @brief SVM sigmoid prediction
  233. * @param[in] S Pointer to an instance of the rbf SVM structure.
  234. * @param[in] in Pointer to input vector
  235. * @param[out] pResult Decision value
  236. */
  237. void arm_svm_sigmoid_predict_f32(const arm_svm_sigmoid_instance_f32 *S,
  238. const float32_t * in,
  239. int32_t * pResult);
  240. #ifdef __cplusplus
  241. }
  242. #endif
  243. #endif /* ifndef _SVM_FUNCTIONS_H_ */