Template function for invoking System Object MATLAB Coder
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Jack
on 29 Mar 2023
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 Comments
See Also
Categories
Find more on C Code Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!