Search code examples
arraysmatlabloopsvhdlhdl

Expression out of bounds on MATLAB with HDL coder app


I try to get the VHDL code corresponding to my simulation on MATLAB with HDL coder app, but I get a first error at the line 25 when I build the MATLAB code on the HDL coder app:

Index expression out of bounds. Attempted to access element 15. The valid range is 1-1.

I don't understand because the simulation on MATLAB works and I don't get this error.

function U_p = HDL_decoder_function(r)

H1 = [ 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0 ];
H = 0;
S_in_rows = 0;
Sm = 0;
S = 0;
f_in_column = 0;
f = 0;
f_largest = 0;
f_nth_largest = 0;

% Declaration of the parity-check matrix

    % Fill the first row to prepare the shift in the H matrix
 for i = 1 : 15
     H( 1, i ) = H1(i);
 end


% Fill all the other rows with the shift
for j = 2 : 15
    for i = 1 : 15
       if( i == 1 )
           H( j, i) = H( j-1, 15);  % first error
       else
           H( j, i) = H( j-1, i-1);
       end
    end  
end
H;


% Start of the bit-flipping algorithm 

for k = 1 : 20 % Authorize 20 executions maximum of the statements of the           algorithm

% Calculate the syndrome S = r^T * H
for j = 1 : 15
    for i = 1 : 15
        S_in_rows(i) = and( r(i), H( j, i) );
    end
    for i = 1 : 15
        Sm = sum(S_in_rows);
    end
    if rem(Sm, 2) == 1
        S(j) = 1;
    else
        S(j) = 0;
    end
end
S;
% Go out from the loop when syndrome S = 0
if S == 0
    U_p = r;
    break
end
if k == 20
    disp('Decoding fail')
    U_p = [ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ];
end

% Find the number f of failed syndrome bits for every received bits r
for i = 1 : 15
    f(i) = 0; % Declaration
end
for i = 1 : 15
    for j = 1 : 15
        f_in_column = and( S(j), H( j, i) );
        if f_in_column == 1 
            f(i) = f(i)+1;
        end
    end
end
f;

% Flip the the rth bit corresponding to the first largest number of error in f 
f_largest = 0;
for i = 1 : 15 
    if f(i) > f_largest
        f_largest = f(i); % Save the number of error 
        f_nth_largest = i; % Save the position of f_largest
    end
end
f_largest;
f_nth_largest;

r(f_nth_largest) = not(r(f_nth_largest));
r;

U_p = r;

end

Solution

  • I don't use the HDL coder, but I have a likely cause. From your code:

    H = 0;
    
    % Fill the first row to prepare the shift in the H matrix
    for i = 1 : 15
        H( 1, i ) = H1(i);
    end
    

    Here, you define H to be a single integer, than use it as if it was a matrix. This is bad practice in Matlab (though will run) since each time you write to an array location that doesn't exist, Matlab creates a new array, then copy the current content to it, free the previous content and finally make the assignation. When it comes to VHDL, the variable size must be fixed and known in advance, there is no dynamic array in hardware.

    You should pre-allocate your variables to the right dimensions before you use them. Simply change H = 0 to

    H = zeros(15, 15);
    

    Note that similarly, other variables are not initialized to the right size, including S, S_in_row and f.