by Neal R. Wagner
Copyright © 2001 by Neal R. Wagner. All rights reserved.
NOTE: This site is obsolete. See book draft (in PDF):
Here is the program to print an HTML table of channel capacities:
// Cap4.java: print table of capacities
// p: the channel probability for a binary symmetric channel
import java.text.DecimalFormat;
public class Cap4 {
static final int TABLE_SIZE = 20;
static DecimalFormat twoDigits = new DecimalFormat("0.00");
static DecimalFormat fifteenDigits = new DecimalFormat("0.000000000000000");
// main function to do calculation
public static void main (String[] args) {
double p; // channel probability
System.out.println("<table border>");
System.out.println("<tr><td><b>Probability</b></td>");
System.out.println("<td><b>Channel Capacity</b></td></tr>");
System.out.println("<tr><td></td><td></td></tr>");
for (int i = 0; i <= TABLE_SIZE/2; i++) {
p = (double)i/TABLE_SIZE;
System.out.print("<tr><td>" + twoDigits.format(p));
System.out.print(" or " + twoDigits.format(1-p));
System.out.println("</td><td>" +
fifteenDigits.format(capacity(p)) + "</td></tr>");
}
System.out.println("</table>");
} // end of main
// capacity: the capacity of the binary symmetric channel
private static double capacity(double p) {
if (p == 0 || p == 1) return 1;
return 1 + p*log2(p) + (1 - p)*log2(1 - p);
}
// log2: Logarithm base 2
public static double log2(double d) {
return Math.log(d)/Math.log(2.0);
}
}
Here is the output, as an HTML table:
<table border> <tr><td><b>Probability</b></td> <td><b>Channel Capacity</b></td></tr> <tr><td></td><td></td></tr> <tr><td>0.00 or 1.00</td><td>1.000000000000000</td></tr> <tr><td>0.05 or 0.95</td><td>0.713603042884044</td></tr> <tr><td>0.10 or 0.90</td><td>0.531004406410719</td></tr> <tr><td>0.15 or 0.85</td><td>0.390159695283600</td></tr> <tr><td>0.20 or 0.80</td><td>0.278071905112638</td></tr> <tr><td>0.25 or 0.75</td><td>0.188721875540867</td></tr> <tr><td>0.30 or 0.70</td><td>0.118709100769307</td></tr> <tr><td>0.35 or 0.65</td><td>0.065931944624509</td></tr> <tr><td>0.40 or 0.60</td><td>0.029049405545331</td></tr> <tr><td>0.45 or 0.55</td><td>0.007225546012192</td></tr> <tr><td>0.50 or 0.50</td><td>0.000000000000000</td></tr> </table>