漢明(7,4)碼,傳輸7bits,其中4bits是資訊,3bits是校驗碼
下面的程式是仿真漢明(7,4)碼的編碼,傳輸,解碼的過程并統計誤碼率,希望對大家有所幫助。
% 産生資訊序列,1000000個碼 0/1
N = 1000000 for i=1:N
if rand <.5>
s(i)=0;
else
s(i)=1;
end
end
% 産生3bits的校驗碼
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];
% 産生白噪聲
b=randn(1,N*7/4);
%14種噪聲密度
for j = 1:14
%加入白噪聲 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;
%接收傳輸資訊 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
% 解碼,H是奇偶校驗矩陣
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);
% 計算誤碼數
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
%計算誤碼率 Tauxderreur2(j) = Nberreur(j) / N;
end
%顯示誤碼率曲線 figure
semilogy(EbNo,Tauxderreur2);
仿真結果如下: