some questions about hamming code

% Generate information array with 1000000 elements
N = 1000000
for i=1:N
if rand <.5
s(i)=0;
else
s(i)=1;
end
end
% Generate 3bits check code
source = reshape(s,N/4,[]);
for i = 1:N/4
source(i,5) = xor(source(i,1),xor(source(1,2),source(1,3)));
source(i,6) = xor(source(i,2),xor(source(1,3),source(1,4)));
source(i,7) = xor(source(i,1),xor(source(1,2),source(1,4)));
end
%enery per bit to noise spectral density ratio      
EbNo = [-3:1:10];
% Generate white noise
b = randn(1,N*7/4);
%14 noise density
for j = 1:14
%join white noise
sigma(j) = power(10,(-EbNo(j)/20))/ sqrt (2);
for i = 1:N*7/4
n(i)=sigma(j)*b(i);
end
n = reshape(n,N/4,[]);
y=source+n;
%Receive transmitted codeword  
for i = 1 : N/4
for k = 1 : 7
if y(i,k) > 0
y(i,k) = 1;
else
y(i,k) = 0;
end
end
end
% decoding
H = [1 1 1 0 1 0 0
0 1 1 1 0 1 0
1 1 0 1 0 0 1
];
t_H = H';
syndrome = mod((y* t_H),2);
% calculate bit error probability
Nberreur(j)=0;
for i = 1 : N/4
if (syndrome(i,1) == 1) & (syndrome(i,2) == 0) & (syndrome(i,3) == 1)
Nberreur(j) = Nberreur(j) +1;
y(i,1) = ~y(i,1);
end
if (syndrome(i,1) == 1) & (syndrome(i,2) == 1) & (syndrome(i,3) == 1)
Nberreur(j) = Nberreur(j) +1;
y(i,2) = ~y(i,2);
end
if (syndrome(i,1) == 1) & (syndrome(i,2) == 1) & (syndrome(i,3) == 0)
Nberreur(j) = Nberreur(j) +1;
y(i,3) = ~y(i,3);
end
if (syndrome(i,1) == 0) & (syndrome(i,2) == 1) & (syndrome(i,3) == 1)
Nberreur(j) = Nberreur(j) +1;
y(i,4) = ~y(i,4);
end
end
%calculate bit error probability
Tauxderreur2(j) = Nberreur(j) / N;
end
%show plot
semilogy(EbNo,Tauxderreur2);
My friends, I'm learning hamming code now. And I want to calculate bit error probability and this is an example. At first, I want to ask what "if rand <.5" means? I know what rand means but I have never met "<.5" before. Secondly, I wanna to ask why we use "syndrome = mod((y* t_H),2)" to decode? I try several arrays and it works, but I don't know why? Finally, should we join white noise for every time? Or it's just optional?

1 Comment

rand generates a new random number in the open range 0 to 1 .
< is comparison for less than
If you compare random over 0 to 1 as less than 0.5 then you are going to take the branch with aa 50% chance . If it were less than 0.374 then that would be aa 37.4% chance of taking the branch.

Sign in to comment.

Answers (0)

Asked:

on 10 Dec 2018

Commented:

on 11 Dec 2018

Community Treasure Hunt

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

Start Hunting!