Include libsvm in simulink, s function
2 views (last 30 days)
I'm wondering how complicated it would be to call libsvm [ http://www.csie.ntu.edu.tw/~cjlin/libsvm/ ] from a simulink model. In addition it would be nice, if code generation would be supported.
In the libsvm package there already is the source code for the purpose of generating a mex file that can be used in matlab. This works:
mex svmpredict.c ../svm.cpp svm_model_matlab.c
the function then is called svmpredict:
Usage: [predicted_label, accuracy, decision_values/prob_estimates] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
[predicted_label] = svmpredict(testing_label_vector, testing_instance_matrix, model, 'libsvm_options')
model: SVM model structure from svmtrain.
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
-q : quiet mode (no outputs)
predicted_label: SVM prediction output vector.
accuracy: a vector with accuracy, mean squared error, squared correlation coefficient.
prob_estimates: If selected, probability estimate vector.
svm_model_matlab.c is a wrapper to transform the
The file svm_model_matlab contains only a wrapper to transform the 'model' (the svm itself) to a matlab struct.
svmpredict.c has the mexFunction(...) entry point to call the function:
void mexFunction( int nlhs, mxArray *plhs,
int nrhs, const mxArray *prhs )
My big question is: How to include these files in simulink?
- embedded matlab function: does not work since it only supports matlab scripts, not mex files
- interpreted matlab function: does not work, only 1dim, and i didn't find a way to input parameters (ie the svm model, that has been trained before). Additionally it does not support code generation afaik
- s-function: this is quite more complicated... I thought, I could easily use the source code for the mex generation, but the mechanisms are different, right? I used the s function template (matlabroot/simulink/src/sfuntmpl_basic.c) and set the ports width etc. But I don't know how to combine the
static void mdlOutputs(SimStruct *S, int_T tid)
function (which seems to be called each time the model updates?) with the mexFunction from the svmpredict.c file. If I try to compile everything (svmpredictSimulink.c is the s function) this happens:
>>mex svmpredictSimulink.c svmpredict.c ../svm.cpp svm_model_matlab.c
svmpredict.obj : error LNK2005: mexFunction already defined in svmpredictSimulink.obj
So there seems to be an additional mexFunction defined implicitly, because the function is not present in the svmpredictSimulink.c file. Is it created in the background? Where? The file consists only of the template, no output calculations or so. Only the definition of the parameters and portnumbers and -widths is done.
Is the way to create a S-function the way I should go, or is there any other? I've seen the legacy code tool, but it seems to be for easier functions with direct return values and so?
There is also a raw c file to create the prediction (with main.c etc), but I thought if it's already ported to matlab, it should be simpler to start there?
Thank you very much, Jan
Friedrich on 5 May 2014
Edited: Friedrich on 5 May 2014
you can use LIBSVM in a embedded matlab function block but you need to declare those functions as extrinsic (See coder.extrinsic function, or in the case you use on older MATLAB see eml.extrinsic). However doing this, you cannot generate Code out of your Simulink model but you can still simulate it. So e.g.