mirror of
https://github.com/Fishwaldo/bl_mcu_sdk.git
synced 2025-07-14 16:58:50 +00:00
116 lines
3.3 KiB
C
116 lines
3.3 KiB
C
#include "riscv_math.h"
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include "../common.h"
|
|
|
|
#include "../HelperFunctions/math_helper.c"
|
|
#include "../HelperFunctions/ref_helper.c"
|
|
|
|
#include <stdio.h>
|
|
#define DELTAF32 (0.05f)
|
|
#define DELTAQ31 (63)
|
|
#define DELTAQ15 (1)
|
|
#define DELTAQ7 (1)
|
|
|
|
int test_flag_error = 0;
|
|
|
|
/*
|
|
The rbf SVM instance containing all parameters.
|
|
Those parameters can be generated with the python library scikit-learn.
|
|
*/
|
|
riscv_svm_rbf_instance_f32 params;
|
|
|
|
/*
|
|
Parameters generated by a training of the SVM classifier
|
|
using scikit-learn and some random input data.
|
|
*/
|
|
#define NB_SUPPORT_VECTORS 12
|
|
|
|
/*
|
|
Dimension of the vector space. A vector is your feature.
|
|
It could, for instance, be the pixels of a picture or the FFT of a signal.
|
|
*/
|
|
#define VECTOR_DIMENSION 2
|
|
|
|
const float32_t dualCoefficients[NB_SUPPORT_VECTORS]={-1.0f,-0.24755389f,-0.25096047f,-1.0f,0.46499702f,0.0444816f,0.2506171f,0.33291225f,0.40335676f,0.39688342f,0.41563006f,0.18963614f}; /* Dual coefficients */
|
|
|
|
const float32_t supportVectors[NB_SUPPORT_VECTORS*VECTOR_DIMENSION]={0.63477121f,-0.59986042f,-0.12523598f,0.63629978f,0.06312245f,-0.59010787f,-0.45084347f,0.5881345f,2.95843437f,-0.42401806f,-0.59248704f,-2.96471627f,-2.15141529f,-1.92858494f,0.55448567f,3.17860023f,-1.1236758f,2.7331152f,-0.05251905f,-3.00311314f,-2.86027661f,0.78232224f,-2.77279855f,-1.32510909f}; /* Support vectors */
|
|
|
|
/*
|
|
Class A is identified with value 0.
|
|
Class B is identified with value 1.
|
|
|
|
This array is used by the SVM functions to do a conversion and ease the comparison
|
|
with the Python code where different values could be used.
|
|
*/
|
|
const int32_t classes[2]={0,1};
|
|
|
|
int main()
|
|
{
|
|
int i;
|
|
BENCH_INIT;
|
|
/* Array of input data */
|
|
float32_t in[VECTOR_DIMENSION];
|
|
|
|
/* Result of the classifier */
|
|
int32_t result;
|
|
|
|
|
|
/*
|
|
Initialization of the SVM instance parameters.
|
|
Additional parameters (intercept, degree, coef0 and gamma) are also coming from Python.
|
|
*/
|
|
riscv_svm_rbf_init_f32(¶ms,
|
|
NB_SUPPORT_VECTORS,
|
|
VECTOR_DIMENSION,
|
|
0.607375f, /* Intercept */
|
|
dualCoefficients,
|
|
supportVectors,
|
|
classes,
|
|
0.500000f /* Gamma */
|
|
);
|
|
|
|
|
|
/*
|
|
Input data.
|
|
It is corresponding to a point inside the first class.
|
|
*/
|
|
in[0] = 0.4f;
|
|
in[1] = 0.1f;
|
|
BENCH_START(riscv_svm_rbf_predict_f32);
|
|
riscv_svm_rbf_predict_f32(¶ms, in, &result);
|
|
BENCH_END(riscv_svm_rbf_predict_f32);
|
|
/* Result should be 0 : First class */
|
|
if (result != 0) {
|
|
BENCH_ERROR(riscv_svm_rbf_predict_f32);
|
|
printf("expect: %d, actual: %d\n", 0, result);
|
|
test_flag_error = 1;
|
|
}
|
|
BENCH_STATUS(riscv_svm_rbf_predict_f32);
|
|
|
|
/*
|
|
This input vector is corresponding to a point inside the second class.
|
|
*/
|
|
in[0] = 3.0f;
|
|
in[1] = 0.0f;
|
|
|
|
riscv_svm_rbf_predict_f32(¶ms, in, &result);
|
|
|
|
/* Result should be 0 : First class */
|
|
if (result != 1) {
|
|
BENCH_ERROR(riscv_svm_rbf_predict_f32);
|
|
printf("expect: %d, actual: %d\n", 1, result);
|
|
test_flag_error = 1;
|
|
}
|
|
BENCH_STATUS(riscv_svm_rbf_predict_f32);
|
|
|
|
|
|
BENCH_FINISH;
|
|
if (test_flag_error) {
|
|
printf("test error apprears, please recheck.\n");
|
|
return 1;
|
|
} else {
|
|
printf("all test are passed. Well done!\n");
|
|
}
|
|
return 0;
|
|
};
|