汉明编码解码的原理就不用多i说了,疑惑的的地方可以翻一翻樊昌信老先生的《通信原理》。
(7,4)汉明编码每组码元共有7位,4个信息位C6 C5 C4 C3 + 3个监督位C2 C1 C0
那么监督矩阵和生成矩阵分别是:
因为(7,4)汉明编码是7位,所以在最末位添加一个0补成8bit方便串口发送和接收。由此可以得到下面这个表格:
上面给出了编码,对于解码通过
计算出S2 S1 S0 就能知道当出现1bit的误码时对应的哪一位是错误的,也就能纠正过来。
具体S2 S1 S0 是对应哪一位的错误可以自己假设一个错误的bit位带入计算,得到S2 S1 S0和误码位置的关系。
下面是vHDL代码:
编码
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity HCode is
port(
data_in :in std_logic_vector(3 downto 0);
H_out :out std_logic_vector(7 downto 0)
);
end HCode;
architecture bv of HCode is
begin
process(data_in)
begin
case data_in is
when "0000"=> H_out <="00000000";
when "0001"=> H_out <="00011010";
when "0010"=> H_out <="00101110";
when "0011"=> H_out <="00110100";
when "0100"=> H_out <="01000110";
when "0101"=> H_out <="01011100";
when "0110"=> H_out <="01101000";
when "0111"=> H_out <="01110010";
when "1000"=> H_out <="10001100";
when "1001"=> H_out <="10010110";
when "1010"=> H_out <="10100010";
when "1011"=> H_out <="10111000";
when "1100"=> H_out <="11001010";
when "1101"=> H_out <="11010000";
when "1110"=> H_out <="11100100";
when "1111"=> H_out <="11111110";
end case;
end process;
end bv;
解码:(本来S算出来应该是3bit,但是为了方便数码管显示我强行加了一个0 凑够4bit)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity Hdecode is
port( --a bit is wrong
Hcode :in std_logic_vector(7 downto 0); --input hamming code
Decode:out std_logic_vector(3 downto 0); --output signal
S :out std_logic_vector(3 downto 0) --where is wrong
);
end entity;
architecture BV of Hdecode is
signal SS :std_logic_vector(3 downto 0):="0000";
begin
process(Hcode)
begin
SS(3) <='0';
SS(2) <=Hcode(7) xor Hcode(5) xor Hcode(4) xor Hcode(3) ;
SS(1) <=Hcode(7) xor Hcode(6) xor Hcode(5) xor Hcode(2) ;
SS(0) <=Hcode(6) xor Hcode(5) xor Hcode(4) xor Hcode(1) ;
--SS <= SS+'1';
case SS is
when "0000" => Decode <= (Hcode(7),Hcode(6),Hcode(5),Hcode(4)); S<="0000";
when "0001" => Decode <= (Hcode(7),Hcode(6),Hcode(5),Hcode(4)); S<="0001";
when "0010" => Decode <= (Hcode(7),Hcode(6),Hcode(5),Hcode(4)); S<="0010";
when "0011" => Decode <= (Hcode(7),not Hcode(6),Hcode(5),Hcode(4)); S<="0110";
when "0100" => Decode <= (Hcode(7),Hcode(6),Hcode(5), Hcode(4)); S<="0011";
when "0101" => Decode <= (Hcode(7),Hcode(6), Hcode(5),not Hcode(4)); S<="0100";
when "0110" => Decode <= (not Hcode(7),Hcode(6),Hcode(5),Hcode(4)); S<="0111";
when "0111" => Decode <= (Hcode(7),Hcode(6),not Hcode(5),Hcode(4)); S<="0101";
when others => S<="1111";
end case;
--S <= SS;
end process;
end BV;