#!/usr/local/bin/perl -w # # REQUIRES erftail (linux/octave) # # say_pe2.p # say_pe2b.p is a working backup that can't cope with big blocks # see also _doc/code/say_pe.p # and _doc/code/perl.bch # and _doc/code/perl.tmp # # returns the prob of block error, i.e. # prob(more than t errors | n , p) # # This reads n,t,k from command line and writes a file # with # # x Eb/No p_bit(raw) p_block(out) and maybe Berlekamp's p_bit also. # # usage: # /home/mackay/code/EN: ~/_doc/code/perl.com0 # NB: all performance curves assume hard output from BSC from gaussian channel # say_pe2.p n=63 t=15 k=7 # to make repetition code: # say_pe2.p n=3 t=1 k=1 prefix="repetition/" # say_pe2.p n=5 t=2 k=1 prefix="repetition/" # H 7,4 code # say_pe2.p n=7 t=1 k=4 prefix="hamming/" # Golay codes # say_pe2.p n=23 t=3 k=12 prefix="golay/" # from rm codes, see ~/_doc/code/rm/all/0.050 # and ~/_doc/code/rm/all/0.050 # for examples of n,k,t $prefix="bch/" ; $n = 63 ; $k = 3 ; # only used to compute the rate $t = 5 ; $Berlekamp = 1 ; # set to 1 to get bit error probability computed using # the formula # p_e = sum_(j>r) (n choose j) p^j (1-p)^(n-j) TIMES (j/(2(n-1))) # $xfrom=0.3; $xto=3.0; $num=100; $printp=0 ; $brief = 0 ; $xfactor=1; $spacer="&"; $latex=0; $gnu = 1 ; # whether to write stuff to the gnu files $v = 0 ; # verbose $headings = 1 ; $printp = 1 ; # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=([^\s]+)/ && shift; $R = sprintf "%.5f", ($k*1.0/$n) ; $Rshort = sprintf "%.2f", ($k*1.0/$n) ; # $Rvshort = sprintf "%.1f", ($k*1.0/$n) ; $filename = "$prefix$n.$k.$Rshort" ; $filename =~ s/\s*//g ; print STDERR "# writing to $filename\n" ; open ( O , "> $filename" ) ; if ( !$latex ) { $spacer = " " ; } if ( $num == 1 ) { $xfrom = $x ; } else { $xfactor=exp( (log ( $xto / $xfrom ) )/ ($num-1.0) ) ; } if ( $headings ) { printf O "\# Error-versus channel noise for BCH (n=$n, k=$k, t=$t, R=$R) code \n" ; printf O "\# Gaussian channel (snr x) -> CLIP -> binary symmetric channel (Raw bit P)\n" ; printf O "\# P_e(bit) depends on a formula whose validity I am not sure of \n" ; if ( !$brief ) { printf O "\# x $spacer (Eb/No)/dB " ; } if ( $printp ) { printf O " $spacer Raw bit P " ; } if ( 1 ) { printf O " $spacer P_e(block) " ; } if ( $Berlekamp ) { printf O " $spacer P_e(bit) " ; } printf O "\n" ; } $x = $xfrom ; for ( $a = 1 ; $a <= $num ; $a++ , $x *= $xfactor ) { $x = sprintf("%10.4g",$x) ; $ebno = $x * $x / ( 2.0 * $R ) ; $ebno = 10.0 * ( log ( $ebno ) / log (10.0) ) ; $ebno = sprintf("%10.4g",$ebno) ; # get p from x $command = "cd /home/mackay/itp/octave ; erftail $x" ; open ( E , "$command |" ) ; $_ = ; $p = $_ ; print "x = $x -> p_b(raw) = $p\n" if ( $v ) ; $cum = 0.0 ; $Berlcum = 0.0 ; $q = 1.0 - $p ; $factor = 1.0 ; for ( $i = $n , $j = 0 ; $i > $t ; $i-- , $j++ ) { $pr = $factor * $p**$i * $q**$j ; if ( $pr =~ /NaN/ ) { print STDERR "Nan i=$i\t" ; } else { if ( $n > 1 ) { $Berlcum += $pr * $i / ( 2.0 * ($n-1) ) ; } $cum += $pr ; if ($v>5) { printf "%4d %4d %9.3g %9.4g %9.4g\n", $i , $j, $factor, $pr , $cum ; } } $factor *= $i / ($j+1) ; } # if ( $n == 1 ) { $Berlcum = $cum ; } # n k t (p) (Bcum) cum rate(k/n) if ( 1 ) { printf O "$spacer %s ", $x ; } if ( 1 ) { printf O "$spacer %s ", $ebno ; } if ( $printp ) { printf O "$spacer %9.5g ", $p ; } printf O "$spacer %13.4g ", $cum ; if ( $Berlekamp ) { printf O "$spacer %9.5g ", $Berlcum ; } print O "\\\\ " if ( $latex ) ; print O " \n" ; } if ( $gnu ) { print STDERR "writing to gnu/textbook/pb.E.R$Rshort\n" ; print STDERR "writing to gnu/textbook/pB.E.R$Rshort\n" ; print STDERR "writing to gnu/textbook/pB.praw.R$Rshort\n" ; open ( G , ">> gnu/textbook/pb.E.R$Rshort" ) ; print G "# pbit(?) vs ebno " ; print G "# gnuplot\n" ; print G "plot '$filename' u 2:5 t '$n,$k' w l\n" ; close(G) ; open ( G , ">> gnu/textbook/pB.E.R$Rshort" ) ; print G "# pblock vs ebno " ; print G "# gnuplot\n" ; print G "plot '$filename' u 2:4 t '$n,$k' w l\n" ; close(G) ; open ( G , ">> gnu/textbook/pB.praw.R$Rshort" ) ; print G "# pblock vs p_raw " ; print G "# gnuplot\n" ; print G "plot '$filename' u 3:4 t '$n,$k' w l\n" ; }