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 thefilter
function or adsp.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.
Change your function to just filter the data using the input filter coefficients.
Compile the function as below.
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,
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.
MATLAB Coder app will take you through this steps and makes the process easier.