#!/usr/bin/env perl

## Mini-Circuits MAR-x PreAmp with a 3 pole input bandpass filter

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

use Math::Complex;

sub MMIC {
print "\33[H\33[J".
"                                                        Rbias              \n".
"                                                    +---/\\/\\/--+---> Vcc \n".
"                L2      C2              Cblock1     |          |           \n".
"Input >---+--+--()()()--| |--+--+-------+          ( ) RFC     -   0.01 uF \n".
"          |  |               |  |       |   |\\     ( )         -          \n".
"          |  |               |  |       -   | \\     |          G          \n".
"         ( ) |               | ( )      -   |  \\    |                     \n".
"      L1 ( ) -  C1       C3  - ( )      |   |  /----+-----| |------> Output\n".
"         ( ) -               - ( )      +---| /  3         Cblock2         \n".
"          |  |               |  |         1 |/                             \n".
"          |  |               |  | L3           MAR-x                       \n".
"          G  G               G  G                                          \n".
"                                       MAR-x pins 2 & 4 are ground       \n\n";
}

print "\n\nMini-Circuits MAR-x pre-amp with a 3 pole input bandpass designer\n\n";

while (!$mar) {
        print "Enter the Mini-Circuit's MAR device number (1-8): ";
        chomp($mar = <STDIN>);
        $mar =~ tr/1-8//csd;
}

if ($mar == 1) {
	$fl = 0;
	$fu = 1000;
	$db1 = 18.5;
	$db2 = 17.5;
	$db3 = 15.5;
	$db4 = 0;
	$dbmin = 13.0;
	$maxpo = 1.5;
	$maxpi = 13.0;
	$nfdb = 5.5;
	$ip3 = 14.0;
	$swri = 1.3;
	$swro = 1.3;
	$imax = 40.0;
	$pmax = 200.0;
	$dcim = 17.0;
	$dcv = 5.0;
}

elsif ($mar == 2) {
        $fl = 0;
        $fu = 2000;
        $db1 = 12.5;
        $db2 = 12.3;
        $db3 = 12.0;
        $db4 = 11.0;
        $dbmin = 8.5;
        $maxpo = 4.5;
        $maxpi = 13.0;
        $nfdb = 6.5;
        $ip3 = 17.0;
        $swri = 1.3;
        $swro = 1.4;
        $imax = 60.0;
        $pmax = 325.0;
        $dcim = 25.0;
        $dcv = 5.0;
}

elsif ($mar == 3) {
        $fl = 0;
        $fu = 2000;
        $db1 = 12.5;
        $db2 = 12.2;
        $db3 = 12.0;
        $db4 = 10.5;
        $dbmin = 8.0;
        $maxpo = 10.0;
        $maxpi = 13.0;
        $nfdb = 6.0;
        $ip3 = 23.0;
        $swri = 1.5;
        $swro = 1.7;
        $imax = 70.0;
        $pmax = 400.0;
        $dcim = 35.0;
        $dcv = 5.0;
}

elsif ($mar == 4) {
        $fl = 0;
        $fu = 1000;
        $db1 = 8.3;
        $db2 = 8.2;
        $db3 = 8.0;
        $db4 = 0;
        $dbmin = 7.0;
        $maxpo = 12.5;
        $maxpi = 13.0;
        $nfdb = 6.5;
        $ip3 = 25.5;
        $swri = 1.6;
        $swro = 2.0;
        $imax = 85.0;
        $pmax = 600.0;
        $dcim = 50.0;
        $dcv = 5.25;
}

elsif ($mar == 6) {
        $fl = 0;
        $fu = 2000;
        $db1 = 20.0;
        $db2 = 18.5;
        $db3 = 16.0;
        $db4 = 11.0;
        $dbmin = 9.0;
        $maxpo = 2.0;
        $maxpi = 13.0;
        $nfdb = 3.0;
        $ip3 = 14.5;
        $swri = 1.5;
        $swro = 1.4;
        $imax = 50.0;
        $pmax = 200.0;
        $dcim = 16.0;
        $dcv = 3.5;
}

