- 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.
Variable fold/reduction in Simulink
8 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Darshak
on 30 May 2025
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:
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.
0 Comments
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!