Variable fold/reduction in Simulink

8 vues (au cours des 30 derniers jours)
Eric
Eric le 8 Avr 2025
I am looking for a method to perform a fold/reduction across a variable number of inputs in the form of a vector signal. I specifically would like it to work without manually rewiring to multiple ports whenever I change the number of inputs. There currently exists one for add/sum, but there is no equivalent for general binary functions. The closest solution seems to be with delay + feedback in a systolic fashion like with CORDIC iterations however I need a method without delay and removing the delay in this solution gives an algebraic loop error. I also need the method to be HDL Coder compatible.

Réponses (1)

Darshak
Darshak le 30 Mai 2025
Hello @Eric,
I ran into a similar challenge where I needed to apply a reduction operation across a vector signal in Simulink and make sure it is HDL coder compatible. I found that this can be done using the following approach:
  • Use a MATLAB Function block inside a Subsystem (this is required for HDL Coder).
  • Make sure your input vector is fixed size and set the data type to fixed-point using “fixdt” because double isn’t supported for synthesis.
  • Implement the reduction as a simple loop applying the binary function over the vector elements. Keep it purely combinational to avoid algebraic loops.
  • Define your binary operation inside a helper function so it’s easy to swap out.
This approach works well with HDL Coder too You may use the following function for reference:
function result = reduce_vector(u)
%#codegen
assert(~isempty(u));
N = length(u);
result = u(1);
for i = 2:N
result = my_binary_op(result, u(i));
end
end
function out = my_binary_op(a, b)
% Change to your own binary operation
out = min(a, b);
end
You can refer to the following documentation for more information on HDL Coder - https://www.mathworks.com/help/hdlcoder/ug/supported-simulink-blocks.html
I hope this helps.

Catégories

En savoir plus sur Schedule Model Components dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by