gfdeconv implementation in Simulink

I need to implement binary polynomial division in Simulink such as gfdeconv (in main Matlab). In Simulink i add block with MATLAB function, but gfdeconv is Undefined function in Simulink. Deconv (without gf) works, but result is incorrect.
function [q,rem] = fcn(b,a)
%#codegen
[q,rem]=gfdeconv(b,a);
it should be like thisscheme

回答(1 个)

If gfdeconv is not supported for code generation (it appears not to be as there is no "Extended Capabiliites" section on its doc page), then you should be able to declare it extrinsic:
function [q,rem] = fcn(b,a)
%#codegen
coder.extrinsic('gfdeconv')
[q,rem]=gfdeconv(b,a);
end
See coder.extrinsic for all of the details.

9 个评论

thanks, but i can't fixed it. I define the output sizes for the extrinsic call (without this was error). Now i have: Undefined function 'gfdeconv' for input arguments of type 'double'. String and double are allowed for input arguments. Trasform to string does not help.
function [q,rem] = fcn(b,a)
%#codegen
q=zeros(1,4);
rem=zeros(1,3);
%coder.extrinsic('num2str');
%a=num2str(a);
%b=num2str(b);
coder.extrinsic('gfdeconv')
[q,rem]=gfdeconv(b,a);
end
If you're getting an erro that gfdeconv is an undefined function, then there is some other problem. But you showed in the Question that you do have gfdeconv in your installation, so I don't know what's going on there.
This worked for me in Simulink:
function [q,r] = fcn(b,a)
coder.extrinsic('gfdeconv')
q = zeros(1,4);
r = zeros(1,3);
[q,r] = gfdeconv(reshape(b,1,[]),reshape(a,1,[]));
end
It looks like if the Constant blocks have the "Intepret vector parameters as 1D" checked, then b and a come into the fcn as column vectors. But they have to be row vectors for gfdeconv, hence the reshapes. You can probably uncheck those boxes in the Constant blocks, and then they'd come in as row vectors, but I didn't try that. Or you could probably go to "Edit Data" in the function editor and explicitly set the sizes of b and a.
What I don't like is that q and r have to be pre-allocated to the exactly correct size. So there will be a problem if changes to b and/or a result in a change to the size of q and/or r.
I'm not sure how to get around that problem. I don't know enough about gfdeconv to know if numel(q) and numel(r) can be determined from numel(b) and numel(a), or even if they can if Simullink would allow q and r to be allocated based on some function of numel(b) and numel(a).
Thanks, i tried but it is not successful. I known that pre-allocated size is bad, i made it to be sure of the correctness, I should probably use zeros(size(b)) for q and rem, because b has larger size. Is it possible to run model ? I don't know what is wrong. Checkbox in the Constant block didn't change anything.
Paul
Paul 2022-10-8
编辑:Paul 2022-10-8
Sorry, I can't download and run a model.
The function I posted above works for me, feeding b and a with Constant blocks and sending q and r to Display blocks.
BTW, I wouldn't use rem as a variable name; it's a built-in function (rem).
If your model is as simple in the original Question, maybe you can post a screen shot of the block diagram, the code in fcn, and a screen shot of the error message that shows up in in the Diagnostic viewer after clicking Update or Play.
My experimentation showed that q and r have to be pre-allocated to the exact size as returned from gfdeconv.
So, I write description for each picture:
Pic 1: model
Pic 2:constant block
Pic 3: after running scheme program stopped as debug with error. Before breakpoint q=[0 0 0 0], r=[0 0 0 0], b=[0 0 0 0 1 1 1], a=[1 1 0 1].
Pic 4: default model configuration parameters
The error message indicates that gfdeconv is either not on the path or not licensed. Can you run gfdeconv from the Matlab prompt in the same session in which the Simulink model is open?
i'm sorry, it's my mistake. When i make post i worked in r2007b, but when i have coder (eml in r2007b) error i change matlab to r2014, where i'm not test gfdeconv in vanilla matlab, maybe some toolbox is missed. Now i tried this model in r2021 with all toolbox and it works. Thanks. I fixed all in r2007b but it has error with compiler (info from google). So it's easier to stay in a newer version. But as you noticed size of q and r is actual. If i take zeros(size(b)) (because it large), will have size error. For example, must be 1x4 size (actual in picture), but i write 1x5, compiler knows what size must be, maybe it is possible to get this size avoiding pre-allocated size of zeros?
The only think I can think of is to write a wrapper function that calls gfdeconv and stuffs the results into large, fixed-size vectors. So, a function like this assuming that the largest ever needed for q and r is eight elements.
function [q,r] = gfdeconvwrapper(b,a)
[qq,rr] = gfdeconv(b,a);
q = [zeros(1,8 - numel(qq)) qq];
r = [zeros(1,8 - numel(rr)) rr];
end
With that function somewhere on the Matlab path, fcn in the Simulink model looks like this
function [q,r] = fcn(b,a)
coder.extrinsic('gfdeconvwrapper')
q = zeros(1,8);
r = zeros(1,8);
[q,r] = gfdeconvwrapper(reshape(b,1,[]),reshape(a,1,[]));
end
In this way, q and r output from fcn will always have eight elements, regardless of the size of the output from gfdeconv. Increase from eight if needed. Hopefully, the zero-padding on the left won't have any impact on the rest of the model.
Thank you so much, it works.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Error Detection and Correction 的更多信息

产品

版本

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by