#!/usr/bin/env perl

## T network design in perl
## Refer to Fig. 17.63 in the 1997 ARRL handbook for more info

## This file Copyright 2000 <contact@gbppr.org> under the GPL
## NO WARRANTY. Please send bug reports / patches / reports.

use Math::Complex;

sub Tnet {
print "\33[H\33[J".
      "          |                                                     \n".
      "         ( ) RFC                                                \n".
      "         ( )                                                    \n".
      "         ( )   Rs = Output series impedance                     \n".
      "          |                                                     \n".
      "          |                L1             L2                    \n".
      "      Co  |                                                     \n".
      "          +-------| |------()()()()---+---()()()()----->  Rload \n".
      "         /                            |                         \n".
      "   Ba  |/                             |                         \n".
      "-------|                              |                         \n".
      "       |\\                             -  C1                    \n".
      "         |                            -                         \n".
      "     Em  |                            |                         \n".
      "         +----------------------------+----------------> Ground \n\n\n";
}             

print "\n\t\tT RF Network Design\n\n";

while (!$vcc) {
	print "Enter Vcc (in volts): ";
	chomp($vcc = <STDIN>);
	$vcc =~ tr/0-9.//csd;
}

print "\n";

while (!$po) {
	print "Enter Pout (in watts): ";
	chomp($po = <STDIN>);
	$po =~ tr/0-9.//csd;
}

print "\n";

while (!$cout) {
	print "Enter Cout (in picofarads): ";
	chomp($cout = <STDIN>);
	$cout =~ tr/0-9.//csd;
}

print "\n";

while (!$freq) {
	print "Enter frequency (in MHz): ";
	chomp($freq = <STDIN>);
	$freq =~ tr/0-9.//csd;
}

print "\n";

while (!$rl) {
	print "Enter load impedence (in ohms): ";
	chomp($rl = <STDIN>);
	$rl =~ tr/0-9.//csd;
}

print "\n";

while (!$qi) {
	# use low Q in solid state circuits (4-10)
	print "Enter desired Q: ";
	chomp($qi = <STDIN>);
	$qi =~ tr/0-9.//csd;
}

$rout = ($vcc * $vcc) / (2 * $po);
$xcout = 1 / (2 * pi * ($freq * 1000000) * ($cout / 1000000000000));
$rs = $rout / (1 + (($rout / $xcout) ** 2));
$xcs = $rs * ($rout / $xcout);
$xl1 = ($rs * $qi) + $xcs;
$l1 = $xl1 / (2 * pi * $freq);
$rv = (($qi ** 2) + 1) * $rs;
$ql = sqrt (($rv / $rl) - 1);
$xl2 = $rl * $ql;
$l2 = $xl2 / (2 * pi * $freq);
$xc1 = $rv / ($qi + $ql);
$c1 = 1 / (2 * pi * $freq * $xc1);
$rfc = ((4 * $rout) / (2 * pi * ($freq * 1000000))) * 1000000;

&Tnet;

printf " Frequency : %s MHz\n".
       "  R source : %.3f ohms\n".
       "    R load : %s ohms\n\n".
       "        L1 : %.6f uH\n".
       "        L2 : %.6f uH\n", $freq, $rs, $rl, $l1, $l2;
printf "        C1 : %.6f uF or %.2f pF\n".
       "       RFC : %.6f uH\n\n\n", $c1, $c1 * 1000000, $rfc;
