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);


回答(1 个)
Paul
2022-10-8
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
9 个评论
Alexander Mike
2022-10-8
Paul
2022-10-8
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).
Alexander Mike
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.
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.
Alexander Mike
2022-10-9
Paul
2022-10-9
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?
Alexander Mike
2022-10-9
Paul
2022-10-9
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.
Alexander Mike
2022-10-9
类别
在 帮助中心 和 File Exchange 中查找有关 Error Detection and Correction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




