📄 block_code_sim.m
字号:
function [encoded,received,corrected,decoded]=block_code_sim(G,n,k,information_bits,to_flip)
len=length(information_bits); % finding the length of the info bits
number_blocks=len/k; % finding the number of blocks of information bits
% by dividing by info word length
j=0;
while j ~= number_blocks, % encoding, decoding, correcting the block of bits
information_bits1= information_bits(1,(j*k)+1:(j+1)*k); %taking the information_bits in sets
[encoded(1,(j*n)+1:(j+1)*n)]= encode(G,n,information_bits1); % we have now received the info bits in blocks i.e. info_bits_1
% we are now encoding them
[received(1,(j*n)+1:(j+1)*n)]=receive(encoded(1,(j*n)+1:(j+1)*n),n,k,j,to_flip); % now we are getting the received bits
% from the encoded bits
[corrected(1,(j*n)+1:(j+1)*n)]=correct(G,n,k,received(1,(j*n)+1:(j+1)*n)); % correcting the bits from the received bits
[decoded(1,(j*k)+1:(j+1)*k)]=decode(n,k,corrected(1,(j*n)+1:(j+1)*n)); % decdoding the block of bits received
j=j+1;
end
% NOW THE WHOLE PRINTING STUFF STARTS
fprintf('********************************************');
fprintf('********************************************');
fprintf('********************************************');
fprintf('\nTable with information bits -- code-word pairs:\n\n');
for i=0:2^k-1
m=convert(i,k);
code=encode(G,n,m);
fprintf('[')
for i=1:k
fprintf('%d',m(1,i));
end
fprintf('] -> [');
for i=1:n
fprintf('%d',code(1,i))
end
fprintf(']');
fprintf('\n');
end
fprintf('********************************************');
fprintf('********************************************');
fprintf('********************************************');
% NOW PRINTING THE S VECTORS AND TABLE ERROR BITS
fprintf('****************************************************');
fprintf('\nTable with bit errors -- syndrom pairs:\n\n');
p=G(1:k,1:n-k); % PARITY MATRIX
h=[eye(n-k) p']; % H MATRIX
trans_h= h'; % TRANSPOSED H MATRIX
for i=0:2^n-1
biterrors=convert(i,n);
syndrom=biterrors*trans_h; % CALCULATING THR SYNDROME MATRIX BY MULTIPLYING BIT ERRORS WITH TRANSPOSE MATRIX
for i=1:n-k,
if rem(syndrom(1,i),2)==0, % SYNDROM MATRIX
syndrom(1,i)=0;
else syndrom(1,i)=1;
end
end
fprintf('[')
% PRINTING THE MATRICES
for i=1:n
fprintf('%d',biterrors(1,i)); % PRINTING THE BIT ERRORS
end
fprintf('] -> [');
for i=1:n-k
fprintf('%d',syndrom(1,i)) % PRINTING THE SYNDROME MATRIX
end
fprintf(']');
fprintf('\n');
end
fprintf('\n*********************************************');
% DISPLAYING THE INFORMATION BITS AND THE ENCODEDE BITS
fprintf('\n\ninformation bits to be encoded:\n\nBits >>>>>'); % DISPLAYING THE INFO BITS
disp(information_bits);
fprintf('\n*********************************************');
fprintf('\n\nMessage to be transmitted:\n\nencoded = \n'); % DISPLAYING THE ENCODED BITS
disp(encoded);
fprintf('\n*********************************************');
% DISPLAYING THE ENCODED MESSAGE WITH FLIPPED BITS
fprintf('\nThe following bits in the encoded message will be flipped >>>>> [ ')
len=length(to_flip); % CALAULATING THE LENGTH OF FLIP BITS
for i=1:len,
fprintf('%d',to_flip(i));
fprintf(' ');
end
% DISPLAYING THE RECEIVED MESSAGE WITH ERRORS
fprintf('] \n Received message with errors:\nreceived = \n'); % DISPLAY CODE OF RCVD MSG WITH ERRORS
disp(received);
fprintf('\n*********************************************');
fprintf('\n*********************************************');
fprintf('\n*********************************************\n\n');
fprintf('corrected bits\n\n'); % DISPLAYING THE CORRECTED BITS
disp(corrected);
fprintf('\n Decoded bits\n\n'); % DISPLAYING THE DECODED BITS
disp(decoded);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -