svm_functions.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. */
  57. __STATIC_INLINE float32_t arm_exponent_f32(float32_t x, int32_t nb)
  58. {
  59. float32_t r = x;
  60. nb --;
  61. while(nb > 0)
  62. {
  63. r = r * x;
  64. nb--;
  65. }
  66. return(r);
  67. }
  68. /**
  69. * @brief Instance structure for linear SVM prediction function.
  70. */
  71. typedef struct
  72. {
  73. uint32_t nbOfSupportVectors; /**< Number of support vectors */
  74. uint32_t vectorDimension; /**< Dimension of vector space */
  75. float32_t intercept; /**< Intercept */
  76. const float32_t *dualCoefficients; /**< Dual coefficients */
  77. const float32_t *supportVectors; /**< Support vectors */
  78. const int32_t *classes; /**< The two SVM classes */
  79. } arm_svm_linear_instance_f32;
  80. /**
  81. * @brief Instance structure for polynomial 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. float32_t intercept; /**< Intercept */
  88. const float32_t *dualCoefficients; /**< Dual coefficients */
  89. const float32_t *supportVectors; /**< Support vectors */
  90. const int32_t *classes; /**< The two SVM classes */
  91. int32_t degree; /**< Polynomial degree */
  92. float32_t coef0; /**< Polynomial constant */
  93. float32_t gamma; /**< Gamma factor */
  94. } arm_svm_polynomial_instance_f32;
  95. /**
  96. * @brief Instance structure for rbf 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. float32_t gamma; /**< Gamma factor */
  107. } arm_svm_rbf_instance_f32;
  108. /**
  109. * @brief Instance structure for sigmoid 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 coef0; /**< Independent constant */
  120. float32_t gamma; /**< Gamma factor */
  121. } arm_svm_sigmoid_instance_f32;
  122. /**
  123. * @brief SVM linear instance init function
  124. * @param[in] S Parameters for SVM functions
  125. * @param[in] nbOfSupportVectors Number of support vectors
  126. * @param[in] vectorDimension Dimension of vector space
  127. * @param[in] intercept Intercept
  128. * @param[in] dualCoefficients Array of dual coefficients
  129. * @param[in] supportVectors Array of support vectors
  130. * @param[in] classes Array of 2 classes ID
  131. * @return none.
  132. *
  133. */
  134. void arm_svm_linear_init_f32(arm_svm_linear_instance_f32 *S,
  135. uint32_t nbOfSupportVectors,
  136. uint32_t vectorDimension,
  137. float32_t intercept,
  138. const float32_t *dualCoefficients,
  139. const float32_t *supportVectors,
  140. const int32_t *classes);
  141. /**
  142. * @brief SVM linear prediction
  143. * @param[in] S Pointer to an instance of the linear SVM structure.
  144. * @param[in] in Pointer to input vector
  145. * @param[out] pResult Decision value
  146. * @return none.
  147. *
  148. */
  149. void arm_svm_linear_predict_f32(const arm_svm_linear_instance_f32 *S,
  150. const float32_t * in,
  151. int32_t * pResult);
  152. /**
  153. * @brief SVM polynomial instance init function
  154. * @param[in] S points to an instance of the polynomial SVM structure.
  155. * @param[in] nbOfSupportVectors Number of support vectors
  156. * @param[in] vectorDimension Dimension of vector space
  157. * @param[in] intercept Intercept
  158. * @param[in] dualCoefficients Array of dual coefficients
  159. * @param[in] supportVectors Array of support vectors
  160. * @param[in] classes Array of 2 classes ID
  161. * @param[in] degree Polynomial degree
  162. * @param[in] coef0 coeff0 (scikit-learn terminology)
  163. * @param[in] gamma gamma (scikit-learn terminology)
  164. * @return none.
  165. *
  166. */
  167. void arm_svm_polynomial_init_f32(arm_svm_polynomial_instance_f32 *S,
  168. uint32_t nbOfSupportVectors,
  169. uint32_t vectorDimension,
  170. float32_t intercept,
  171. const float32_t *dualCoefficients,
  172. const float32_t *supportVectors,
  173. const int32_t *classes,
  174. int32_t degree,
  175. float32_t coef0,
  176. float32_t gamma
  177. );
  178. /**
  179. * @brief SVM polynomial prediction
  180. * @param[in] S Pointer to an instance of the polynomial SVM structure.
  181. * @param[in] in Pointer to input vector
  182. * @param[out] pResult Decision value
  183. * @return none.
  184. *
  185. */
  186. void arm_svm_polynomial_predict_f32(const arm_svm_polynomial_instance_f32 *S,
  187. const float32_t * in,
  188. int32_t * pResult);
  189. /**
  190. * @brief SVM radial basis function instance init function
  191. * @param[in] S points to an instance of the polynomial SVM structure.
  192. * @param[in] nbOfSupportVectors Number of support vectors
  193. * @param[in] vectorDimension Dimension of vector space
  194. * @param[in] intercept Intercept
  195. * @param[in] dualCoefficients Array of dual coefficients
  196. * @param[in] supportVectors Array of support vectors
  197. * @param[in] classes Array of 2 classes ID
  198. * @param[in] gamma gamma (scikit-learn terminology)
  199. * @return none.
  200. *
  201. */
  202. void arm_svm_rbf_init_f32(arm_svm_rbf_instance_f32 *S,
  203. uint32_t nbOfSupportVectors,
  204. uint32_t vectorDimension,
  205. float32_t intercept,
  206. const float32_t *dualCoefficients,
  207. const float32_t *supportVectors,
  208. const int32_t *classes,
  209. float32_t gamma
  210. );
  211. /**
  212. * @brief SVM rbf prediction
  213. * @param[in] S Pointer to an instance of the rbf SVM structure.
  214. * @param[in] in Pointer to input vector
  215. * @param[out] pResult decision value
  216. * @return none.
  217. *
  218. */
  219. void arm_svm_rbf_predict_f32(const arm_svm_rbf_instance_f32 *S,
  220. const float32_t * in,
  221. int32_t * pResult);
  222. /**
  223. * @brief SVM sigmoid instance init function
  224. * @param[in] S points to an instance of the rbf SVM structure.
  225. * @param[in] nbOfSupportVectors Number of support vectors
  226. * @param[in] vectorDimension Dimension of vector space
  227. * @param[in] intercept Intercept
  228. * @param[in] dualCoefficients Array of dual coefficients
  229. * @param[in] supportVectors Array of support vectors
  230. * @param[in] classes Array of 2 classes ID
  231. * @param[in] coef0 coeff0 (scikit-learn terminology)
  232. * @param[in] gamma gamma (scikit-learn terminology)
  233. * @return none.
  234. *
  235. */
  236. void arm_svm_sigmoid_init_f32(arm_svm_sigmoid_instance_f32 *S,
  237. uint32_t nbOfSupportVectors,
  238. uint32_t vectorDimension,
  239. float32_t intercept,
  240. const float32_t *dualCoefficients,
  241. const float32_t *supportVectors,
  242. const int32_t *classes,
  243. float32_t coef0,
  244. float32_t gamma
  245. );
  246. /**
  247. * @brief SVM sigmoid prediction
  248. * @param[in] S Pointer to an instance of the rbf SVM structure.
  249. * @param[in] in Pointer to input vector
  250. * @param[out] pResult Decision value
  251. * @return none.
  252. *
  253. */
  254. void arm_svm_sigmoid_predict_f32(const arm_svm_sigmoid_instance_f32 *S,
  255. const float32_t * in,
  256. int32_t * pResult);
  257. #ifdef __cplusplus
  258. }
  259. #endif
  260. #endif /* ifndef _SVM_FUNCTIONS_H_ */