使用HDL编码器app在MATLAB上表达超出范围

时间:2015-06-02 08:14:42

标签: arrays matlab loops vhdl hdl

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

  

索引表达式超出范围。尝试访问元素15.有效范围为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

1 个答案:

答案 0 :(得分:3)

我不使用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