{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 8 Error Control Coding" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example8.1 page 384" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "generator matrix\n", "[[ 1. 1. 1. 1. 1.]]\n", "\n", "parity-check matrix\n", "[[ 1. 0. 0. 0. 1.]\n", " [ 0. 1. 0. 0. 1.]\n", " [ 0. 0. 1. 0. 1.]\n", " [ 0. 0. 0. 1. 1.]]\n", "\n", "code word for binary one input\n", "[[ 1. 1. 1. 1. 1.]]\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import ones,zeros,identity,transpose,hstack,mat\n", "\n", "n =5# #block of identical 'n' bits\n", "k =1# #one bit\n", "m = 1## bit value = 1\n", "I = identity(n-k) #Identity matrix\n", "P = ones(n-k)##coefficient matrix\n", "I=mat(I)\n", "P=mat(P)\n", "H = hstack([I,transpose(P)])##parity-check matrix\n", "G = hstack([P, mat([1])])##generator matrix \n", "x = m*G# #code word\n", "print 'generator matrix\\n',G\n", "print '\\nparity-check matrix\\n',H\n", "print '\\ncode word for binary one input\\n',x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example8.2 page 386" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "identity matrix Ik\n", "[[ 1. 0. 0. 0.]\n", " [ 0. 1. 0. 0.]\n", " [ 0. 0. 1. 0.]\n", " [ 0. 0. 0. 1.]]\n", "\n", "coefficient matrix P\n", "[[1 1 0]\n", " [0 1 1]\n", " [1 1 1]\n", " [1 0 1]]\n", "generator matrix G\n", "[[ 1. 1. 0. 1. 0. 0. 0.]\n", " [ 0. 1. 1. 0. 1. 0. 0.]\n", " [ 1. 1. 1. 0. 0. 1. 0.]\n", " [ 1. 0. 1. 0. 0. 0. 1.]]\n", "parity chechk matrix H\n", "[[ 1. 0. 0. 1. 0. 1. 1.]\n", " [ 0. 1. 0. 1. 1. 1. 0.]\n", " [ 0. 0. 1. 0. 1. 1. 1.]]\n", "Code words of (7,4) Hamming code\n", "[[ 0. 0. 0. 0. 0. 0. 0.]\n", " [ 1. 0. 1. 0. 0. 0. 1.]\n", " [ 1. 1. 1. 0. 0. 1. 0.]\n", " [ 0. 1. 0. 0. 0. 1. 1.]\n", " [ 0. 1. 1. 0. 1. 0. 0.]\n", " [ 1. 1. 0. 0. 1. 0. 1.]\n", " [ 1. 0. 0. 0. 1. 1. 0.]\n", " [ 0. 0. 1. 0. 1. 1. 1.]\n", " [ 1. 1. 0. 1. 0. 0. 0.]\n", " [ 0. 1. 1. 1. 0. 0. 1.]\n", " [ 0. 0. 1. 1. 0. 1. 0.]\n", " [ 1. 0. 0. 1. 0. 1. 1.]\n", " [ 1. 0. 1. 1. 1. 0. 0.]\n", " [ 0. 0. 0. 1. 1. 0. 1.]\n", " [ 0. 1. 0. 1. 1. 1. 0.]\n", " [ 1. 1. 1. 1. 1. 1. 1.]]\n" ] } ], "source": [ "from __future__ import division\n", "from numpy import ones,zeros,identity,multiply,mat,concatenate,hstack,transpose\n", "\n", "\n", "k = 4# #message bits length\n", "n = 7# #block length\n", "m = n-k##Number of parity bits\n", "I = identity(k) #identity matrix\n", "I=mat(I)\n", "print 'identity matrix Ik\\n',I\n", "P =[[1,1,0],[0,1,1],[1,1,1],[1,0,1]]##coefficient matrix\n", "P=mat(P)\n", "print '\\ncoefficient matrix P\\n',P\n", "G = hstack([P,I]) #generator matrix\n", "print 'generator matrix G\\n',G\n", "\n", "H = hstack([identity(k-1),transpose(P)])##parity check matrix\n", "print 'parity chechk matrix H\\n',H\n", "\n", "#message bits\n", "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]]\n", "\n", "C = m*G#\n", "C = (C%2)#\n", "print 'Code words of (7,4) Hamming code\\n',C\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example8.3 page 389" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "remainder in polynomial form: \n", " 2\n", "1 x + 1 x\n", "Parity bits are: [ 1. 1. 0.]\n", "G:\n", "[1, 1, 0, 1, 0, 0, 0]\n", "[0, 1, 1, 0, 1, 0, 0]\n", "[0, 0, 1, 1, 0, 1, 0]\n", "[0, 0, 0, 1, 1, 0, 1]\n", "\n", "Generator Matrix G =\n", "[1, 1, 0, 1, 0, 0, 0]\n", "[0, 1, 1, 0, 1, 0, 0]\n", "[1, 1, 1, 0, 0, 1, 0]\n", "[1, 0, 1, 0, 0, 0, 1]\n", "\n", "Partiy Check matrix H =\n", "\n", "[1, 0, 0, 1, 0, 1, 1]\n", "[0, 1, 0, 1, 1, 1, 0]\n", "[0, 0, 1, 0, 1, 1, 1]\n" ] } ], "source": [ "from numpy import poly1d, polydiv\n", "#message sequence = [1,0,0,1]\n", "g = poly1d([1,0,1,1]) #generator polynomial\n", "m = poly1d([1,0,0,0])*poly1d([1,0,0,1]) #message sequence\n", "q = polydiv(m,g)[0]\n", "r = polydiv(m,g)[1]\n", "p = r.coeffs\n", "print 'remainder in polynomial form: \\n',r\n", "print 'Parity bits are:',p\n", "\n", "def rev_coeffs(x):\n", " X=[]\n", " for i in reversed(x):\n", " X.append(i)\n", " return X\n", "\n", "\n", "G = [rev_coeffs(g.coeffs),rev_coeffs((g*poly1d([1,0])).coeffs),rev_coeffs((g*poly1d([1,0,0])).coeffs),rev_coeffs((g*poly1d([1,0,0,0])).coeffs)]\n", "M=len(G[-1])\n", "for gg in G:\n", " while len(gg)