#!/usr/bin/env perl

## Tuned LC calculator

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

use Math::Complex;

sub Ind {
undef $_;
while (!m/^([0-9.]+)(n|N|m|M|u|U+)$/) {
        print "\nn = nanohenries\n".
	      "u = microhenries\n".
	      "m = millihenries\n\n".
	      "Enter inductance  [value][n,u,m]: ";
	chomp($_ = <STDIN>);

if (m/^([0-9.]+)(n|N|m|M|u|U+)$/) {
        $l = $1;
        $unit = $2;
}
if ($unit =~ /n/i) {
        $l = $l / 1000000000;
}
elsif ($unit =~ /u/i) {
        $l = $l / 1000000;
}               
elsif ($unit =~ /m/i) {
        $l = $l / 1000;
  }
 }
}   

sub Cap {
undef $_;
while (!m/^([0-9.]+)(u|U|p|P|n|N+)$/) {
        print "\nu = microfarads\n".
	      "n = nanofarads\n".
	      "p = picofarads\n\n".
	      "Enter capacitance  [value][u,n,p]: ";
	chomp($_ = <STDIN>);

if (m/^([0-9.]+)(u|U|p|P|n|N+)$/) {
        $c = $1;
        $unit = $2;
}            
if ($unit =~ /u/i) {
        $c = $c / 1000000;
}
elsif ($unit =~ /n/i) {
        $c = $c / 1000000000;
}
elsif ($unit =~ /p/i) {
        $c = $c / 1000000000000;
  }
 }
}   

sub Freq {
undef $_;
while (!m/^([0-9.]+)(k|K|m|M|h|H+)$/) {
        print "\nk = kilohertz\n".
	      "m = megahertz\n".
	      "h = just hertz\n\n".
	      "Enter frequency  [value][k,m,h]: ";
	chomp($_ = <STDIN>);

if (m/^([0-9.]+)(k|K|m|M|h|H+)$/) {
        $frq = $1;
        $unit = $2;
}
if ($unit =~ /k/i) {
        $frq = $frq * 1000;
}                     
elsif ($unit =~ /m/i) {
        $frq = $frq * 1000000;
}
elsif ($unit =~ /h/i) {
        $frq = $frq;
  }
 }
}     

sub Print {
printf "\n\n   Frequency : %.6f MHz\n", $frq / 1000000;
printf "             : %.6f kHz\n", $frq / 1000;
printf "             : %.6f Hz\n\n", $frq;
printf " Capacitance : %.6f uF\n", $c * 1000000;
printf "             : %.6f pF\n\n", $c * 1000000000000;
printf "  Inductance : %.6f mH\n", $l * 1000;
printf "             : %.6f uH\n", $l * 1000000;
printf "             : %.6f nH\n\n", $l * 1000000000;
}

while (!$ans || $ans > 4 || $ans =~ m/[a-z]/i) {
	print "\33[H\33[J".
	      "Tuned LC circuit solver\n\n".
              "F  = Frequency in megahertz\n".
              "C  = Capacitance in picofarads\n".
              "L  = Inductance in microhenrys\n\n".
              "1) Calculate F  - given L and C\n".
              "2) Calculate L  - given F and C\n".
              "3) Calculate C  - given F and L\n".
	      "4) Calculate Z  - given L and Q\n\n".
              "Enter your selection: ";
	chomp($ans = <STDIN>);
}
	
if ($ans == 1) {  

	&Ind;
	&Cap;
	$frq = 1 / (2 * pi * (sqrt ($c * $l)));
	&Print;
}

elsif ($ans == 2) {

	&Freq;
 	&Cap;
        $l = 1 / ((($frq ** 2) * $c) * ((2 * pi) ** 2));
	&Print;
}

elsif ($ans == 3) {

	&Freq;
	&Ind;
        $c = 1 / ((($frq ** 2) * $l) * ((2 * pi) ** 2));
	&Print;
}   

elsif ($ans == 4) {

	&Freq;
	&Ind;

	print "\n";

	while (!$ql) {
		print "Enter Q of inductor at resonant frequency: ";
		chomp($ql = <STDIN>);
	}

	print "\n";

	while (!$qc) {
		print "Enter Q of capacitor: ";
		chomp($qc = <STDIN>);
	}
	
	$x1 = 2 * pi * $frq * $l;

	$r1 = $x1 / $ql;
	$r2 = $x1 / $qc;
	$r3 = (1 + ($ql ** 2)) * $r1;
	$r4 = (1 + ($qc ** 2)) * $r2;

	$z1 = $r1 + $r2;
	$z2 = ($r3 * $r4) / ($r3 + $r4);

	$q = ($ql * $qc) / ($ql + $qc);

	printf "\n\n      Resultant LC circuit Q : %.4f\n".
	       "   Series resonant impedance : %.4f ohms\n".
	       " Parallel resonant impedance : %.4f ohms\n\n", $q, $z1, $z2;
}
