#!/usr/local/bin/perl -w # # say_peQ.p # see also _doc/code/say_pe2.p # # returns the prob of block error for a (n,k) code # with symbols of size b bits when the channel is a q-ary # symmetric channel, where q <-> c bits (q=2^c) # prob(more than t errors | n , p) # # 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_peQ.p n=137 t=4 k=129 I=4 # say_peQ.p n=252 t=14 k=224 I=2 $b = 8 ; # change this to 4 or 8 for bytes of 4 or 8 bits. $c = 4 ; # channel symbol size $prefix="RS/" ; $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))) # $pfrom=1e-10; $pto=0.2; $p=0.1; $num=100; $printp=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; $symbols = $b / $c ; print STDERR "$symbols channel symbols per code symbol\n" ; $Q = 2**$c ; $R = sprintf "%.5f", ($k*1.0/$n) ; $Rshort = sprintf "%.2f", ($k*1.0/$n) ; # $Rvshort = sprintf "%.1f", ($k*1.0/$n) ; $filename = "QSC$Q/$prefix$n.$k.$Rshort" ; $filename =~ s/\s*//g ; print STDERR "# writing to $filename\n" ; open ( O , "> $filename" ) ; if ( !$latex ) { $spacer = "\t" ; } if ( $headings ) { print O "\# Error-versus channel noise for (n=$n, k=$k, t=$t, R=$R) code (symbols $b bits)\n\# $symbols channel symbols per code symbol\n" ; print O "\# Q=$Q-ary symmetric channel $c bits\n" ; printf O "\# P_e(bit) depends on a formula whose validity I am not sure of \n" ; if ( 1 ) { printf O "\# " ; } if ( $printp ) { printf O "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" ; } $porig = $pfrom ; if ( $num == 1 ) { $pfrom = $p ; } else { $xfactor=exp( (log ( $pto / $pfrom ) )/ ($num-1.0) ) ; } for ( $a = 1 ; $a <= $num ; $a++ , $porig *= $xfactor ) { $p = $porig ; # preserve the raw bit error prob. if ( $symbols > 1 ) { # turn this bit error probability into a byte prob. $p = 1.0 - (1.0 - $p)**$symbols ; } $cum = 0.0 ; $Berlcum = 0.0 ; $q = 1.0 - $p ; $lp = log ( $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 ( $printp ) { printf O "%9.5g ", $porig ; } printf O "$spacer %9.5g ", $cum ; if ( $Berlekamp ) { printf O "$spacer %9.5g ", $Berlcum ; } if ( $symbols > 1 ) { # give extra info about the multiple bits in a byte case $tmpo = $cum * $porig * 0.5 ; # multiply by 0.5 since the original symbols are not bits. (dodgy?) printf O "$spacer %9.5g ", $p ; # code symbol error probability 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.praw.R$Rshort\n" ; open ( G , ">> gnu/textbook/pB.praw.R$Rshort" ) ; print G "# pblock vs p_raw " ; print G "# gnuplot\n" ; print G "plot '$filename' u 1:2 t '$n,$k' w l\n" ; }