Matlab 2015a HDL coder

1 次查看(过去 30 天)
Kaishavi Umrethwala
回答: Nikhilesh 2023-1-20
I have used matlab 2015a HDL coder for converting SAD( Sum of Absolute Differences) matl Sum of Absolute Differences) matlab code to VHDL code. I am getting following error 9 times. Is there any problem with the for loops I have used?
SAD1_HDL_fixpt:0ErrorFound an unsupported unbounded loop structure. This loop may be user written or automatically generated due to the use of specific vector expressions or functions. For more information on unsupported loop structures, please refer to the documentation.
function [ Disparity ] = SAD1_HDL( A1,A2 )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
A1 = reshape(A1,[375,450]);
A2 = reshape(A2,[375,450]);
[imgHeight, imgWidth] = size(A1);
Disparity=zeros(size(A1));
disparityRange=50;
persistent blockDiffs
persistent template
persistent block;
persistent X;
persistent X1;
if isempty(template)
template = uint8(zeros(3,3));
block = uint8(zeros(3,3));
X = zeros(3,3);
X1 = zeros(3,3);
blockDiffs = zeros(1,50);
end
for m = 2 : 374 %imgHeight-1
for n = 2 : 449 %imgWidth-1
% mind=0;
maxd = min(50, 449 - n);
%template = A2(m-1:m+1, n-1:n+1);
a2r = m-2;
a2c = n-2;
for j=1:3
for k=1:3
template(j,k) = A2((a2r+j),(a2c+k));
end
end
for i = 0 : maxd-1
% Select the block from the left image at the distance 'i'.
%block = A1(m-1:m+1, (n-1 + i):(n+1 + i));
a1r = m-2;
a1c = n-2+i;
for j=1:3
for k=1:3
block(j,k) = A1((a1r+j),(a1c+k));
end
end
% Compute the 1-based index of this block into the 'blockDiffs' vector.
blockIndex = i +1;
% Take the sum of absolute differences (SAD) between the template
% and the block and store the resulting value.
for k=1:3
for j=1:3
X(k,j) = int8(template(k,j))- int8(block(k,j));
end
end
for k=1:3
for j=1:3
if (X(j,k)<0)
X1(j,k)=X(j,k)*(-1);
else
X1(j,k)=X(j,k);
end
end
end
Y=uint8(0);
for k=1:3
for j=1:3
P=Y;
Y=P+X1(k,j);
end
end
blockDiffs(1,blockIndex) = Y;
end
sortedIndeces = sort(blockDiffs);
bestMatchIndex = sortedIndeces(1, 1);
d = bestMatchIndex - 1;
Disparity(m, n) = d;
end
end
[sd1,sd2] = size(Disparity);
Disparity = reshape(Disparity,1,sd1*sd2);
end

回答(1 个)

Nikhilesh
Nikhilesh 2023-1-20
When generating HDL and realizing hardware in general, control flow like break is not feasible. The full data path must be set in the hardware to always look at every possible bit. That's why the for-loop must run for a constant number of iterations. There's no concept of a program counter and early bail in a calculation. Using a for-loop and if-else is the best way to do this type of calculation. A computation like 'any' is a very simple calculation in hardware because it is just a bunch of OR operations. If you are looking for an index of a value, there will be a lot more hardware required like muxing in a priority structure where the MSB is considered first.
Here is how I would write the code in an extremely compact way.
function y = fcn(u)
% return 0 if no 1's, else return one-based idx of first found 1
% return value is smallest possible fixpt value for idx
y = fi(0, 0, ceil(log2(numel(u))), 0);
for ii = cast(1:numel(u), 'like', y)
if y == 0 && u(ii) == true
y = ii;
end
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by