#!/usr/local/bin/perl -w # reads x, K and N from command line and returns Eb/No # and the distance from the shannon limit for the rate in use. # also reads r and n from command line and returns the Bayesian # confidence interval for bernoulli "p" # # Gaussian channel has outputs +/- x plus noise of s.d. sigma=1 # Code has rate K/N # In n trials, there are r failures. # # the failure rate p is estimated on the assumption that # r had a bernoulli distribution. # # the probs with eb's go to 11:12:13. The log probs are 14:15:16 (sd is 17) # the logits go in 18:19:20 $pr_latex = 0 ; $verbose = 0 ; $latex_printlower = 0 ; $lowerbound = 1e-06; $z_score = 2.0 ; $r=-1; $n=-1; $K=-1; $N=-1; $x=-1; $sigma = 1.0; $logs=1; $logits=1; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; if ( $x < 0 || $K < 0 || $N < 1 || $r < 0 || $n < 1) { print STDERR "You must supply x, K and N, r and n\n" ; &print_usage (); exit(0); } sub print_usage { print STDERR " bernebno.p K=4544 N=14544 x=0.9 r=3 n=10\n" ; print STDERR " You may supply sigma also\n" ; } $R = $K / $N ; $ebno = $x * $x / ( 2.0 * $R * $sigma * $sigma ) ; $logEb = 10.0 * log ( $ebno ) / log (10.0) ; $C = 0.5 * log ( 1.0 + $x * $x / ( $sigma * $sigma ) ) / log ( 2.0 ) ; # limitting value of x^2/sigma^2 $x2s_lim = exp ( 2 * $R * log (2.0)) - 1 ; $ebno_lim = $x2s_lim / ( 2.0 * $R ) ; $logEbno_lim = 10.0 * log ( $ebno_lim ) / log (10.0) ; $d = $logEb - $logEbno_lim ; # distance from shannon limit for this rate if ( $verbose ) { printf "# x2s_lim = %9.4g\n" , $x2s_lim ; printf "# ebno_lim = %9.4g\n" , $ebno_lim ; printf "# logEbno_lim = %9.4g\n" , $logEbno_lim ; } print "# ebno (dB) distance C R x " ; print "# errors trials p_ML p_plaus point upper lower " ; if ( $logs) {print "#logs: point upper lower sd_log " ; } if ( $logits) {print "#logits: point upper lower " ; } print "\n"; printf "%9.4g %9.4g %9.4g %9.4g %9.4g %9.4g" , $ebno, $logEb , $d , $C , $R , $x ; # print "\n" ; $errors = $r ; $trials = $n ; # &find_ebs_and_print ( 1 ) ; sub find_ebs_and_print { # this routine looks at the global variables $errors and $trials # and computes a confidence interval and maximum likelihood value. # these are called $p_lower and $p_upper, and $p_point (ML value) # the size of the interval is dictated by $zscore, e.g. 2.0 # # [a value called $p_plaus is also computed, which is the point to plot # if one wants a plot which just shows point estimates. # this is not currently used in the paper, Sep 30 95] # # a bit error probability is also computed, $bitep, by looking at the # global variables $bitswrong and $FEN # local ( $arg ) = @_ ; # typesetting # $symbol = $arg ? $finalsymbol : $indivsymbol ; # $Neff = ( $errors == 0 ) ? 1 : $errors ; # make the point $p_plaus = $Neff / $trials ; # estimate $p_plaus # # Confidence interval # if ( $errors == 0 ) { $p_ml = 0.0 ; $p_point = $lowerbound ; $p_lower = $lowerbound ; $p_upper = 1.0 - exp( - 2.0 / $trials ) ; } else { $p_point = $errors / $trials ; $p_ml = $p_point ; # factor corresponding z sd's in log p $sd_logp = sqrt( ($trials - $errors ) / ( $errors * $trials ) ) ; $factor = exp ( $z_score * $sd_logp ) ; # this is a lot like sqrt( $errors ) $p_upper = $p_point * $factor ; $p_lower = $p_point / $factor ; } # # end of confidence interval bit # # p_ML is the true ML estimate # p_plaus differs by replacing 0 errors by 1 error # p_point is p_ML, except "lowerbound" is used instead of zero. # similarly p_lower is the lower error bar, with lowerbound replacing zero # I think the most useful pair of numbers are p_ML and p_upper. printf "%6s %6s " , $errors, $trials ; printf "%8.4g %8.4g %8.4g %8.4g %8.4g ", $p_ml , $p_plaus, $p_point , $p_upper , $p_lower ; if ($logs) { printf "%8.4g %8.4g %8.4g %8.4g ", log( $p_point ) , log ( $p_upper ) , log ( $p_lower ) , $sd_logp ; } if ($logits) { printf "%8.4g %8.4g %8.4g ", &logit( $p_point ) , &logit ( $p_upper ) , &logit ( $p_lower ) ; } print "\n" ; if($pr_latex){ if ( $arg == 1 ) { # print some latex stuff too print "% errors trials p_ML p^+ (p_-)\n" ; printf "%4s & %6s " , $errors, $trials ; printf "& %8.3g & %8.3g " , $p_ml , $p_upper ; if ( $latex_printlower ) { printf "& %8.3g ", $p_lower ; } print "\n" ; print "\\\\ \n" ; } } } sub logit { local ( $ppp ) = @_ ; if (( $ppp > 0.0) && ( 1.0 - $ppp > 0.0)) { return ( log ( ( $ppp ) / ( 1.0 - $ppp ) ) ) ; } else { return 1.0 ; } }