How to write C code generation for ' not available function for standalone generation' ?

Hi, i can not transfert my matlab algorithme. When I use Matlab Coder to convert it into C code, it build failed with the following error:
The function 'designfilt' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation.
Then i think i need to write a bandpass filter in C code by myself using DFT and inverse fourier transformation, but i really don't know how to write it. May somebody help me ?

2 Comments

Please do not use # characters in the tags. This is not twitter.
What kind of bandpass filter do you need? As long, as you do not explain this, it is hard to suggest a solution. A full replacement of the designfilt command would be an overkill.
Hi Jan, sorry i am new here. I use two different kind of filter, one 'designfilter' and the other 'Least Square' is reported just over there ))
function [td_filtrage]= LeastSquare(Datad,Fs)
N = 40; % Order
Fstop1 = 0.7; % First Stopband Frequency
Fpass1 = 0.9; % First Passband Frequency
Fpass2 = 1.4; % Second Passband Frequency
Fstop2 = 1.5; % Second Stopband Frequency
Wstop1 = 1; % First Stopband Weight
Wpass = 1; % Passband Weight
Wstop2 = 1; % Second Stopband Weight
% Calculate the coefficients using the FIRLS function.
b = firls(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 Fs/2]/(Fs/2), [0 0 1 1 0 ...
0], [Wstop1 Wpass Wstop2]);
Hd = dfilt.dffir(b);
td_filtrage = filter(Hd,Datad)
Then when i try to translate in C code, the software send me: Error 'dfilt.dffir' is not supported for code generation.
Thank you to help me !

Sign in to comment.

 Accepted Answer

Do you need to change your filter over the simulation, if not, then the coder.extrinsic approach, as outlined in the error message, would be a good way to go. Essentially you can use MATLAB to compute the coefficients and then perform the filtering.
HTH

8 Comments

Thank you for your answer )) I don't need other filter, i had try design.filter too. Both of them works for my project. I need to occurate, i am student and i didn't learn how to use it. So how to use 'code.extrinsic' or calculate coefficient of filter ?
function [td_filtrage]= LeastSquare(Datad,Fs)
N = 40; % Order
Fstop1 = 0.7; % First Stopband Frequency
Fpass1 = 0.9; % First Passband Frequency
Fpass2 = 1.4; % Second Passband Frequency
Fstop2 = 1.5; % Second Stopband Frequency
Wstop1 = 1; % First Stopband Weight
Wpass = 1; % Passband Weight
Wstop2 = 1; % Second Stopband Weight
% Calculate the coefficients using the FIRLS function.
b = firls(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 Fs/2]/(Fs/2), [0 0 1 1 0 ...
0], [Wstop1 Wpass Wstop2]);
Hd = dfilt.dffir(b);
td_filtrage = filter(Hd,Data)
Error 'dfilt.dffir' is not supported for code generation
You are using firls, it actually supports codegen. So I think you just need to make a very simple change. Instead of forming Hd, just use b directly. Like this
function [td_filtrage]= LeastSquare(Datad,Fs)
N = 40; % Order
Fstop1 = 0.7; % First Stopband Frequency
Fpass1 = 0.9; % First Passband Frequency
Fpass2 = 1.4; % Second Passband Frequency
Fstop2 = 1.5; % Second Stopband Frequency
Wstop1 = 1; % First Stopband Weight
Wpass = 1; % Passband Weight
Wstop2 = 1; % Second Stopband Weight
% Calculate the coefficients using the FIRLS function.
b = firls(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 Fs/2]/(Fs/2), [0 0 1 1 0 ...
0], [Wstop1 Wpass Wstop2]);
td_filtrage = filter(b,1,Data);
Now to codegen, you need to ensure that you pass in a constant Fs. You can achieve that using coder.Constant
codegen LeastSquare.m -args {ones(1024,1),coder.Constant(100)}
Once the mex file is generated, you can compare the results as
x = randn(1024,1);
y = LeastSquare(x,100);
yc = LeastSquare_mex(x,100);
max(abs(y-yc))
HTH
Ok fine i will do it. because i did some change lately and the same message appears saying the designfilt must be eliminated for stand alone code. Then software says fix the error by not using designfilt or by ensuring that its outputs are unused. =)
But nevermind . Thank you!!
I did the thing. You were right about input must be constant. Because its saying 'All LeastSquare output must be constant'. Then i dont know where and how to use the code to fix it.
codegen LeastSquare.m -args {ones(1024,1),coder.Constant(100)}
Same for the last instruction that you sent me.
How are you calling the function, do you change Fs every time you call it? If not, it shouldn't be an issue. If yes, then could you elaborate a bit more what you want to do? For example, it seems that you are designing the filter and then filter it once. I would imagine most of time you can design the filter once and then filter the signal many times?
Yes i call my filter function once in my main and then i execute it for my heart signal. That's why i use a bandpass around 1Hz, in order to obtain the pulse of heart ;). Concerning Fs, its always the same 37Hz. I will change if it necessary.. All i want it translate my program (Main + functions) in C code. And there is my call part in main program.
td_filtrage = LeastSquare(Datad,Fs);
When i execute for the code, i receive the following
I fix the problem about the constant Fs, I put this variable insade the LeastSquare function.
function [td_filtrage]= LeastSquare(Datad)
Fs = 37;
N = 40; % Order
Fstop1 = 0.7; % First Stopband Frequency
Fpass1 = 0.9; % First Passband Frequency
Fpass2 = 1.4; % Second Passband Frequency
Fstop2 = 1.5; % Second Stopband Frequency
Wstop1 = 1; % First Stopband Weight
Wpass = 1; % Passband Weight
Wstop2 = 1; % Second Stopband Weight
% Calculate the coefficients using the FIRLS function.
b = firls(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 Fs/2]/(Fs/2), [0 0 1 1 0 ...
0], [Wstop1 Wpass Wstop2]);
td_filtrage = filter(b,1,Datad);
Then i change the call code in the Main
td_filtrage = LeastSquare(Datad);
I received my function in C code, i think its working. I need to put the code in my microprocesor . Thank you for everything !!
i am facing the same issue but instead of bandpass filter i am using low pass filter. how to design the lowpass filter using firls syntax ?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!