ASCII/HAMMING DECODER

5 次查看(过去 30 天)
Hi guys. I work on encoding and decoding ASCII binary characters with hamming code 7.4 although the v code runs without stopping on an error, but the decoding output is incorrect. It only works for some letters like: e,l,o,c,i the rest of the letters don't work properly. Can you please advise me what is wrong with the code?
% Define the generator matrix for Hamming code 7.4
G = [1 0 0 0 1 1 0;
0 1 0 0 1 0 1;
0 0 1 0 1 1 1;
0 0 0 1 0 1 1];
% Define the parity check matrix for Hamming code 7.4
H = [1 1 1 0 1 0 0;
1 0 1 1 0 1 0;
1 1 0 1 0 0 1];
% Ask the user to input a text message
msg = input("Enter a text message: ", 's');
% Convert the message to ASCII codes
asciiMsg = uint8(msg);
% Convert the ASCII codes to binary and concatenate them into a single vector
binaryMsg = dec2bin(asciiMsg, 8) - '0';
binaryMsg = binaryMsg.';
% Pad the binary message with zeros if its length is not a multiple of 4
numPaddingZeros = mod(-length(binaryMsg), 4);
binaryMsg = [zeros(1, numPaddingZeros), binaryMsg(:).'].';
% Reshape the binary message into 4-bit blocks
binaryMsg = reshape(binaryMsg, 4, []).';
% Encode each 4-bit block using Hamming code 7.4
encodedMsg = mod(binaryMsg * G, 2);
% Convert the encoded message to a string of characters
encodedMsgStr = reshape(char(encodedMsg.'+'0'), 1, []);
% Print the encoded message
disp("Encoded message: " + encodedMsgStr)
% Convert the encoded message to binary
encodedMsg = encodedMsgStr - '0';
% Reshape the binary message into 7-bit blocks
encodedMsg = reshape(encodedMsg, 7, []).';
% Calculate the syndrome for each 7-bit block
syndrome = mod(encodedMsg * H.', 2);
% Determine the error correction vector
errorVector = zeros(size(syndrome, 1), 7);
for i = 1:size(syndrome, 1)
for j = 1:size(H, 2)
if H(:, j).' == syndrome(i, :)
errorVector(i, j) = 1;
end
end
end
% Correct the errors in the encoded message
decodedEncodedMsg = mod(encodedMsg + errorVector, 2);
% Remove the parity bits to retrieve the original message
decodedMsg = decodedEncodedMsg(:, [1, 2, 3, 5]);
% Convert the binary message to decimal and then to ASCII
decodedMsg = decodedMsg.';
decodedMsg = decodedMsg(:);
decodedMsg = reshape(decodedMsg, 8, []).';
decodedMsg = bin2dec(num2str(decodedMsg, '%d'));
decodedMsg = char(decodedMsg).';
% Print the decoded message
disp("Decoded message: " + decodedMsg)
  1 个评论
Voss
Voss 2023-2-25
Notice that, at the point where you do the zero-padding, binaryMsg is an 8-by-M matrix, where M is the number of characters in the original message:
msg = 'message'; % 7 characters long
% Convert the message to ASCII codes
asciiMsg = uint8(msg);
% Convert the ASCII codes to binary and concatenate them into a single vector
binaryMsg = dec2bin(asciiMsg, 8) - '0';
binaryMsg = binaryMsg.' % 8-by-7 after transposing
binaryMsg = 8×7
0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 1 0 1 1 1 1 1 1 1
Now, length returns the size of the largest dimension, so length(binaryMsg) is:
  • the length of the message, if the message is 8 or more characters long, or
  • 8, if the message is 8 or fewer characters long
What I think you meant, instead of length(binaryMsg), is size(binaryMsg,2) or numel(asciiMsg).
However, that's not going to work either because this
binaryMsg = [zeros(1, numPaddingZeros), binaryMsg(:).'].';
is only padding numPaddingZeros zeros, instead of 8*numPaddingZeros zeros (i.e., a column's worth of zeros for each "missing" column of binaryMsg), so you'll get an error using reshape on the next line any time the message length is more than 8 and not a multiple of 4.
Instead of padding binaryMsg with zeros, you could more easily pad asciiMsg with numPaddingZeros zeros, before binaryMsg is created.
However, I don't think you need any zero-padding at all because the purpose of zero-padding in this case is to make sure the number of elements in binaryMsg (not the number of columns) is a multple of 4 (so that you can encode each sequence of 4 bits by multiplying with G to get 7 encoded bits), and since you are expressing each character as 8 bits when you do
binaryMsg = dec2bin(asciiMsg, 8) - '0';
% ^ eight bits specified
you are guaranteeing that numel(binaryMsg) is a multiple of 8 (and therefore a multiple of 4); no zero-padding is necessary.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2023-2-24
It's been awhile since I studied error-correcting codes, but I believe that with this generator matrix:
G = [1 0 0 0 1 1 0;
0 1 0 0 1 0 1;
0 0 1 0 1 1 1;
0 0 0 1 0 1 1];
the message bits are bits 1-4 and the parity bits are bits 5-7. (Notice G(:,1:4) is the identity matrix - that's how you can tell which are message bits - and the parity bits always have more than one 1 in their columns.)
So use the following to retrieve the original message
% Remove the parity bits to retrieve the original message
decodedMsg = decodedEncodedMsg(:, [1, 2, 3, 4]);
  1 个评论
Branislav Gabco
Branislav Gabco 2023-2-25
Thank you very much for explanation and solving my problem with code. After running the code. Code has correct output.
>> encod_decod
Enter a text message: message
Encoded message: 01100101101000011001001011100111001001110001110010011100011001000010110110010011100101100100101110
Decoded message: message

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by