<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="m0097">
  
  <name>Error-Correcting Codes: Hamming Codes</name>

  <metadata>
  <md:version>2.23</md:version>
  <md:created>2000/07/31</md:created>
  <md:revised>2005/04/15 15:10:35.786 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="jac3">
      <md:firstname>John</md:firstname>
      <md:othername>Austin</md:othername>
      <md:surname>Cottrell</md:surname>
      <md:email>jac3@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>decode</md:keyword>
    <md:keyword>digital communication</md:keyword>
    <md:keyword>double-bit</md:keyword>
    <md:keyword>error</md:keyword>
    <md:keyword>error correcting code</md:keyword>
    <md:keyword>generator matrix</md:keyword>
    <md:keyword>Hamming</md:keyword>
    <md:keyword>Hamming code</md:keyword>
    <md:keyword>information communication</md:keyword>
    <md:keyword>parity</md:keyword>
    <md:keyword>parity check</md:keyword>
    <md:keyword>single-bit</md:keyword>
  </md:keywordlist>

  <md:abstract>A description of some strategies for minimizing there errors in a transmitted bit-stream.</md:abstract>
</metadata>

  
  <content>
    <para id="par1">
      For the <cnxn document="m0095" target="codematrix" strength="6">(7,4) example</cnxn>, we have

      <m:math display="inline">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:minus/>
	    <m:apply>
	      <m:power/>
	      <m:cn>2</m:cn>
	      <m:apply>
		<m:minus/>
		<m:ci>N</m:ci>
		<m:ci>K</m:ci>
	      </m:apply>
	    </m:apply>
	    <m:cn>1</m:cn>
	  </m:apply>
	  <m:cn>7</m:cn>
	</m:apply>
      </m:math>

      error patterns that can be corrected.  We start with single-bit
      error patterns, and multiply them by the parity check matrix.
      If we obtain unique answers, we are done; if two or more error
      patterns yield the same result, we can try double-bit error
      patterns.  In our case, single-bit error patterns give a unique
      result.
    </para>

    <table frame="all" id="table1">
      <name>Parity Check Matrix</name>
      <tgroup cols="2" align="left" colsep="1" rowsep="1">
	<thead valign="top">
	  <row>
	    <entry>
	      e
	    </entry>
	    <entry>
	      He
	    </entry>
	  </row>
	</thead>
	<tbody valign="top">
	  <row>
	    <entry>
	      1000000
	    </entry>
	    <entry>
	      101
	    </entry>
	  </row>
	  <row>
	    <entry>
	      0100000
	    </entry>
	    <entry>
	      111
	    </entry>
	  </row>
	  <row>
	    <entry>
	      0010000
	    </entry>
	    <entry>
	      110
	    </entry>
	  </row>
	  <row>
	    <entry>
	      0001000
	    </entry>
	    <entry>
	      011
	    </entry>
	  </row>
	  <row>
	    <entry>
	      0000100
	    </entry>
	    <entry>
	      100
	    </entry>
	  </row>
	  <row>
	    <entry>
	      0000010
	    </entry>
	    <entry>
	      010
	    </entry>
	  </row>
	  <row>
	    <entry>
	      0000001
	    </entry>
	    <entry>
	      001
	    </entry>
	  </row>
	</tbody>
      </tgroup>
    </table>

    <para id="parafix2">
      This corresponds to our decoding table: We associate the parity
      check matrix multiplication result with the error pattern and
      add this to the received word. If more than one error occurs
      (unlikely though it may be), this "error correction" strategy
      usually makes the error worse in the sense that more bits are
      changed from what was transmitted.
    </para>

    <para id="parafix1">
      As with the repetition code, we must question whether our (7,4)
      code's error correction capability compensates for the increased
      error probability due to the necessitated reduction in bit
      energy.  <cnxn target="figurefixy"/> shows that if the
      signal-to-noise ratio is large enough channel coding yields a
      smaller error probability.  Because the bit stream emerging from
      the source decoder is segmented into four-bit blocks, the fair
      way of comparing coded and uncoded transmission is to compute
      the probability of <term>block</term> error: the probability
      that any bit in a block remains in error despite error
      correction and regardless of whether the error occurs in the
      data or in coding buts.  Clearly, our (7,4) channel code does
      yield smaller error rates, and is worth the additional systems
      required to make it work.
    </para>

    <figure id="figurefixy" orient="vertical">
      <name>Probability of error occurring</name>
      <media type="image/png" src="hamming.png"/>
      <caption>
	The probability of an error occurring in transmitted
	<m:math>
	  <m:apply>
	    <m:eq/>
	    <m:ci>K</m:ci>
	    <m:cn>4</m:cn>
	  </m:apply>
	</m:math> data bits equals
	<m:math>
	  <m:apply>
	    <m:minus/>
	    <m:cn>1</m:cn>
	    <m:apply>
	      <m:power/>
	      <m:apply>
		<m:minus/>
		<m:cn>1</m:cn>
		<m:ci><m:msub>
		    <m:mi>p</m:mi>
		    <m:mi>e</m:mi>
		  </m:msub></m:ci>
	      </m:apply>
	      <m:cn>4</m:cn>
	    </m:apply>
	  </m:apply>
	</m:math>
	as 
	<m:math>
	  <m:apply>
	    <m:power/>
	    <m:apply>
	      <m:minus/>
	      <m:cn>1</m:cn>
	      <m:ci><m:msub>
		  <m:mi>p</m:mi>
		  <m:mi>e</m:mi>
		</m:msub></m:ci>
	    </m:apply>
	    <m:cn>4</m:cn>
	  </m:apply>
	</m:math>
	equals the probability that the four bits are received without
	error.  The upper curve displays how this probability of an
	error anywhere in the four-bit block varies with the
	signal-to-noise ratio.  When a (7,4) single-bit error
	correcting code is used, the transmitter reduced the energy it
	expends during a single-bit transmission by 4/7, appending
	three extra bits for error correction.  Now the probability of
	any bit in the <emphasis>seven</emphasis>-bit block being in
	error after error correction equals

	<m:math>
	  <m:apply>
	    <m:minus/>

	    <m:apply>
	      <m:minus/>
	      <m:cn>1</m:cn>
	      <m:apply>
		<m:power/>
		<m:apply>
		  <m:minus/>
		  <m:cn>1</m:cn>
		  <m:ci><m:msub>
		      <m:mi>p</m:mi>
		      <m:mi>e</m:mi>
		    </m:msub></m:ci>
		</m:apply>
		<m:cn>7</m:cn>
	      </m:apply>
	    </m:apply>

	    <m:apply>
	      <m:times/>
	      <m:apply>
		<m:times/>
		<m:cn>7</m:cn>
		<m:ci><m:msubsup>
		    <m:mi>p</m:mi>
		    <m:mi>e</m:mi>
		    <m:mo>′</m:mo>
		  </m:msubsup></m:ci>
	      </m:apply>
	      <m:apply>
		<m:power/>
		<m:apply>
		  <m:minus/>
		  <m:cn>1</m:cn>
		  <m:ci><m:msubsup>
		      <m:mi>p</m:mi>
		      <m:mi>e</m:mi>
		      <m:mo>′</m:mo>
		    </m:msubsup></m:ci>
		</m:apply>
		<m:cn>6</m:cn>
	      </m:apply>
	    </m:apply>
	  </m:apply>
	</m:math>, where
	<m:math>
	  <m:ci><m:msubsup>
	      <m:mi>p</m:mi>
	      <m:mi>e</m:mi>
	      <m:mo>′</m:mo>
	    </m:msubsup></m:ci>
	</m:math>
	is the probability of a bit error occurring in the channel when
	channel coding occurs.  Here
	<m:math>
	  <m:apply>
	    <m:times/>
	    <m:apply>
	      <m:times/>
	      <m:cn>7</m:cn>
	      <m:ci><m:msubsup>
		  <m:mi>p</m:mi>
		  <m:mi>e</m:mi>
		  <m:mo>′</m:mo>
		</m:msubsup></m:ci>
	    </m:apply>
	    <m:apply>
	      <m:power/>
	      <m:apply>
		<m:minus/>
		<m:cn>1</m:cn>
		<m:ci><m:msubsup>
		    <m:mi>p</m:mi>
		    <m:mi>e</m:mi>
		    <m:mo>′</m:mo>
		  </m:msubsup></m:ci>
	      </m:apply>
	      <m:cn>6</m:cn>
	    </m:apply>
	  </m:apply>
	</m:math> equals the probability of exactly on in seven bits
	emerging from the channel in error; The channel decoder
	corrects this type of error, and all data bits in the block
	are received correctly.
      </caption>
    </figure>
    
    <para id="par2">
      Note that our (7,4) code has the length and number of data bits
      that perfectly fits correcting single bit errors.  This pleasant
      property arises because the number of error patterns that can be
      corrected,

      <m:math display="inline">
	<m:apply>
	  <m:minus/>
	  <m:apply>
	    <m:power/>
	    <m:cn>2</m:cn>
	    <m:apply>
	      <m:minus/>
	      <m:ci>N</m:ci>
	      <m:ci>K</m:ci>
	    </m:apply>
	  </m:apply>
	  <m:cn>1</m:cn>
	</m:apply>
      </m:math>, equals the codeword length
      
      <m:math display="inline"><m:ci>N</m:ci></m:math>.  Codes that
      have
      
      <m:math display="inline">
	<m:apply>
	  <m:eq/>
	  <m:apply>
	    <m:minus/>
	    <m:apply>
	      <m:power/>
	      <m:cn>2</m:cn>
	      <m:apply>
		<m:minus/>
		<m:ci>N</m:ci>
		<m:ci>K</m:ci>
	      </m:apply>
	    </m:apply>
	    <m:cn>1</m:cn>
	  </m:apply>
	  <m:ci>N</m:ci>
	</m:apply>
      </m:math>
      
      are known as <term>Hamming codes</term>, and <cnxn target="hammtable" strength="8">the following table</cnxn>
       provides the parameters of these codes.  Hamming codes are the
       simplest single-bit error correction codes, and the
       generator/parity check matrix formalism for channel coding and
       decoding works for them.
    </para>

    <table frame="all" id="hammtable">
      <name>Hamming Codes</name>
      <tgroup cols="3" align="left" colsep="1" rowsep="1">
	<thead valign="top">
	  <row>
	    <entry>
	      <m:math><m:ci>N</m:ci></m:math>
	    </entry>
	    <entry>
	      <m:math><m:ci>K</m:ci></m:math>
	    </entry>
	    <entry>
	      <m:math><m:ci>E</m:ci></m:math> (efficiency)
	    </entry>
	  </row>
	</thead>
	<tbody valign="top">
	  <row>
	    <entry>
	      3
	    </entry>
	    <entry>
	      1
	    </entry>
	    <entry>
	      0.33
	    </entry>
	  </row>
	  <row>
	    <entry>
	      7
	    </entry>
	    <entry>
	      4
	    </entry>
	    <entry>
	      0.57
	    </entry>
	  </row>
	  <row>
	    <entry>
	      15
	    </entry>
	    <entry>
	      11
	    </entry>
	      <entry>
	      0.73
	    </entry>
	  </row>
	  <row>
	    <entry>
	      31
	    </entry>
	    <entry>
	      26
	    </entry>
	    <entry>
	      0.84
	    </entry>
	  </row>
	  <row>
	    <entry>
	      63
	    </entry>
	    <entry>
	      57
	    </entry>
	    <entry>
	      0.90
	    </entry>
	  </row>
	  <row>
	    <entry>
	      127
	    </entry>
	    <entry>
	      120
	    </entry>
	    <entry>
	      0.94
	    </entry>
	  </row>
	</tbody>
      </tgroup>
    </table>

    <para id="par3">
      Unfortunately, for such large blocks, the probability of
      multiple-bit errors can exceed the number of single-bit errors
      unless the channel single-bit error probability

      <m:math display="inline">
	<m:ci><m:msub>
	    <m:mi>p</m:mi>
	    <m:mi>e</m:mi>
	  </m:msub></m:ci>
      </m:math> 

      is very small.  Consequently, we need to enhance the code's
      error correcting capability by adding double as well as
      single-bit error correction.
    </para>

    <exercise id="exer4">
      <problem>
	<para id="prob1">
	  What must the relation between
	  <m:math><m:ci>N</m:ci></m:math> and
	  <m:math><m:ci>K</m:ci></m:math> be for a code to correct all
	  single- and double-bit errors with a "perfect fit"?
	</para>
      </problem>
      <solution>
	<para id="sol1">
	  In a length-<m:math><m:ci>N</m:ci></m:math> block,
	  <m:math><m:ci>N</m:ci></m:math> single-bit and

	  <m:math display="inline">
	    <m:apply>
	      <m:divide/>
	      <m:apply>
		<m:times/>
		<m:ci>N</m:ci>
		<m:apply>
		  <m:minus/>
		  <m:ci>N</m:ci>
		  <m:cn>1</m:cn>
		</m:apply>
	      </m:apply>
	      <m:cn>2</m:cn>
	    </m:apply>
	  </m:math>

	  double-bit errors can occur.  The number of non-zero vectors
	  resulting from
	  <m:math display="inline">
	    <m:apply>
	      <m:times/>
	      <m:ci type="matrix">H</m:ci>
	      <m:ci type="vector">
		<m:mover>
		  <m:mi>c</m:mi>
		  <m:mo>^</m:mo>
		</m:mover></m:ci>
	    </m:apply>
	  </m:math>
	  must equal or exceed the sum of these two numbers.

	  <equation id="equ0001">
	    <m:math>
	      <m:apply>
		<m:geq/>
		<m:apply>
		  <m:minus/>
		  <m:apply>
		    <m:power/>
		    <m:cn>2</m:cn>
		    <m:apply>
		      <m:minus/>
		      <m:ci>N</m:ci>
		      <m:ci>K</m:ci>
		    </m:apply>
		  </m:apply>
		  <m:cn>1</m:cn>
		</m:apply>
		<m:apply>
		  <m:plus/>
		  <m:ci>N</m:ci>
		  <m:apply>
		    <m:divide/>
		    <m:apply>
		      <m:times/>
		      <m:ci>N</m:ci>
		      <m:apply>
			<m:minus/>
			<m:ci>N</m:ci>
			<m:cn>1</m:cn>
		      </m:apply>
		    </m:apply>
		    <m:cn>2</m:cn>
		  </m:apply>
		</m:apply>
	      </m:apply>

	      <m:mtext>  or  </m:mtext>
	      
	      <m:apply>
		<m:geq/>
		<m:apply>
		  <m:power/>
		  <m:cn>2</m:cn>
		  <m:apply>
		    <m:minus/>
		    <m:ci>N</m:ci>
		    <m:ci>K</m:ci>
		  </m:apply>
		</m:apply>
		<m:apply>
		  <m:divide/>
		  <m:apply>
		    <m:plus/>
		    <m:apply>
		      <m:power/>
		      <m:ci>N</m:ci>
		      <m:cn>2</m:cn>
		    </m:apply>
		    <m:ci>N</m:ci>
		    <m:cn>2</m:cn>
		  </m:apply>
		  <m:cn>2</m:cn>
		</m:apply>
	      </m:apply>
	    </m:math>  
	  </equation>

	  The first two solutions that attain equality are (5,1) and
          (90,78) codes.  However, <emphasis>no</emphasis> perfect
          code exists other than the single-bit error correcting
          Hamming code. (Perfect codes satisfy relations like <cnxn target="equ0001" strength="9"/> with equality.)
	</para>
      </solution>
    </exercise>

  </content>
</document>
