#!/usr/local/bin/perl -w # trellis.p # # feb 2000 I changed the name from M to S for counting all paths # # writes latex files for trellises (noiseless channel coding) # # stdin: S, then an S*S connection matrix # stdout: coordinates of the trellis # file: picture of trellis section # # usage in /home/mackay/itp/noiseless # trellis.p tsfile=tex/tsfile1.tex < C1 # trellis.p tsfile=tex/tfile1.tex Nsections=7 dotrellis=1 < C1 # trellis.p tsfile=tex/tsfile101.tex < C101 # trellis.p tsfile=tex/tsfile111.tex < C111 # # or source makeall # $ctsize='\tiny' ; $writecounts = 0 ; $plainnodelabels=0 ; # whether to use 00,0,1,11 (0) or 0,1,2,3 (1) $countbox = '\makebox' ; $countboxsize = "(0,0)" ; $countboxheight=0; $countboxwidth=0; # $countbox = '\framebox' ; put this back to have all counts be boxed. $countboxsize = "(50,30)" ; $countboxheight=30; $countboxwidth=40; $writetotals = 0 ; $statelabels = 1 ; # don't have statelabels and counts at same time. $stateoffset = 30 ; $stateside = "[b]" ; $dolabels = 1 ; $labelcircles = 1 ; $coffset = 20 ; # how much the counts are offset $csize='\small\bf' ; # size and or font for the counts # $size="\small" ; $size="" ; $dotrellis = 0 ; $Nsections = 1 ; $unitlength = "0.015cm" ; $verbose = 0 ; $nudge = 2 ; # displacement for labels $vsep = 100 ; $radius = 26 ; # is this the diameter? $edgeextra = 6 ; # where(x) they really start, relative to edgestart #$edgelabelx = 20 ; #$edgelabely = 20 ; $ox = -25 ; $oy = -25 ; $arrow = 1 ; # whether to put arrows on edges. $tsfile="tex/tsfile.tex" ; # trellis section file # $tfile="tex/tfile.tex" ; # trellis file $matrixfile="tex/mfile.tex" ; # trellis matrix file eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $topclearance = - $oy ; if ( $writecounts ) { $oy -= $countboxheight ; } $edgestart = $radius * 11/26; # where(x) edges appear to come from (there is a blank start) # relative to origin of circle $hsep = $vsep + 2 * ($edgestart) ; $linelength = $vsep - 2 * $edgeextra ; $apparentlinelength = $vsep ; # ok, let's roll. @A = <> ; $S = $#A + 1 ; print STDERR "Read in $#A+1 rows for a $S * $S matrix\n" ; # if ( $#A < $S + 1 ) { print STDERR "not enough input\n" ; exit (0); } if ( $plainnodelabels ) { @labels=(0..$S-1) ; @startingcircles=(1,0,0,0,0,0,0,0,0,0,0); } else { if ( $S == 2 ) { @labels=(0,1) ; # labels for nodes (not to be confused with edges) @startingcircles=(1,0); # not used . see thisS } elsif ( $S == 4 ) { @labels=("00","0","1","11") ; @startingcircles=(1,0,0,0); } else { @labels=("a","b","c","d") ; @startingcircles=(1,0,0,0); } } # trellis section open ( O , "> $tsfile" ) ; print O "\%\n" ; print O '% trellis section written by trellis.p',"\n" ; print O "\%\n" ; print STDERR '\input{',$tsfile,"\}\n"; print O '\setlength{\unitlength}{',$unitlength,"\}\n"; $height = $vsep * ($S - 1 ) + (-$oy) + $topclearance + $stateoffset ; if ( $dotrellis && $writecounts ) { $height += $vsep ; } $width = $hsep * ($Nsections -1 ) + $vsep + 2 * (-$ox) ; if ( $writecounts ) { $width += $countboxwidth/2 - (-$ox) ; } print O '\begin{picture}(',"$width,$height)($ox,$oy)\n" ; $line= ($arrow)? "\\vector" : "\\line" ; $x = 0 ; $nsections = 1 ; while ( $nsections <= $Nsections ) { print O "\%\n" ; print O '% starting circles',"\n" ; print O "\%\n" ; if ( $nsections == 1 && $dotrellis ) { # do special leading zero-time state $thisS = 1 ; for ( $s = 0 ; $s <= $S ; $s ++ ) { $count[$s] = 0 ; $oldc[$s] = 0 ; # counts of paths } $oldc[0] = 1 ; } else { $thisS = $S ; } # thisS controls how many circles are drawn and where the edges come from &docircles ; print O "\%\n" ; print O '% lines',"\n" ; print O "\%\n" ; # matrix goes this way: # 1 0 # 1 # 0 for ( $s = 0 ; $s <= $S-1 ; $s ++ ) { $sto = $s ; $yto = $s * $vsep ; $xto = $x + $hsep - $edgestart ; $row = ($S-1) - $s ; @c = split ( '\s+' , $A[$row] ) ; $xfrom = $x + $edgestart ; # where edges appear to emanate from if ( $#c < $#A ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } # to is row, from is col else { for ( $sfrom = 0 ; $sfrom <= $thisS-1 ; $sfrom ++ ) { if ( $c[$S-1-$sfrom] > 0 ) { $yfrom = $sfrom * $vsep ; $symbol = $c[$S-1-$sfrom] % 10 ; # store the symbol in the second digit $diff = $s - $sfrom ; if ( $diff == 0 ) { # horizontal line $labeloffset = $apparentlinelength/2 ; if ( $s > 0 ) { $boxlocn = "[b]" ; $nudgey = $nudge ; } else { $boxlocn = "[t]" ; $nudgey = - $nudge ; } } elsif ( $diff > 0 ) { # slope up $boxlocn = "[br]" ; if ( $diff ==1 ) { $labeloffset = $apparentlinelength*3/4 ; } else { $labeloffset = $apparentlinelength*1/2 ; } $nudgey = $nudge ; } elsif ( $diff < 0 ) { # slope down $boxlocn = "[bl]" ; $nudgey = $nudge ; $labeloffset = $apparentlinelength*1/4 ; } $labelx = $xfrom + $labeloffset ; $labely = $yfrom + $labeloffset * $diff + $nudgey ; $actualx = $xfrom + $edgeextra ; $h = $yfrom + $edgeextra * $diff ; print O "\\put($actualx,$h){$line(1,$diff){$linelength}}\n" ; if ( $dolabels ) { print O "\\put($labelx,$labely){\\makebox(0,0)$boxlocn\{\\tt\{$symbol}}}\n" ; } printf STDOUT "\# symbol=%d (%s->%s)\n" , $symbol , $sfrom , $s ; printf STDOUT "%8.1f %8.1f\n%8.1f %8.1f\n\n" , $xfrom , $yfrom , $xto , $yto ; # at this point we have finished writing the trellis # now we accumualte counts if ( $dotrellis ) { $count[$sto] += $oldc[$sfrom] ; print STDERR "count[$sto] incr by $oldc[$sfrom] to $count[$sto]\n" if ($verbose > 1) ; } } } } } # at this point we have finished writing the trellis # now we can write the counts IN THE NEXT COLUMN OVER $x += $hsep ; # and update the states for next time if ( $dotrellis ) { if ( $writecounts ) { $thisx = $x - $countboxwidth / 2 ; print O "\% section $nsections : cumulative counts\n" ; for ( $s = 0 ; $s <= $S-1 ; $s ++ ) { printf O "%% %5d %5d\n" , $s , $count[$s] ; $oldc[$s] = $count[$s] ; $count[$s] = 0 ; } $h = 0 ; $oldc[$S] = 0 ; # put totals in here for ( $l = 0 ; $l < $S ; $l ++ ) { if ( $countboxheight > 0 ) { $side = "" ; } else { $side = ($l >0 ) ? "[b]" : "[t]"; } $thish = ($l >0 ) ? $h + $coffset : $h - $coffset - $countboxheight ; if ( $oldc[$l] ) { # only mark the non-zero counts print O "\\put($thisx,$thish)\{$countbox$countboxsize$side\{$csize\{$oldc[$l]\}\}\}\n" ; $oldc[$S] += $oldc[$l] ; } $h += $vsep ; } } if ( $writetotals ) { # write grand total too. $l is now $S. $side = "[t]" ; $thish = $h ; $M = 'M_{'.$nsections.'}\!=\!' ; if ( $oldc[$l] ) { # only mark the non-zero counts print O "\%\n" ; print O '% total count',"\n" ; print O "\%\n" ; print O "\\put($x,$thish)\{\\makebox(0,0)$side\{$ctsize\{\$$M$oldc[$l]\$\}\}\}\n" ; } } } $nsections ++ ; } if ($thisS <= 1) {$thisS = 2 ; } # hack added to fix figure 11.4 (tstates1.tex) print O "\%\n" ; print O '% end circles',"\n" ; print O "\%\n" ; &docircles ; print O "\%\n" ; print O "\\end\{picture\}\n" ; print O "\%\n" ; close (O) ; open ( O , "> $matrixfile " ) ; print STDERR '\input{',$matrixfile,"\}\n"; # write the matrix print O "\\left[\n\\begin{array}{*{$S}{c}}\n" ; for ( $s = 0 ; $s <= $S-1 ; $s ++ ) { # $row = ($S-1) - $s ; $row = $s ; # this puts highest number highest @c = split ( '\s+' , $A[$row] ) ; if ( $#c < $#A ) { print STDERR "problem row $row : not enough stuff $#c\n" ; } # to is row, from is col else { for ( $sfrom = 0 ; $sfrom <= $S-1 ; $sfrom ++ ) { # if ( $c[$S-1-$sfrom] > 0 ) { if ( $c[$sfrom] > 0 ) { $edge = 1 ; } else { $edge = 0 ; } print O "$edge " ; if ( $sfrom < $S - 1 ) { print O "&" ; } else { print O "\\\\ \n" ; } } } } print O "\\end{array}\n" ; print O "\\right]\n" ; close (O) ; # end exit(0); if ( $dotrellis ) { open ( O , "> $tfile " ) ; print STDERR '\input{',$tfile,"\}\n"; close (O) ; } sub docircles { # also puts a label above them print O "\\multiput($x,0)(0,$vsep)\{$thisS\}",'{\circle{',$radius,"\}\}\n" ; if ( $labelcircles ) { print O "\%\n" ; print O '% labels for circles',"\n" ; print O "\%\n" ; $h=0 ; $l = 0 ; for ( ; $l < $thisS ; $l ++ ) { print O "\\put($x,$h)\{\\makebox(0,0){$size\{$labels[$l]\}\}\}\n" ; $h += $vsep ; } } if ( $statelabels && ( !($dotrellis) || # if we are doing a trellis we don't label the 1st ( $nsections > 1) ) ) { # column if ( $dotrellis ) { # label each state s_1 s_2 ... $number = $nsections - 1 ; } else { # label each state s_n s_n+1 $number = ( $nsections > 1) ? "n+1" : "n" ; } $statename= '$s_{'.$number.'}$' ; # seems that emacs doesn't parse this right $thish = ( $S - 1 ) * $vsep + $stateoffset ; print O "\%\n" ; print O '% state label for this column',"\n" ; print O "\%\n" ; print O "\\put($x,$thish)\{\\makebox(0,0)$stateside\{\{$statename\}\}\}\n" ; } }