Template function for invoking System Object MATLAB Coder

2 次查看(过去 30 天)
I'd like to make a C++ library object pretty much straight from an existing DSP System Toolbox object (specifically dsp.FIRDecimator). I understand that MATLAB Coder needs a function entry point, and you can use isempty() to construct the object as a persistent variable on the first call. Is there a good template function pattern that takes all the nominal keyword/data pairs as an optional second argument to then pass into the constructor? That way, the caller can provide all the desired customization on the invocation (I'd probably rip away the wrapper and make those steps part of an init() method anyway), and then followup calls can just go straight to the main method. Otherwise, I guess I can hand-build it as a structure. I wish Coder could let you bypass that and just give you the straight class API.

回答(1 个)

Jack
Jack 2023-3-29
Hi,
Yes, you can create a C++ library object from an existing DSP System Toolbox object by using the MATLAB Coder feature.
One way to do this is to create a C++ class that wraps the DSP System Toolbox object. You can create a constructor for the class that takes in the nominal keyword/data pairs as an optional second argument. The constructor can then use these arguments to create the DSP System Toolbox object with the desired customizations.
Here's an example code snippet to get you started:
#include "dsp.FIRDecimator.hpp" // include the generated MATLAB Coder header file
#include <vector>
class MyFIRDecimator {
public:
MyFIRDecimator(double decimationFactor, std::vector<double> coeffs) {
// use the nominal keyword/data pairs to customize the dsp.FIRDecimator object
dsp::firdecimator<double> tmpDecimator("DecimationFactor", decimationFactor, "Coefficients", coeffs);
decimator_ = tmpDecimator;
}
std::vector<double> filter(std::vector<double> input) {
// use the filter method of the dsp.FIRDecimator object to filter the input
return decimator_.filter(input);
}
private:
dsp::firdecimator<double> decimator_;
};
In this example, MyFIRDecimator is a C++ class that wraps the dsp.FIRDecimator object. The constructor of the class takes in the decimationFactor and coeffs as arguments to customize the dsp.FIRDecimator object. The filter method of the class calls the filter method of the dsp.FIRDecimator object to filter the input.
You can then use this C++ class in your code to create an instance of the MyFIRDecimator class and call the filter method on it:
MyFIRDecimator myDecimator(2.0, {1.0, 2.0, 3.0}); // create an instance of the MyFIRDecimator class
std::vector<double> input = {1.0, 2.0, 3.0, 4.0, 5.0}; // create an input vector
std::vector<double> output = myDecimator.filter(input); // filter the input using the MyFIRDecimator object
This is just a basic example to get you started. You can customize the MyFIRDecimator class further to meet your specific requirements.
  2 个评论
Martin Ryba
Martin Ryba 2023-3-29
OK, so to make MATLAB Coder happy, do you create a "shell" function call and just don't use any of that bit of the generated code? I'm thinking that's the path.
Martin Ryba
Martin Ryba 2023-4-3
编辑:Martin Ryba 2023-4-3
Well, unless someone has an example, my experimentation with the Coder shows that you are restricted to creating a specific instance of the desired object, with all the nontunable construction constants hard coded into the produced code. Specifically, arguments into the constructor have to be of class coder.Constant (or declared as constants in the GUI) or Coder will throw an error. The resulting code for the decimator for instance hard-coded in the decimation factor, filter, and input vector lengths.
Your example above seems to contradict this, so what's the magic to create a generic dsp::firdecimator object?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 C Code Generation 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by