#!/usr/local/bin/perl -w # # differs from say_pe I think in that it computes the berlekamp bit err prob. # # say_pe2.p # 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) # # where p is obtained from a gaussian channel with snr x, and # either (if b=0/1) corresponds to single bit error prob, # or, if b>1, we turn that p into a new p = 1 - (1-p)^b, corresponding # to bytes of b bits # # if $I > 1 then it also appends the prob of error of an interleaved system with I interleaves. # # example: say_pe2.p xfrom=0.6 xto=6.0 b=8 n=256 t=16 k=224 prefix="RS/" # writing to RS/256.224.0.88 # writing to gnu/textbook/pb.E.R0.88 # writing to gnu/textbook/pB.E.R0.88 # writing to gnu/textbook/pB.praw.R0.88 # # General: # # 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 $makeerf=0 ; # 1 REQUIRES erftail (linux/octave) to compute tail prob's. # 0 -> will read erf instead. Filename is erf/xmin.xmax.num $writeerf=1 ; # 1 -> if it makes erf it'll write em too. # say_pe2.p n=63 t=15 k=7 makeerf=1 # 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/" # say_pe2.p n=8191 t=94 k=6961 prefix="bch/" # from rm codes, see ~/_doc/code/rm/all/0.050 # and ~/_doc/code/rm/all/0.050 # for examples of n,k,t $b = 0 ; # change this to 4 or 8 for bytes of 4 or 8 bits. $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 ; $I = 1 ; # number of interleaves. # How to override min and max values with command line eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=([^\s]+)/ && shift; $erffile = "erf/$xfrom.$xto.$num" ; if ( $makeerf ) { # create list of tail areas using erftail if ( $num == 1 ) { $xfrom = $x ; } else { $xfactor=exp( (log ( $xto / $xfrom ) )/ ($num-1.0) ) ; } open ( EF , "> $erffile" ) if ( $writeerf ) ; $x = $xfrom ; for ( $a = 1 ; $a <= $num ; $a++ , $x *= $xfactor ) { $x = sprintf("%10.4g",$x) ; # 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 ) ; $xl[$a] = $x ; $pl[$a] = $p ; print EF "$x\t$p\n" if ( $writeerf ) ; } close ( EF ) if ( $writeerf ) ; } else { # read in the erfs from old file open ( E , "< $erffile" ) ; $a = 0 ; while ( ) { $a ++ ; ($x,$p) = split ; print "x = $x -> p_b(raw) = $p\n" if ( $v ) ; $xl[$a] = $x ; $pl[$a] = $p ; } close ( E ) ; if ( $a != $num ) { print STDERR "warning a not equal num: $a != $num \n" ; $num = $a ; } } $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 = "\t" ; } if ( $headings ) { printf O "\# Error-versus channel noise for (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)Brlkmp" ; } if ( $b > 1 ) { printf O "$spacer P_symbol(in)" ; printf O "$spacer My_P_e(bit_out)" ; } if ( $I > 1 ) { printf O "$spacer P(Interleave(%d)-err)",$I ; } printf O "\n" ; } $x = $xfrom ; for ( $a = 1 ; $a <= $num ; $a++ , $x *= $xfactor ) { $x = $xl[$a] ; $p = $pl[$a] ; $porig = $p ; # preserve the raw bit error prob. if ( $b > 1 ) { # turn this bit error probability into a byte prob. $p = 1.0 - (1.0 - $p)**$b ; } # $x = sprintf("%10.4g",$x) ; $ebno = $x * $x / ( 2.0 * $R ) ; $ebno = 10.0 * ( log ( $ebno ) / log (10.0) ) ; $ebno = sprintf("%10.4g",$ebno) ; $cum = 0.0 ; $Berlcum = 0.0 ; $q = 1.0 - $p ; $lp = &safelog ( $p ) ; $lq = log ( $q ) ; # $factor = 1.0 ; $lfactor = 0.0 ; for ( $i = $n , $j = 0 ; $i > $t ; $i-- , $j++ ) { $lpr = $lfactor + $i * $lp + $j * $lq ; $pr = exp ( $lpr ) ; # $pr = $factor * $p**$i * $q**$j ; # if ( $v ) { print "$lpr \t $pr\n" ; } if ( $pr =~ /NaN/ ) { print STDERR "Nan i=$i\t" ; } else { if ( $n > 1 ) { $Berlcum += $pr * $i / ( 2.0 * ($n-1.0) ) ; } $cum += $pr ; if ($v>5) { printf "%4d %4d %9.3g %9.4g %9.4g\n", $i , $j, $lfactor, $pr , $cum ; } } $lfactor += log( $i / ($j+1) ) ; } # if ( $n == 1 ) { $Berlcum = $cum ; } # n k t (p) (Bcum) cum rate(k/n) if ( 1 ) { printf O "%s ", $x ; } if ( 1 ) { printf O "$spacer %s ", $ebno ; } if ( $printp ) { printf O "$spacer %9.5g ", $porig ; } printf O "$spacer %9.5g ", $cum ; if ( $Berlekamp ) { printf O "$spacer %9.5g ", $Berlcum ; } if ( $b > 1 ) { # give extra info about the multiple bits in a byte case $tmpo = $cum * $porig ; printf O "$spacer %9.5g ", $p ; printf O "$spacer %9.5g ", ( $tmpo ) ; } if ( $I > 1 ) { $tmpi = 1.0 - ( 1.0 - $cum ) ** $I ; if ( $tmpi < $cum && ($cum<0.1) ) { # overflow error $tmpi = $I * $cum ; } printf O "$spacer %9.5g ", ( $tmpi ) ; } 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" ; } sub safelog { local($x)=@_; if ($x>0.0) { return log($x) ; } else { print STDERR "SL: can't take log of $x\n" ; return 0.0 ; } }