#!/usr/local/bin/perl

$freq = 2.4;
$tpo = .03;
$txlineloss = 3;
$txantgain = 8;
$rxlineloss = 3;
$rxantgain = 8;
$rxthresh = -80;
$pathlenght = .2; 

print "Path loss and fade margin calculations -\n";

print "Frequency in GHz [$freq]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$freq = $_; 
}

print "Transmitter power in Watts [$tpo]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$tpo = $_; 
}

print "Transmitter transmission line loss in dB [$txlineloss]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$txlineloss = $_; 
}

print "Transmitter antenna gain in dBi [$txantgain]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$txantgain = $_; 
}

print "Receiver transmission line loss in dB [$rxlineloss]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$rxlineloss = $_; 
}

print "Receiver antenna gain in dBi [$rxantgain]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$rxantgain = $_; 
}

print "Receiver threshold in dBm [$rxthresh]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$rxthresh = $_; 
}

print "Path lenght in miles [$pathlenght]: ";
$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$pathlenght = $_; 
}

print "And for Fresnel Zone obstruction calculation -\n";

$obstruction = $pathlenght/2; 

print "Distance from one antenna to potential obstruction in miles [$obstruction]: ";

$_ = <STDIN>;
chomp;
if ($_ ne ""){
	$obstruction = $_; 
}

print "	Frequency 	= $freq GHz\n";
print "	TPO 		= $tpo Watts\n";

$tpom = log10($tpo/0.001) * 10;

print "	TPO 		= $tpom dBm\n";
print "	TX line loss	= $txlineloss dB\n";
print "	TX ant gain	= $txantgain dBi\n";
print "	Path lenght	= $pathlenght miles\n"; 

$pathloss = 96.6+(20*log10($freq))+(20*log10($pathlenght));

print "	Path loss	= $pathloss dB\n";
print "	RX ant gain	= $rxantgain dBi\n";
print "	RX line loss	= $rxlineloss dB\n";

$rxsignal = $tpom - $txlineloss + $txantgain - $pathloss + $rxantgain - $rxlineloss;

print "	RX signal	= $rxsignal dBm\n";
print "	RX threshold	= $rxthresh\n";

$fademargin = $rxsignal - $rxthresh;

if ($fademargin < 0){
	$fademargin = 0;
}
print "	Fade margin	= $fademargin dB\n";

print "	Distance to obstruction	from one transmitter	= $obstruction miles\n";

$foo = $pathlenght - $obstruction;

$firstfresnel = sqrt(($obstruction * $foo)/($pathlenght * $freq)) * 72;
print "	First Fresnel Radius			= $firstfresnel feet\n";

$secondfresnel = $firstfresnel * sqrt(2);
print "	Second Fresnel Radius			= $secondfresnel feet\n";

$thirdfresnel = $firstfresnel * sqrt(3);
print "	Third Fresnel Radius			= $thirdfresnel feet\n";

$forthfresnel = $firstfresnel * sqrt(4);
print "	Forth Fresnel Radius			= $forthfresnel feet\n";

sub log10 {
	my $n = shift;
	return log($n)/log(10);
}  

