-
Notifications
You must be signed in to change notification settings - Fork 1
/
hammingcode_decoder.m
executable file
·91 lines (75 loc) · 2.49 KB
/
hammingcode_decoder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function [ decodedMessage ] = hammingcode_decoder( encodedMessage )
n = length(encodedMessage);
k = floor(log2(n));
%populate a matrix of parity indices
for i = 0:k
k_arr(i+1) = 2^i;
end
decodedMessage = [];
checkBits = [];
%this constructs a checkbit array to compare with the parity bits
for i = 1:length(k_arr)
parityBits(i) = encodedMessage(k_arr(i));
paritySummer = 0;
%begin at the parity bit index + 1
j = k_arr(i)+1;
%start counter at 1 to ensure parity bit isn't included in
%calculation
counter = 1;
while (true)
if counter >= k_arr(i)
% skip k bits if k bits have been checked
j = j + k_arr(i);
%reset counter
counter = 0;
end
if (j > length(encodedMessage))
break;
end
paritySummer = paritySummer + encodedMessage(j);
counter = counter + 1;
j = j + 1;
end
parityBit = mod(paritySummer,2);
checkBits(i) = parityBit;
end
%check for errors
errBits = zeros(1,length(checkBits));
errorFlag = 0;
erroneousBit = 0;
for i = 1:length(checkBits)
if (checkBits(i) ~= parityBits(i))
% record bits in error
errBits(i) = 1;
% set flag to 1 to indicate there were errors
errorFlag = 1;
end
end
if errorFlag == 0
fprintf('No errors found!\n');
else
errors = find(errBits==1);
fprintf('Errors found in parity bits ');
for i = 1:length(errors)
fprintf('%d, ', errors(i))
end
fprintf('\n');
%identify the bit in error
erroneousBit = sum(k_arr(errors));
fprintf('Bit %d is the erroneous bit\n', erroneousBit);
end
% Construct error correct decoded message
for i = 1:length(encodedMessage)
if (~isempty(k_arr)) && i == k_arr(1)
k_arr(1) = [];
elseif i == erroneousBit
%flip the erroneous bit and append to decoded message
decodedMessage = [decodedMessage, ~erroneousBit];
else
%append data bit to decoded message
decodedMessage = [decodedMessage, encodedMessage(i)];
end
end
fprintf('Check Bits: %s\n',mat2str(checkBits));
fprintf('Parity Bits: %s\n',mat2str(parityBits));
end