| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- /******************************************************************************
- * @file svm_functions.h
- * @brief Public header file for NMSIS DSP Library
- * @version V1.10.0
- * @date 08 July 2021
- * Target Processor: RISC-V Cores
- ******************************************************************************/
- /*
- * Copyright (c) 2010-2020 Arm Limited or its affiliates. All rights reserved.
- * Copyright (c) 2019 Nuclei Limited. All rights reserved.
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the License); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- #ifndef SVM_FUNCTIONS_H_
- #define SVM_FUNCTIONS_H_
- #include "riscv_math_types.h"
- #include "riscv_math_memory.h"
- #include "dsp/none.h"
- #include "dsp/utils.h"
- #include "dsp/svm_defines.h"
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- #define STEP(x) (x) <= 0 ? 0 : 1
- /**
- * @defgroup groupSVM SVM Functions
- * This set of functions is implementing SVM classification on 2 classes.
- * The training must be done from scikit-learn. The parameters can be easily
- * generated from the scikit-learn object. Some examples are given in
- * DSP/Testing/PatternGeneration/SVM.py
- *
- * If more than 2 classes are needed, the functions in this folder
- * will have to be used, as building blocks, to do multi-class classification.
- *
- * No multi-class classification is provided in this SVM folder.
- *
- */
- /**
- * @brief Integer exponentiation
- * @param[in] x value
- * @param[in] nb integer exponent >= 1
- * @return x^nb
- */
- __STATIC_INLINE float32_t riscv_exponent_f32(float32_t x, int32_t nb)
- {
- float32_t r = x;
- nb --;
- while(nb > 0)
- {
- r = r * x;
- nb--;
- }
- return(r);
- }
- /**
- * @brief Instance structure for linear SVM prediction function.
- */
- typedef struct
- {
- uint32_t nbOfSupportVectors; /**< Number of support vectors */
- uint32_t vectorDimension; /**< Dimension of vector space */
- float32_t intercept; /**< Intercept */
- const float32_t *dualCoefficients; /**< Dual coefficients */
- const float32_t *supportVectors; /**< Support vectors */
- const int32_t *classes; /**< The two SVM classes */
- } riscv_svm_linear_instance_f32;
- /**
- * @brief Instance structure for polynomial SVM prediction function.
- */
- typedef struct
- {
- uint32_t nbOfSupportVectors; /**< Number of support vectors */
- uint32_t vectorDimension; /**< Dimension of vector space */
- float32_t intercept; /**< Intercept */
- const float32_t *dualCoefficients; /**< Dual coefficients */
- const float32_t *supportVectors; /**< Support vectors */
- const int32_t *classes; /**< The two SVM classes */
- int32_t degree; /**< Polynomial degree */
- float32_t coef0; /**< Polynomial constant */
- float32_t gamma; /**< Gamma factor */
- } riscv_svm_polynomial_instance_f32;
- /**
- * @brief Instance structure for rbf SVM prediction function.
- */
- typedef struct
- {
- uint32_t nbOfSupportVectors; /**< Number of support vectors */
- uint32_t vectorDimension; /**< Dimension of vector space */
- float32_t intercept; /**< Intercept */
- const float32_t *dualCoefficients; /**< Dual coefficients */
- const float32_t *supportVectors; /**< Support vectors */
- const int32_t *classes; /**< The two SVM classes */
- float32_t gamma; /**< Gamma factor */
- } riscv_svm_rbf_instance_f32;
- /**
- * @brief Instance structure for sigmoid SVM prediction function.
- */
- typedef struct
- {
- uint32_t nbOfSupportVectors; /**< Number of support vectors */
- uint32_t vectorDimension; /**< Dimension of vector space */
- float32_t intercept; /**< Intercept */
- const float32_t *dualCoefficients; /**< Dual coefficients */
- const float32_t *supportVectors; /**< Support vectors */
- const int32_t *classes; /**< The two SVM classes */
- float32_t coef0; /**< Independent constant */
- float32_t gamma; /**< Gamma factor */
- } riscv_svm_sigmoid_instance_f32;
- /**
- * @brief SVM linear instance init function
- * @param[in] S Parameters for SVM functions
- * @param[in] nbOfSupportVectors Number of support vectors
- * @param[in] vectorDimension Dimension of vector space
- * @param[in] intercept Intercept
- * @param[in] dualCoefficients Array of dual coefficients
- * @param[in] supportVectors Array of support vectors
- * @param[in] classes Array of 2 classes ID
- */
- void riscv_svm_linear_init_f32(riscv_svm_linear_instance_f32 *S,
- uint32_t nbOfSupportVectors,
- uint32_t vectorDimension,
- float32_t intercept,
- const float32_t *dualCoefficients,
- const float32_t *supportVectors,
- const int32_t *classes);
- /**
- * @brief SVM linear prediction
- * @param[in] S Pointer to an instance of the linear SVM structure.
- * @param[in] in Pointer to input vector
- * @param[out] pResult Decision value
- */
- void riscv_svm_linear_predict_f32(const riscv_svm_linear_instance_f32 *S,
- const float32_t * in,
- int32_t * pResult);
- /**
- * @brief SVM polynomial instance init function
- * @param[in] S points to an instance of the polynomial SVM structure.
- * @param[in] nbOfSupportVectors Number of support vectors
- * @param[in] vectorDimension Dimension of vector space
- * @param[in] intercept Intercept
- * @param[in] dualCoefficients Array of dual coefficients
- * @param[in] supportVectors Array of support vectors
- * @param[in] classes Array of 2 classes ID
- * @param[in] degree Polynomial degree
- * @param[in] coef0 coeff0 (scikit-learn terminology)
- * @param[in] gamma gamma (scikit-learn terminology)
- */
- void riscv_svm_polynomial_init_f32(riscv_svm_polynomial_instance_f32 *S,
- uint32_t nbOfSupportVectors,
- uint32_t vectorDimension,
- float32_t intercept,
- const float32_t *dualCoefficients,
- const float32_t *supportVectors,
- const int32_t *classes,
- int32_t degree,
- float32_t coef0,
- float32_t gamma
- );
- /**
- * @brief SVM polynomial prediction
- * @param[in] S Pointer to an instance of the polynomial SVM structure.
- * @param[in] in Pointer to input vector
- * @param[out] pResult Decision value
- */
- void riscv_svm_polynomial_predict_f32(const riscv_svm_polynomial_instance_f32 *S,
- const float32_t * in,
- int32_t * pResult);
- /**
- * @brief SVM radial basis function instance init function
- * @param[in] S points to an instance of the polynomial SVM structure.
- * @param[in] nbOfSupportVectors Number of support vectors
- * @param[in] vectorDimension Dimension of vector space
- * @param[in] intercept Intercept
- * @param[in] dualCoefficients Array of dual coefficients
- * @param[in] supportVectors Array of support vectors
- * @param[in] classes Array of 2 classes ID
- * @param[in] gamma gamma (scikit-learn terminology)
- */
- void riscv_svm_rbf_init_f32(riscv_svm_rbf_instance_f32 *S,
- uint32_t nbOfSupportVectors,
- uint32_t vectorDimension,
- float32_t intercept,
- const float32_t *dualCoefficients,
- const float32_t *supportVectors,
- const int32_t *classes,
- float32_t gamma
- );
- /**
- * @brief SVM rbf prediction
- * @param[in] S Pointer to an instance of the rbf SVM structure.
- * @param[in] in Pointer to input vector
- * @param[out] pResult decision value
- */
- void riscv_svm_rbf_predict_f32(const riscv_svm_rbf_instance_f32 *S,
- const float32_t * in,
- int32_t * pResult);
- /**
- * @brief SVM sigmoid instance init function
- * @param[in] S points to an instance of the rbf SVM structure.
- * @param[in] nbOfSupportVectors Number of support vectors
- * @param[in] vectorDimension Dimension of vector space
- * @param[in] intercept Intercept
- * @param[in] dualCoefficients Array of dual coefficients
- * @param[in] supportVectors Array of support vectors
- * @param[in] classes Array of 2 classes ID
- * @param[in] coef0 coeff0 (scikit-learn terminology)
- * @param[in] gamma gamma (scikit-learn terminology)
- */
- void riscv_svm_sigmoid_init_f32(riscv_svm_sigmoid_instance_f32 *S,
- uint32_t nbOfSupportVectors,
- uint32_t vectorDimension,
- float32_t intercept,
- const float32_t *dualCoefficients,
- const float32_t *supportVectors,
- const int32_t *classes,
- float32_t coef0,
- float32_t gamma
- );
- /**
- * @brief SVM sigmoid prediction
- * @param[in] S Pointer to an instance of the rbf SVM structure.
- * @param[in] in Pointer to input vector
- * @param[out] pResult Decision value
- */
- void riscv_svm_sigmoid_predict_f32(const riscv_svm_sigmoid_instance_f32 *S,
- const float32_t * in,
- int32_t * pResult);
- #ifdef __cplusplus
- }
- #endif
- #endif /* ifndef _SVM_FUNCTIONS_H_ */
|