#!/usr/local/bin/perl -w # TCC.p # Reads TCC spec file containing # trellis specs # construction matrix # and writes a postscript representation of the # connectivity . And spits out a connectivity matrix. # # TCC.p o=ps/TX1.ps s/TX1 # TCC.p o=ps/TX2.ps s/TX2 # TCC.p o=ps/T.ps s/T ; gv -geometry +10+10 ps/T.ps # TCC.p o=ps/T3.ps s/T3 ; gv -geometry +10+10 ps/T3.ps $trellisheight = 20 ; $trellisboxheight = 15 ; $radius = 5 ; $bitfactor = 12 ; $yfactor = 160 ; $bity = $yfactor ; $verbose = 0 ; $background=1; # colour of surrounding box $o="gh.ps"; $ytop=600; # set nonzero to cause y to be measured from the top instead of # the bottom $rotate=0; # bl corner $x0=40; $y0=100; $stroke = "stroke"; # change this to "stroke" to get lines round all boxes. eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; print STDERR "===== TCC.p ===== (c) DJCM july 1998 =====\n" ; # # read the spec file # $status = 0 ; $totchunks = 0 ; # read TCC spec file $t = 0 ; $T = 0 ; # counting trellises $_ = <> ; BLOOP: while(<>) { s/^\s*// ; if (/^\#/) { # print STDERR ; next ; } # s/\#.*// ; # # if (/T\s*=\s*(.+)\s*/) { $T = $1 ; # print " Number of trellises T = $T\n" ; } elsif (/K\s*=\s*([^\s]+)\s+([^\s]+)\s+/) { $t ++ ; if ( $t > $T ) { print STDERR "warning, trellis number $t is too big for T=$T\n" ; } # $Kt[$t] = $1 ; $chunks[$t] = $2 ; print " reading in number of bits in each of $2 chunks in trellis $t\n" ; for ( $c = 1; $c <= $chunks[$t] ; $c ++ , $totchunks ++ ) { $thestring = "$t.$c" ; do { $_ = <> ; s/^\s*// ; } while (/^\#/) ; s/\#.*// ; # s/\n// ; if ( s/permute// ) { $permute{$thestring} = 1 ; } else { $permute{$thestring} = 0 ; } if ( s/puncture// ) { $puncture{$thestring} = 1 ; } else { $puncture{$thestring} = 0 ; } $edges{$thestring} = int($_) ; print " trellis $t chunk $c : $edges{$thestring} " ; print "(permute=$permute{$thestring})\n" ; } } if ( $T && $t == $T ) { last BLOOP ; } } # print "finished reading trellis spec, now looking for construction matrix\n" ; # make information about all the trellises $thewidth = 0 ; for ( $t = 1 ; $t <= $T ; $t ++ ) { $theoffset=0; for ( $c = 1; $c <= $chunks[$t] ; $c ++ ) { $thestring = "$t.$c" ; # make a little list of integers for the codeword bits to queue for. if ( $permute{$thestring} ) { $queue{$thestring} = "" ; $w = $edges{$thestring} ; for ( $q = 1 ; $q <= $w ; $q ++ ) { $qs[$q-1] = $q ; } for ( $nl = 1 ; $nl <= $w ; $nl ++ ) { $from = $nl ; $q = int ( rand($#qs+1) ) ; $too = splice ( @qs , $q , 1 ) ; $queue{$thestring} .= "$too." ; } } elsif ( $puncture{$thestring} ) { $qa= "" ; $w = $edges{$thestring} ; $cc = 1 ; for ( $q = 1 ; $q <= $w ; $q ++ ) { $qa .= $cc."." ; $cc += 2 ; if ( $cc > $w ) { $cc = 2 ; } } for ( $q = 1 ; $q <= $w ; $q ++ ) { $qs[$q-1] = $q ; } $queue{$thestring} = $qa ; } else { $queue{$thestring} = join('.', 1..$edges{$thestring}) . "." ; } print " queue for trellis $t chunk $c : \n$queue{$thestring}\n" ; $edgenum{$thestring} = 0 ; $offset{$thestring} = $theoffset ; $theoffset += $edges{$thestring} ; } $width[$t] = $theoffset ; if ( $thewidth < $theoffset ) { $thewidth = $theoffset ; } } # ok, now we read the construction and write the ps at the same time. $N = 0 ; for ( $t = 1 ; $t <= $T ; $t ++ ) { $edgenum{$t} = 0 ; for ( $c = 1; $c <= $chunks[$t] ; $c ++ ) { $thestring = "$t.$c" ; do { $_ = <> ; s/^\s*// ; } while (/^\#/) ; s/\#.*// ; # s/\n// ; @construction = split(' ') ; if ( !$N ) { $N = $#construction+1 ; if ( $thewidth < $N ) { $thewidth = $N ; } &postscriptstart($thewidth,$T) ; &makeboxes ; } else { if (!( $N == $#construction+1) ) { print STDERR "warning N ($N) != $#construction+1\n" ; } } print "number in this lot = $N\n" ; for ( $n = 0 ; $n <= $#construction ; $n ++ ) { for ( ; $construction[$n] > 0 ; $construction[$n] -- ) { if ( $queue{$thestring} =~ s/([^\.]+)\.// ) { $whoto = $1 ; $edgenum{$thestring} ++ ; $edgenum{$t} ++ ; &addedge($t,$c,$edgenum{$thestring},$whoto,$n,$offset{$thestring}) ; } else { print STDERR "warning, trellis $t.$c has no more edges to give\n" ; } } } } } sub makeboxes { print OUT "%% nodes \n" ; $x = $x0 ; $y = $y0 + $yfactor ; for ( $n=0; $n < $N ; $n ++ ) { &circle( $x , $y , $radius , $radius ) ; $x += $bitfactor ; } print OUT "%% two trellises \n" ; $y = $y0 ; $x = $x0 + $bitfactor / 2 ; $sign = $trellisboxheight ; $delta = $trellisheight ; for ( local($t) = 1 ; $t <= $T ; $t ++ ) { &box( $x , $y , $width[$t]*$bitfactor , $sign , $background , $stroke) ; $xx = $x0 + $bitfactor ; $yy = $y + $delta ; for ( $n=0; $n < $width[$t] ; $n ++ ) { &line( $xx , $y + $sign , $xx , $yy ) ; $xx += $bitfactor ; } $y += 2 * $yfactor ; $sign *= -1 ; $delta *= -1 ; } } sub addedge { local($t,$c,$e,$w,$n,$o) = @_ ; print "joining $t.$c edge number $w to bit number $n+1\n" ; $startx = $bitfactor * $n ; $starty = $bity ; $newy = (2*$t-2) * ( $yfactor ) - (2*$t-3)* $trellisheight ; $newx = ($o + $w) * $bitfactor ; &line($x0+$startx ,$y0+ $starty ,$x0+ $newx ,$y0+ $newy ) ; } &endpostscript ; # # Rate computations: # # find out how many constraints each trellis is imposing. $totnumconstraints = 0 ; for ( $t = 1 ; $t <= $T ; $t ++ ) { $num_constraints[$t] = $edgenum{$t} - $Kt[$t] ; if ( $num_constraints[$t] <= 0 ) { print "WARNING: trellis $t has num_constraints = $num_constraints[$t] <= 0\n" ; } $totnumconstraints += $num_constraints[$t] ; } $trueK = $N - $totnumconstraints ; $R = $trueK/$N ; printf "This code has N = %d , M = %d , K = %d\n", $N , $totnumconstraints , $trueK ; printf "This code has rate %10.5g\n", $R ; exit(0); sub postscriptstart { local ( $N,$T ) = @_ ; $height = $yfactor * 2 ; $width = $bitfactor * ($N+1) ; open ( OUT , ">$o" ) ; print OUT "%!\n" ; print OUT "%%Title: none\n" ; print OUT "%%Creator: TCC.p (DJCM 9806)\n" ; print OUT "%%Pages: 1\n" ; print OUT "%%BoundingBox:" ; if ( $ytop ) { if( $rotate == 1 ) { # this is not right printf OUT " %d %d %d %d\n" , $y0, $x0 , $y0 + $height , $x0 + $width ; } else { printf OUT " %d %d %d %d\n" , $x0 ,$ytop-$y0-$height , $x0 + $width ,$ytop-$y0 ; } } else { if( $rotate == 1 ) { printf OUT " %d %d %d %d\n" , $y0, $x0 , $y0 + $height , $x0 + $width ; } else { printf OUT " %d %d %d %d\n" , $x0 ,$y0 , $x0 + $width ,$y0 + $height ; } } print OUT "%%EndComments\n" ; print OUT "/\$F2psDict 32 dict def \n" ; print OUT "\$F2psDict begin\n" ; print OUT " \$F2psDict /mtrx matrix put\n" ; print OUT "\n" ; print OUT " end\n" ; print OUT " /\$F2psBegin {\$F2psDict begin /\$F2psEnteredState save def} def\n" ; print OUT " /\$F2psEnd {\$F2psEnteredState restore end} def\n" ; print OUT " %%EndProlog\n" ; print OUT "\n" ; print OUT "\$F2psBegin\n" ; print OUT "1 setlinecap 1 setlinejoin\n" ; print OUT "0.10 setlinewidth\n" ; print OUT '%%% ellipse stuff %% Blue Book Program 3, on page 137 %%Creator: Adobe Systems Incorporated %%CreationDate: Thu Dec 28 13:17:45 PST 1989 %%EndComments /ellipsedict 8 dict def ellipsedict /mtrx matrix put /ellipse { ellipsedict begin /endangle exch def /startangle exch def /yrad exch def /xrad exch def /y exch def /x exch def /savematrix mtrx currentmatrix def x y translate xrad yrad scale 0 0 1 startangle endangle arc savematrix setmatrix end } def %% %% ' ; # print OUT "% background boxes\n" ; $x = $x0 ; $y = $y0 ; # &box ( $x , $y , # $width , $height , $background , "stroke") ; } # circle plus arrow sub circle_n_arrow { &circle($x0+$w/2+$n , $y0+$w/2+$m , $w/$cshrink ,$w/$cshrink ) ; # add a tiny arrow thing too $newox = $x0+$w/2+$n; $newoy = $y0+$w/2+$m - 0.5* $w/$cshrink ; &line($newox - $arrowsize , $newoy - $arrowsize , $newox , $newoy ) ; &line($newox - $arrowsize , $newoy + $arrowsize , $newox , $newoy ) ; } sub endpostscript { print OUT "\$F2psEnd\nshowpage\n" ; } sub showstring { # this is specialized for showing single digits local($y,$x,$str,$w) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; } if($ytop) { $y = $ytop - $y ; } printf OUT "/Helvetica findfont %d scalefont setfont\n" , $w ; printf OUT "%6.4f %6.4f moveto\n" , $x-$w/4, $y-$w/3 ; printf OUT "(%s) show\n", $str ; } sub show3string { # this is specialized for showing 3 digits local($y,$x,$str,$w) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; } if($ytop) { $y = $ytop - $y ; } printf OUT "/Helvetica findfont %d scalefont setfont\n" , $w ; printf OUT "%6.4f %6.4f moveto\n" , $x-3*$w/4, $y-$w/3 ; printf OUT "(%s) show\n", $str ; } sub show_bl_string { # puts it bottom left aligned local($y,$x,$str,$w) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; } if($ytop) { $y = $ytop - $y ; } printf OUT "/Helvetica findfont %d scalefont setfont\n" , $w ; printf OUT "%6.4f %6.4f moveto\n" , $x, $y ; printf OUT "(%s) show\n", $str ; } sub box { # draws a box from x,y with width specified. # to get a box with an edge, set $stroke=stroke ; #usage ( $x , $y , $xsize , $ysize, $gray , $stroke ) local( $y , $x , $ysize, $xsize, $gray , $stroke ) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; $tmpo = $xsize ; $xsize = $ysize ; $ysize = $tmpo ; } if($ytop) { $y = $ytop - $y ; $ysize = -$ysize ; } printf OUT "%s box %6.4f %6.4f %6.4f %6.4f\n" , '%' , $x, $y, $xsize, $ysize; printf OUT "newpath %6.4f %6.4f moveto %6.4f %6.4f lineto %6.4f %6.4f lineto %6.4f %6.4f lineto closepath %s\n" , $x+$xsize, $y+$ysize, $x+$xsize, $y, $x, $y, $x, $y+$ysize, $stroke ; } sub circle { # draws a circle centred on x,y with diameter w specified. #usage ( $x , $y , $xsize , $ysize ) local( $y , $x , $ysize, $xsize ) = @_ ; local ( $stroke ) = "stroke" ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; $tmpo = $xsize ; $xsize = $ysize ; $ysize = $tmpo ; } if($ytop) { $y = $ytop - $y ; $ysize = -$ysize ; } printf OUT "%s circle/ellipse %6.4f %6.4f %6.4f %6.4f\n" , '%' , $x, $y, $xsize, $ysize; # newpath # 144 400 72 144 0 360 ellipse # stroke printf OUT "newpath %6.4f %6.4f %6.4f %6.4f %6.4f %6.4f ellipse %s\n" , $x, $y, $xsize*0.5, $ysize*0.5, 0,360, $stroke ; } sub cbox { # draws a box centred on x,y with half width specified. # to get a box with an edge, set $stroke=stroke ; #usage ( $x , $y , $xsize , $ysize, $gray ) local( $y , $x , $ysize, $xsize, $gray , $stroke ) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; $tmpo = $xsize ; $xsize = $ysize ; $ysize = $tmpo ; } printf OUT "newpath %6.4f %6.4f moveto %6.4f %6.4f lineto %6.4f %6.4f lineto %6.4f %6.4f lineto closepath gsave %6.4f setgray fill grestore %s\n" , $x+$xsize, $y+$ysize, $x+$xsize, $y-$ysize, $x-$xsize, $y-$ysize, $x-$xsize, $y+$ysize, $gray , $stroke ; } sub line { # draws a line #usage ( $x , $y , $x2 , $y2 ) ; local( $y , $x , $y2, $x2 ) = @_ ; if($rotate==0) { $tmpo = $x ; $x = $y ; $y = $tmpo ; $tmpo = $x2 ; $x2 = $y2 ; $y2 = $tmpo ; } if($ytop) { # ionvert verttically $y = $ytop - $y ; $y2 = $ytop - $y2 ; } printf OUT "newpath %6.4f %6.4f moveto %6.4f %6.4f lineto stroke\n" , $x,$y, $x2,$y2 ; }