elsif ($mar == 7) {
        $fl = 0;
        $fu = 2000;
        $db1 = 13.5;
        $db2 = 13.1;
        $db3 = 12.5;
        $db4 = 11.0;
        $dbmin = 8.5;
        $maxpo = 5.5;
        $maxpi = 13.0;
        $nfdb = 5.0;
        $ip3 = 19.0;
        $swri = 1.4;
        $swro = 1.5;
        $imax = 60.0;
        $pmax = 275.0;
        $dcim = 22.0;
        $dcv = 4.0;
}

elsif ($mar == 8) {
        $fl = 0;
        $fu = 1000;
        $db1 = 32.5;
        $db2 = 28.0;
        $db3 = 22.5;
        $db4 = 0;
        $dbmin = 19.0;
        $maxpo = 12.5;
        $maxpi = 13.0;
        $nfdb = 3.3;
        $ip3 = 27.0;
        $swri = "Not 50 ohms"; 
        $swro = "Not 50 ohms";
        $imax = 65.0;
        $pmax = 500.0;
        $dcim = 36.0;
        $dcv = 7.8;
}

print "\n";

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

print "\n";

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

print "\n";

while (!$db) {
	print "Enter the 3 dB bandwidth (in MHz): ";
	chomp($db = <STDIN>);
	$db =~ tr/0-9.//csd;
}

print "\n";

while (!$imp) {
	print "Enter the terminating impedance (in ohms): ";
	chomp($imp = <STDIN>);
	$imp =~ tr/0-9.//csd;
}

$flim1 = ($db / 2) + $center;
$flim2 = abs (($db / 2) - $center);
$fo = sqrt($flim1 * $flim2);

$L = 2 * ($imp * (1 / ((2 * pi) * $db)));
$C = (1 / $imp) * (1 / ((2 * pi) * $db));
$L1 = 1 / (((2 * pi * $fo) ** 2) * $C);
$C1 = 1 / (((2 * pi * $fo) ** 2) * $L);

$rbias = ($vcc - $dcv) / ($dcim / 1000);
$rfc = ($imp * 10) / (2 * pi * ($center - ($db / 2)));

&MMIC;

printf " Input Filter Q : %.1f   Bandwidth: %s MHz   Center Frequency: %s Mhz\n\n", $fo / $db, $db, $center;
printf "                  L1, L3 : %.6f uH\n".
       "                      L2 : %.6f uH\n", $L1, $L;
printf "                  C1, C3 : %.6f uF or %.2f pF\n", $C, $C * 1000000;
printf "                      C2 : %.6f uF or %.2f pF\n", $C1, $C1 * 1000000;
print  "       Cblock1 & Cblock2 : between 100 and 1000 pF\n";
printf "                     RFC : %.6f uH\n".
       "                   Rbias : %.2f ohms\n\n", $rfc, $rbias;

print "Press ENTER to continue...";
chomp($_ = <STDIN>);

print "\n\nMAR-$mar Device Information\n\n".
      "  Lowest frequency : $fl MHz\n".
      " Highest frequency : $fu MHz\n".
      "   Gain at 100 MHz : $db1 dB\n".
      "   Gain at 500 MHz : $db2 dB\n".
      "  Gain at 1000 MHz : $db3 dB\n".
      "  Gain at 2000 MHz : $db4 dB\n".
      "      Minimum gain : $dbmin dB\n".
      "  Max power output : $maxpo dBm (1 dB compression)\n".
      "   Max power input : $maxpi dBm\n".
      "      Noise figure : $nfdb dB (typical)\n".
      "               IP3 : $ip3 dBm (typical)\n".
      "        Input VSWR : $swri : 1\n".
      "       Output VSWR : $swro : 1\n".
      "   Maximum current : $imax mA\n".
      "    Maxmimum power : $pmax mW\n".
      "   Working current : $dcim mA (at pin 3)\n".
      "   Working voltage : $dcv V (at pin 3)\n\n";
