-
Notifications
You must be signed in to change notification settings - Fork 3
/
hamming.sce
110 lines (91 loc) · 1.72 KB
/
hamming.sce
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
disp("Enter block length (n):");
n = input("");
k = 4;
//Number of parity bits
m = n-k;
I = eye(k,k);
//coefficient matrix
P = zeros(k,m);
disp("Enter elements of coefficient matrix P");
for i=1:k
for j=1:m
P(i,j)=input("");
end
end
disp(P, "Coefficient matrix P")
// generator matrix
G = [P I];
disp(G, "Generator matrix G")
//P transpose
PT = P';
// parity check matrix
H = [eye(k-1,k-1) PT];
disp(H, "Parity check matrix H")
//message bits from 0000 to 1111
m = [0,0,0,0;
0,0,0,1;
0,0,1,0;
0,0,1,1;
0,1,0,0;
0,1,0,1;
0,1,1,0;
0,1,1,1;
1,0,0,0;
1,0,0,1;
1,0,1,0;
1,0,1,1;
1,1,0,0;
1,1,0,1;
1,1,1,0;
1,1,1,1];
//
//codeword of Hamming codes
C = m*G;
C = modulo(C,2);
disp(C, "Code words of Hamming code")
//received code word y
y = zeros(1,n);
disp("Enter the received codeword")
for h = 1:n
y(1,h)=input("");
end
disp(y, "Received code word");
//H transpose
HT = H';
//syndrome calculation
S = y*HT;
S = modulo(S,2);
disp(S, "Syndrome of the received message");
// ERROR CORRECTION FOR (7, 4) HAMMING CODE //
if (n==7) & (k==4) then
//syndrome patterns for (7, 4) hamming code
syndrome = [1,0,0;
0,1,0;
0,0,1;
1,1,0;
0,1,1;
1,1,1;
1,0,1];
//corressponding error patterns
error_pattern = eye(7,7);
if S==[0,0,0] then
disp("Your message is error free")
else
disp("Your message has an error")
end
ep= zeros(1,7);
for v = 1:7
if S == syndrome(v,:)
ep=error_pattern(v,:);
end
end
//corrected message
y_corrected = y + ep;
y_corrected = modulo(y_corrected, 2);
disp(y, "Received code word");
disp(S, "Syndrome of the received message");
disp(ep,"Error pattern for syndrome is")
disp(y_corrected, "The corrected code word is")
else
disp("You do not have (7,4) hamming code")
end