简体   繁体   中英

generating c++ code from matlab

let us suppose we have following code

function Hdl=mcadd()
%#codegen
Fc=0.4;
N=100; % FIR filter order
Hf=fdesign.lowpass('N,Fc',N,Fc);
Hdl=design(Hf,'window','window',@hamming,'SystemObject',true);
end

i would like designing of lowpass filter from matlab code transform intro equivalent c++ code, for this i have tried following command

codegen -args {Z} -report mcadd.m

i have took this command from the following command 在此处输入图片说明

but i got following error

codegen -args {Z} -report mcadd.m
Failed to evaluate '{Z}' to non-empty array in the base workspace.
Use help codegen for more information on using this command.
Error using codegen (line 146)

here is working code

function c=add_numbers(a,b)
%#codegen
c=a+b;
disp(c)
end

result

>> a=5;
>> b=4;
>> codegen -args {a,b} -report add_numbers.m

Code generation successful: View report

There are multiple problems with what you are trying. First the option -args to codegen command is needed only when there is an input to your function. Since you do not have an input you do not need that option. If the function needs an input argument you would need to provide a valid existing MATLAB variable between {} for that argument.

You can just run codegen mcadd to try codegen for your function.

But fdesign.lowpass class does not support code generation. This will throw a code generation error saying the same. Code generation supports filtering process when you use the filter function or a dsp.FIRFilter System object. But I do not think any of the filter design process is supported for code generation. If you know your filter parameters you usually design the filter in MATLAB and use the filter coefficients in your function using a System object or filter function. You can then generate C code for this function.

A sample workflow is as below.

% Design filter in MATLAB
Fc=0.4;
N=100; % FIR filter order
Hf=fdesign.lowpass('N,Fc',N,Fc);
Hdl=design(Hf,'window','window',@hamming)

Change your function to just filter the data using the input filter coefficients.

function y=mcadd(data, coeffs)
%#codegen

persistent obj
if isempty(obj)
    obj = dsp.FIRFilter('Numerator', coeffs);
end

y = step(obj, data);

end

Compile the function as below.

codegen mcadd -args {0,coder.Constant(Hdl.Numerator)}

The above line assumes you will be sending one sample of input at a time to filter. You can change 0 in the above code to match your input size and type. After compiling you can call your function using,

mcadd_mex(0, Hdl.Numerator)

codegen command by default generates code for a mex file. If you would like to take the generated code and use it for integration into some other C code, try using lib target for codegen as below.

codegen -config:lib mcadd -args {0,coder.Constant(Hdl.Numerator)}

MATLAB Coder app will take you through this steps and makes the process easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM