Unable to generate C code for Gaussian Process Regression Model
6 views (last 30 days)
Show older comments
Hi Everyone
I am working on Battery State of Charge (SOC) Estimation using Gaussian Process Regression. I have successfully designed and optimized my model that works well according to my needs.
The Model Structure can be roughly described as:
Input: 6000x2 Array named (InputArray)
Column1 = capacity
Column 2 = voltage
Output: 6000 x 1 Array named (OutputArray)
Column1 = SOC
The GPR Model takes battery capacity and voltage as input and predicts the SOC as output. So far, the model provides high accuracy and I am able to use the predict function to observe and plot the responses.
My next task is to export the C code for this model, as I want to implement it on Texas Instrument CC1350 LaunchPad boards for my Wireless Sensor Network research. Here are the steps that I took to move towards the C/C++ Code Generation tool
- Saved the model using saveLearnerForCoder - Success
saveLearnerForCoder(trainedGPR.RegressionGP,'GPRout'
- Created The entry point function to load this model - Success
function label = entrypointGPR(X) %#codegen
Mdl = loadLearnerForCoder('GPRout');
label = predict(Mdl,X);
end
- codegen for MEX file - Error
testX = InputArray;
codegen entrypointGPR -args {testX} -report
This is where I am unable to figure out what to do next. I read the documentation on codegen, code builder and after many weeks I am still unable to figure out the next step. I am aware that perhaps I will not be able to use the code for my intended hardware (that's the worry for future project), but, I want to understand the complete code generation workflow. Ideally, if I am able to generate a desktop executable, I can still try to convert that C code for my intended hardware.
I would really appreciate if anyone could please point me in the right direction.
Bests
0 Comments
Answers (1)
Infinite_king
on 18 Apr 2024
Hi Omer Ali,
Here are a few basic things to know about the codegen command
"-args" option
Unlike MATLAB, C++ is a typed language, so the data type of the input should be determined before generating the code. For example, consider the following function
function res = add(x,y)
res = x+y;
end
the function 'add' can be called with multiple data types.
add(double(2),double(3))
add(single(2),single(3))
However, while generating code the input data type should be fixed. To achieve this, the '-args' option should be used as follows,
% define the inputs
x = double(5);
y = double(6);
codegen function_name -args {x,y}
% note that the values in x and y doesn't matter, only their types are used
% to determine the input types
"-config" option
The MATLAB function can be converted to 'mex', static or dynamic library and an executable. To specify the resultant format and to customize few code generation setting "-config" option can be used as follows
cfg = coder.config('mex'); % for mex
cfg = coder.config('lib'); % for static library
cfg = coder.config('dll'); % for dynamic library
codegen function_name -args {arg1,arg2,arg3..} -config cfg
However, to generate an executable, a 'main' function is needed to serve as the entry point of the program. The config object can be configured to generate a sample main file,
cfg = coder.config('exe'); % for executable
cfg.GenerateExampleMain = true;
codegen function_name -args {arg1,arg2,arg3..} -config cfg
For more information please refer the following resources,
Generating Standalone C/C++ Executables from MATLAB Code - https://www.mathworks.com/help/coder/ug/standalone-c-c-executables-from-matlab-code.html
'codegen' command - https://www.mathworks.com/help/coder/ref/codegen.html
Hope this is helpful.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!