Template function for invoking System Object MATLAB Coder

4 views (last 30 days)
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.

Answers (1)

Jack on 29 Mar 2023
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 {
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);
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.
Martin Ryba
Martin Ryba on 3 Apr 2023
Edited: Martin Ryba on 3 Apr 2023
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?

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!