使用 HDL 编码器应用程序在 MATLAB 上表达越界

Expression out of bounds on MATLAB with HDL coder app

我尝试使用 HDL 编码器应用程序获取与我在 MATLAB 上的仿真对应的 VHDL 代码,但是当我在 HDL 编码器应用程序上构建 MATLAB 代码时,我在第 25 行遇到第一个错误:

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

我不明白,因为在 MATLAB 上的仿真有效,但我没有收到此错误。

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

我不使用 HDL 编码器,但我有一个可能的原因。来自您的代码:

H = 0;

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

在这里,您将 H 定义为单个整数,而不是像矩阵一样使用它。这在 Matlab 中是不好的做法(尽管会 运行),因为每次写入不存在的数组位置时,Matlab 都会创建一个新数组,然后将当前内容复制到其中,释放以前的内容,最后进行分配。说到VHDL,变量大小必须是固定的,而且是事先知道的,硬件中没有动态数组。

您应该在使用变量之前将它们预先分配到正确的维度。只需将 H = 0 更改为

H = zeros(15, 15);

请注意,类似地,其他变量未初始化为正确的大小,包括 SS_in_rowf