Include libsvm in simulink, s function

2 visualizzazioni (ultimi 30 giorni)
Jan Kappen
Jan Kappen il 5 Mag 2014
Risposto: Ricardo Gualavisi il 27 Giu 2016
Hello guys.
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:
>> 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')
Parameters:
model: SVM model structure from svmtrain.
libsvm_options:
-b probability_estimates: whether to predict probability estimates, 0 or 1 (default 0); one-class SVM not supported yet
-q : quiet mode (no outputs)
Returns:
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?
I tried:
  • 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

Risposte (2)

Friedrich
Friedrich il 5 Mag 2014
Modificato: Friedrich il 5 Mag 2014
Hi,
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.
coder.extrinsic('svmpredict')
  2 Commenti
Jan Kappen
Jan Kappen il 5 Mag 2014
Thank you, this could work, but as you mentioned not with code generation.
The even bigger problem is, that libsvm makes heavy use of sparse matrices. So far there is no support, right?
If I convert them via full(...), matlab crashes because of memory access violation.
Friedrich
Friedrich il 6 Mag 2014
Right, sparse matrices arent supported by simulink but they are supported in MATLAB Function Blocks. You could send a sparse matrix as (i,j,value) vectors and assemble it later on. E.g. lets say you have
>> a = sparse(eye(3))
a =
(1,1) 1
(2,2) 1
(3,3) 1
You cannot send a as signal but you could do
[i,j,val] = find(a)
and then use i,j,val as 3 signals which you can later assemble in a MATLAB function block again using sparse
>> a = sparse(i,j,val)
Or consider switching to MATLAB complety (see ode45 and friends to solve your system over time).

Accedi per commentare.


Ricardo Gualavisi
Ricardo Gualavisi il 27 Giu 2016
Have you got any solution for this problem ? It would be very helpful for me. Thanks a lot!!

Categorie

Scopri di più su Simulink Coder in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by