Multiple copies of the same embedded subfunction in simulink
Afficher commentaires plus anciens
Hello, I would like to use multiple instances of a line_buffer (from the eml design patterns) in the same function in order to build a muti-channel buffer. This is intended for hdl gen. A sketch of the code is something like (I provided this code as an example but may not work as is):
function out_vect = multichannel_buffer(u_vect,N)
for ch=1:8
out_vect(ch) = line_buffer(u_vect(ch), N);
end
end
function y = line_buffer(u, N)
persistent buffer ctr;
if isempty(buffer)
buffer = fi(zeros(1,N), numerictype(u), fimath(u));
ctr = uint8(1);
end
y = buffer(ctr);
buffer(ctr) = u;
if ctr == N
ctr = uint8(1);
else
ctr = ctr + 1;
end
end
The problem here is that both persistent variables (buffer and ctr) are the same on all the calls. Is there any way I can write just one line_buffer function and use it multiple times ( in a channel per channel basis), having a separate variable stack (buffer and ctr) for each channel?
Thanks in advance for any clue, Regards A
1 commentaire
Kiran Kintali
le 1 Juin 2012
short of using system object for representing the delay, i do not know if there is a best way to model this using persistent variables in MATLAB. thanks.
function out_vect = multichannel_buffer(u_vect,N)
persistent h1 h2 h3 h4 h5 h6 h7 h8
if isempty(h1)
h1 = dsp.Delay(N);
h2 = dsp.Delay(N);
h3 = dsp.Delay(N);
h4 = dsp.Delay(N);
h5 = dsp.Delay(N);
h6 = dsp.Delay(N);
h7 = dsp.Delay(N);
h8 = dsp.Delay(N);
end
out_vect = u_vect; % Initialize
out_vect(1) = step(h1, u_vect(1));
out_vect(2) = step(h2, u_vect(2));
out_vect(3) = step(h3, u_vect(3));
out_vect(4) = step(h4, u_vect(4));
out_vect(5) = step(h5, u_vect(5));
out_vect(6) = step(h6, u_vect(6));
out_vect(7) = step(h7, u_vect(7));
out_vect(8) = step(h8, u_vect(8));
end
Réponses (2)
Kaustubha Govind
le 14 Mai 2012
0 votes
It seems writing line_buffer as a class with 'buffer' and 'ctr' as its members might be the right way to solve this problem. However, MATLAB Coder started supporting code generation for MATLAB classes only in R2012a. I believe you need to write the class as a System Object. Also see Code Generation for MATLAB Classes for more information.
2 commentaires
Andres
le 14 Mai 2012
Kaustubha Govind
le 14 Mai 2012
Oops, it does look like only a limited set of shipping System objects are supported for HDL Code Generation: http://www.mathworks.com/help/toolbox/hdlcoder/ug/bs4rkua-2.html#bteb6n_
Sorry, I don't know what might be a better solution at this point. Perhaps you can wait and see if someone else has a better idea or contact Tech Support if you're on maintenance.
TAB
le 15 Mai 2012
One way is to pass the buffer & ctr on each function call and also return their updated values each time. Something like this
function out_vect = multichannel_buffer(u_vect,N)
buffer = Initialize;
ctr = Initialize;
for ch=1:8
[out_vect(ch) buffer ctr] = line_buffer(u_vect(ch), N, buffer, ctr);
end
end
This will remove the need of persistent variable. You can manage buffer & ctr variables for your each channel using arrays.
Catégories
En savoir plus sur General Applications dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!