{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 12 - Option 1: Viterbi\n", "#### Authors:\n", "\n", "v1.0 (2014 Fall) Rishi Sharma \\*\\*, Sahaana Suri \\*\\*, Paul Rigge \\*\\*, Kangwook Lee \\*\\*, Kannan Ramchandran \\*\\*
\n", "v1.1 Special thanks to David Tse and the teaching staff of EE178 at Stanford for their modifications to this lab
\n", "v1.2 (2015 Fall) Kabir Chandrasekher \\*, Max Kanwal \\*, Kangwook Lee \\*\\*, Kannan Ramchandran \\*\\*
\n", "v1.2 (2016 Spring) Kabir Chandrasekher, Tony Duan, David Marn, Ashvin Nair, Kangwook Lee, Kannan Ramchandran\n", "\n", "----------------------------------------------------------------------------------------------------------------------\n", "Alice and Bob are working together to bake some treats.\n", "Alice is in charge of sending Bob the recipe, and Bob is responsible for following directions.\n", "Bob is *extremely* literal- if an error in the recipe says to use 2000 eggs instead of 2, he will use 2000 eggs without a second thought.\n", "Alice is aware of Bob's lack of common sense, but she is also busy.\n", "She already has the recipe open on her phone, so she wants to send it via email (using wifi) to Bob.\n", "Unfortunately, Alice's evil next-door-neighboor Eve has her microwave running continuously at maximum power.\n", "Microwaves emit a lot of radiation around wifi's 2.4GHz channels, causing interference.\n", "This lab will explore different techniques for ensuring that Alice's message will make it to Bob uncorrupted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Preliminaries\n", "\n", "We assume that Alice's message is $N$ bits long with each bit iid Bernoulli(0.5).\n", "We model the channel as a [binary symmetric channel](http://en.wikipedia.org/wiki/Binary_symmetric_channel), as shown below.\n", "Each bit sent through the channel is flipped (independently) with probability p.\n", "We'll assume p=0.05, which is a fairly typical value for wireless communications.\n", "\n", "![Alt](http://upload.wikimedia.org/wikipedia/commons/8/8e/Binary_symmetric_channel_%28en%29.svg)\n", "\n", "We assume that Alice and Bob use some sort of message integrity check in their message (like a [CRC](http://en.wikipedia.org/wiki/Cyclic_redundancy_check)) - to keep things simple, let's assume Bob can detect any error with probability 1.\n", "\n", "We also assume that Bob sends Alice an ACK or NACK and assume that this message always succeeds.\n", "If Bob sends an ACK, Alice knows Bob got the message.\n", "If Bob sends a NACK, Alice tries to send again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convolutional Coding\n", "It should make intuitive sense that we can get to low probabilities of error if we increase the number of repetitions.\n", "However, increasing the number of repetitions lowers Alice's effective datarate.\n", "Alice's phone also has limited battery life, and sending lots of repetitions consumes a lot of energy.\n", "Is it possible to correct a lot of errors without having to send so many extra bits? \n", "\n", "Yes! The 802.11 standards for wifi use convolutional codes and LDPC codes to correct errors.\n", "Convolutional codes can be efficiently decoded using the Viterbi algorithm, so they will be the focus of this lab.\n", "\n", "\n", "The above picture is a block diagram for a simple convolutional encoder.\n", "The input message is treated as a stream of bits \n", "\n", "$$d_0, d_1, d_2, d_3 \\ldots$$\n", "\n", "The input is shifted through a series of delays - at time k, the input is $d_k$, the first delay element (the \"D\" on the left) contains $d_{k-1}$, and the last delay elements (the \"D\" on the right) contains $d_{k-2}$.\n", "In this example, each input bit produces two output bits - the first output computed by the top \"adder\" and the second output computed by the bottom \"adder\". We denote the output of the top adder as $u_k$ and the bottom adder as $v_k$. The equations for each are given by\n", "\n", "\\begin{align*}\n", "u_k &= d_k + d_{k-2} &\\text{mod } 2\\\\\n", "v_k &= d_k + d_{k-1} + d_{k-2} &\\text{mod } 2\n", "\\end{align*}\n", "\n", "We further define $d_{-2}= d_{-1}=0$ for initalization of the algorithm. The two outputs are interleaved into one bitstream so the output is $(u_0, v_0, u_1, v_1, u_2, v_2, ...)$.\n", "\n", "The first thing to note is that this is not actually all that different from repetition coding.\n", "Like repetition coding, we are adding redundancy by generating multiple output bits per input bit.\n", "However, unlike repetition coding, convolutional codes have *memory*.\n", "Each output bit is a function of multiple input bits.\n", "The idea is that if there is an error, you should be able to use the surrounding bit estimates to help you figure out what was actually sent.\n", "\n", "The figure below shows the state transition diagram corresponding to the example encoder above.\n", "Each transition is labelled $d_k/(u_k, v_k)$.\n", "The two bits inside the circle correspond to the *state*. In order to compute the next output we must have $d_{k-2},d_{k-1}$ so we represent our state to as two numbers $d_{k-2}d_{k-1}$ (of which there are 4 different combinations). \n", "\n", "As an example, consider the state $10$ and the transition $1/00$. We first identify $d_{k-2} = 1$ and $d_{k-1}=0$ from the state. From the transition information we identify $d_k = 1$, $u_k = 0$, and $v_k = 0$ (note that $u_k$ and $v_k$ need not be given as they are entirely determined by the other three variables). The new state is then $d_{k-1}d_k = 10$ which is consistent with the diagram.\n", "\n", "Be sure to convince yourself that the encoder above is equivalent to the state transition diagram below.\n", "\n", "\n", "\n", "This finite state machine (FSM) diagram uses the following convention: the bit left of the slash represents the input, while the bits right to the slash represent the output pair $(u,v)$.\n", "\n", "If we assume that the input bits are iid Bernoulli(0.5), this is a Markov chain with every state equally likely.\n", "We can run the Viterbi algorithm on our output bits (even after going through a noisy channel) to recover a good estimate of the input bits.\n", "\n", "We assume that we start at state 0.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q1 The Convolutional Encoder\n", "### (A) Implement the encoder described by the block diagram above. Your code will go in the encode function. \n", "*Hint*: Try to understand what the function apply_generator does. It is helpful to go through some examples by hand before you start coding." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Takes in d_{k},d_{k-1},d_{k-2} as the 'state' and computes the output (a 1 or 0) \n", "# by XORing the bits specified by generator. \n", "# The generator basically tells the function whether to compute u_k or v_k in the above block diagram\n", "def apply_generator(state, generator):\n", " return reduce(lambda x,y: x^y, map(lambda x: x[0]*x[1], zip(state,generator)), 0)\n", "\n", "example_generators=[[1,0,1], [1,1,1]] # Specifies the coefficients to be used in the output of our convolutional code\n", "\n", "# Given a bit stream, performs the operations in the block diagram above\n", "def encode(bits_in, generators=example_generators):\n", " # Your encoder here!\n", " \"\"\"\n", " >>> encode([1,1,0,1,0])\n", " [1, 1, 1, 0, 1, 0, 0, 0, 0, 1]\n", " >>> encode([0,1,0,1,0])\n", " [0, 0, 1, 1, 0, 1, 0, 0, 0, 1]\n", " \"\"\"\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# debug\n", "print(encode([1,1,0,1,0]))\n", "print(encode([0,1,0,1,0]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q2 The Viterbi Algorithm\n", "\n", "#### We would like to use Viterbi algorithm to maximize the following:\n", "\n", "$$\\arg \\max_{d_0,\\ldots,d_n} P\\bigl(Y^u_0 =a_0, Y_0^v = b_0,Y^u_1 =a_1, Y_1^v = b_1,\\ldots,Y^u_n =a_n, Y_n^v = b_n \\big{|} U_0=u_0, V_0 =v_0\\ldots, U_n = u_n, V_n=v_n\\bigr)$$\n", "#### where $(u_0,v_0,\\ldots,u_n,v_n)$ is the output stream corresponding to the input stream $(d_0,\\ldots,d_n)$.\n", "\n", "An implementation for a Viterbi decoder can now be constructed as follows:\n", "1. A state $s$ has a *path metric* $p_s$ that gives the number of observed bit errors associated with being in $s$ at a given time.\n", "2. A state $s$ and input bit $b$ have *branch metric* $b_{s,b}$ that compares the observed channel output to the expected output given that you were in state $s$ and had input bit $b$. The branch metric is the number of different bits between the observed and expected output (Hamming weight).\n", "3. If input $b$ has transitions from state $s$ to $s'$, we can compute an updated path metric as $p_s + b_{s,b}$.\n", "4. Each state $s'$ will have two incoming transitions, we select the minimum $p_s+b_{s,b}$ and call that our new path metric $p_s'$. This is called Add-Compare-Select.\n", "5. Traceback uses the decisions at each add-compare-select to reconstruct the input bit sequence.\n", "Starting at the ending state with the smallest path metric, traceback finds the predecessor based on the decision made by the add-compare-select unit.\n", "For example, if at the end, state zero has the lowest path metric, and the last add-compare-select for state zero chose the path_metric coming from state 2, we know that the last input bit was zero and the previous state was 2. This continues backwards until it reaches the beginning.\n", "\n", "[This link](http://home.netcom.com/~chip.f/viterbi/algrthms2.html) may be a helpful resource for understanding implementing the Viterbi algorithm for convolutional codes (note that what we are designing is called a hard decoder- soft decoders are a refinement that we won't worry about).\n", "\n", "We assume that we start and end in state 0.\n", "We end in state 0 by appending enough 0s to the end of our input to force us into 0.\n", "Our decoder relies on this by initializing all path metrics to a big number, except for 0 which we initialize to 0.\n", "### (A) In the space below, implement a viterbi decoder for our 4-state example code. \n", "*Hint*: You may find the function ham_dist defined below helpful." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Returns the hamming distance of two binary vectors (must be of array type)\n", "def ham_dist(vec1, vec2):\n", " return np.sum(np.logical_xor(vec1,vec2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Run the viterbi algorithm to return ML estimate of input bits\n", "def viterbi(bits_in):\n", " # Your implementation of the Viterbi Algorithm here\n", " pass\n", " \n", "# Starting at the final state which we know to have the shortest overall path, we work our way back through \n", "# the table to the start and tack on the bit that correspond to the state transition\n", "def traceback(tb, prev_state):\n", " # Your implementation of traceback here. You do not have to make this function, but it will make \n", " # implementation easier\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (B) The following code block defines two functions, one which converts strings to their binary ASCII representations, and the other that converts from bits to ASCII. Use your implementation of the Viterbi Algorithm to decode the message in 'secret.txt' (which has been encoded using the convolutional code described in this lab)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#thanks to http://stackoverflow.com/questions/10237926/convert-string-to-list-of-bits-and-viceversa\n", "def tobits(s):\n", " result = []\n", " for c in s:\n", " bits = bin(ord(c))[2:]\n", " bits = '00000000'[len(bits):] + bits\n", " result.extend([int(b) for b in bits])\n", " return result\n", "\n", "def frombits(bits):\n", " chars = []\n", " for b in range(len(bits) / 8):\n", " byte = bits[b*8:(b+1)*8]\n", " chars.append(chr(int(''.join([str(bit) for bit in byte]), 2)))\n", " return ''.join(chars)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'np' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mfin\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'secret.txt'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0msecret_bits\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfin\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m', '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0msecret_bits\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msecret_bits\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mfin\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mfrombits\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mviterbi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msecret_bits\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mNameError\u001b[0m: name 'np' is not defined" ] } ], "source": [ "fin = open('secret.txt')\n", "secret_bits = [int(i) for i in fin.read()[1:-2].split(', ')]\n", "secret_bits = np.array(secret_bits)\n", "fin.close()\n", "print frombits(viterbi(secret_bits))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q3. Empirical Bit Error Rate of Convolutional Coding\n", "### (A) So how does the convolutional code compare with simple repetition coding? We are going to plot the bit error rate. \n", "\n", "$$ BER =\\frac{\\textrm{Number of incorrectly decoded bits}}{\\textrm{Total number of bits}}$$ \n", "\n", "### for some different channel parameters. For $0.01\\le p \\le 0.1$ on the x-axis, plot the bit error rate on a log scale on the y-axis. Run *100 trials* for randomly generated 512-bit long inputs at each channel parameter (this might take a little while to run). \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "# Plot against these values of the crossover probability p\n", "ps = np.linspace(0.01, 0.1, 10)\n", "\n", "#your code here\n", "\n", "figure(figsize(9,6))\n", "semilogy(ps, error_prob,'o')\n", "\n", "plt.title('Bit Error Rate vs. Crossover Probability of BSC')\n", "plt.xlabel('Crossover Probability (p)')\n", "plt.ylabel('Estimated BER')\n", "\n", "plt.minorticks_on()\n", "plt.grid(b=True,which = 'both',color = 'b')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (B) Assume we are communicating over a binary symmetric channel with $p =0.05$. Using our repetition encoder, what is the smallest number of repetitions $r$ we would need in order to achieve a superior BER to our convolutional code? Suppose the coded symbols are sent over a 10MHz wireless channel at the rate of $10\\times10^6$ symbols/sec. Compare the data rate, in bits per second (bps), of the convolutional code and the repetition code for this value of $r$. Now suppose, $p=0.02$. What is the minimum $r$ we need to outperform convolutional encoding? How does the data rate compare now?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Your answer here" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }