0

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 enter image description here

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks in advance, for the given code, could you help me to show me just one example which will work? thanks in advanmce
let me write simple function then
coefficients are result of designing filter? i mean those design of filter should be outsize of function?
Yes, design should be outside function that is generating code. Design is not supported by code generation.
what about this code codegen mcadd -args {0,coder.Constant(Hdl.Numerator)}?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.