How to convert to C-code from MATLAB code by using MATLAB CODER for case of Low-pass filter

Situation:
1) I made a low-pass filter by using Filter-Designer.
2) I exported a function with MATLAB code by using Filter-Designer.
3) I added a script for calling the function as MATLAB code .
4) I convert to a C code from the MATLAB code. Then I cannot convert to a C code.
Created MATLAB code:
Low-pass Filter:
function Hd = untitled
%UNTITLED 離散時間フィルター オブジェクトを返します。
% MATLAB Code
% Generated by MATLAB(R) 9.9 and Signal Processing Toolbox 8.5.
% Generated on: 02-Feb-2022 10:51:19
% FIR Window Lowpass filter designed using the FIR1 function.
% All frequency values are in Hz.
Fs = 1000; % Sampling Frequency
N = 20; % Order
Fc = 48; % Cutoff Frequency
flag = 'scale'; % Sampling Flag
Beta = 0.5; % Window Parameter
% Create the window vector for the design algorithm.
win = kaiser(N+1, Beta);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, Fc/(Fs/2), 'low', win, flag);
Hd = dfilt.dffir(b);
% [EOF]
Script:
Fs = 2882;
t = linspace(0,1,Fs);
y2 = filter(Hd,x);
plot(t,x,t,y2);
t = linspace(1,2882,Fs);
plot(t,x,t,y2);
How can I convert the above MATLAB code using MATLAB Coder?
Could you please tell me the method and procedure?

8 Comments

I seem to recall that the general technique is to design the filter first, and save it to a .mat file, and then create a function (not a script) that load()'s the filter from the file and uses it.
If I recall, the load() will effectively be done at compile time, and the filter effectively becoming hard-coded.
Thank you for your prompt response.
Thank you for your advice too.
First, I will try.
Do you want to generate the code for the function that generates filter coefficients ?
As their script does not show invoking the design function, I suspect they want to generate code for using the designed filter. (If I recall correctly, the design functions are not supported for code generation.)
@Walter Roberson san
Thank you for your advice. It was carried out as you said. However, I get a message that discrete and structured variables stored in the workspace in .mat are not supported.
I will contact the support center.
@Raghu Boggavarapu san
Thank you for your consideration. Yes, I do. I want to generate the code for the function that generates filter coefficients.
Then I want use filter coefficients for multiply the raw data by the filter coefficients.
As far as I can see, code cannot be generated for the filter design functions that you are using. (I did not check whether you can generate for simpler functions such as butterord)
Thank you @Walter Roberson for your kindly information.
I understood regarding you said.
I think I want to select the other function.

Sign in to comment.

 Accepted Answer

Here is the online documentation page for functinos in the Signal Processing Toolbox that can be generated to C/C++:
Your code can only use these functions if you want to generate C/C++ code using MATLAB Coder.

2 Comments

Thank you @Benjamin Thompson for your answer.
I understood that "dfilt.dffir" is out of usage for generating.
I'm thinking to change to other function.
If this answer was helpful please mark it as accepted.

Sign in to comment.

More Answers (0)

Categories

Find more on Signal Processing Toolbox 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!