(* To load this package, type "<< budget.up.m" at the
   Mathematica command line. To find the value of a variable, for a given
   assignment of values to variables, type "N[<var>/.<subs>]", e.g. to
   find the link margin for the substitutions in subs11M, type
   "N[M/.subs11M]".
*)


(* Transmitter Losses (CommLt):
          Pointing loss: 1 dB
   Antenna circuit loss: 3 dB (2 for phase shifters)
   
   Path Losses (CommLp):
           Ionospheric loss: 0.1 dB
    Atmospheric Attenuation: 0.3 dB
           Rain Attenuation: 1.0 dB (Based on 99.5% availability at worst
	                            DSN site, Canberra, Australia, for
				    8.45 GHz. 0.601 dB, rounded up)
   Waveform distortion loss: 0.2 dB

   Receiver Losses (CommLr):
    Demod/Detector loss: 0.2 dB
      Polarization loss: 0.2 dB
          Pointing loss: 0.2 dB
   Antenna circuit loss: 0.5 dB
           Phase jitter: 0.2 dB
     Receiver feed loss: 0.1 dB
*)
(* CommPt = Transmitter output power (W)
   Commfreqt = carrier frequency (Hz)
   Commlambdat = carrier wavelength (m)
   Commtheta0Max = Maximum electronically pointed angle (degrees)
   CommDt = Diameter of (circular) phased array transmitter (m)
   CommAt = Area of transmitter (m^2)
   Commetat = efficiency of transmitting antenna (dimensionless)
   CommLtPoint = Pointing loss
   CommLtBand = Bandlimiting loss
   CommLtCircuit = Antenna circuit loss
   CommLt = Transmitter Losses
   CommGt = Gain of transmitter
   commHPBWr = Half Power Beam Width (degrees)
   Commfactor = holds an intermediate result
   Commdx = Horizontal spacing of elements in phased array (m)
   Commdy = Vertical spacing of elements in phased array (m)
   CommNumElements = Approx. number of elements in phased array
   CommEIRP = Effective Isotropic Radiated Power (W)
   CommDist = Propogation Path Length (m)
   CommLs = So called space Loss
   CommLpIonosphere = Ionospheric loss
   CommLpAtmosphere = Atmospheric Attenuation
   CommLpRain = Rain Attenuation
   CommLp = Path Losses
   CommLrWaveDistort = Waveform Distortion Loss
   CommLrMod = Receiver Demod/Detector loss
   CommLrPolar = Receiver Polarization loss
   CommLrPoint = Receiver Pointing loss
   CommLrCircuit = Receiver Antenna circuit loss
   CommLrPhase = Phase jitter (receiver)
   CommLrFeed = Receiver feed loss
   CommLr = Receiver Losses
   Commetar = efficiency of receiving antenna (dimensionless)
   CommDr = Diameter of (parabolic) receiving antenna (m)
   CommGr = Gain of receiving antenna (dimensionless)
   CommFlux = Flux density on Earth (W/m^2)
   CommPr = Received power (W)
   CommTe = Equivalent noise temperature (K)
   CommR = Modulated data rate (bits/sec)
   CommEbN0 = Signal per bit / single sided noise density  (Eb/N0) (dB)
   CommEbN0Req = Required Eb/N0 (dB)
   CommM = Link Margin (dB)
*)   

CommsubsXband = {Commfreqt-> 7.234 10^9, CommPt -> 100, Commfreqr ->
8.90 10^9, Commtheta0Max -> 65., CommDt -> 22.0, Commetat -> 0.55,
Commetar -> 0.70, CommDr -> 0.55, CommTe -> 500, CommR -> 125 * 10^3, CommEbN0Req -> 2, CommDist -> 400000000.} ; 

CommsubsSband = {Commfreqt-> 2.4015 10^9, CommPt -> 100, Commfreqr ->
8.90 10^9, Commtheta0Max -> 65., CommDt -> 22.0, Commetat -> 0.55,
Commetar -> 0.70, CommDr -> 0.55, CommTe -> 400, CommR -> 7.5 * 10^3, CommEbN0Req -> 2, CommDist -> 400000000.} ; 

subs = CommsubsSband;

TodB[x_] := 10. * Log[x] / Log[10.] ;
FromdB[xdB_] := 10^(xdB / 10) ;

(* Path Losses *)
CommLpIonospheredB = 0.1 ;
CommLpAtmospheredB = 0.3 ;
CommLpRaindB = 1.0 ;
CommLpdB = CommLpIonospheredB + CommLpAtmospheredB + CommLpRaindB ;

(* Transmitter Losses *)
CommLtPointdB = 1;
(* CommLtBanddB = 1; *)
CommLtCircuitdB = 3;
CommLtdB = CommLtPointdB (* + CommLtBanddB *) + CommLtCircuitdB ;

(* Receiver Losses in dB*)
CommLrWaveDistortdB = 0.2 ;
CommLrModdB =  0.2 ;
CommLrPolardB = 0.2 ;
CommLrPointdB = 0.2 ;
CommLrCircuitdB = 0.5 ;
CommLrPhasedB = 0.2 ;
CommLrFeeddB = 0.1 ;
CommLrdB = CommLrModdB +  CommLrPolardB + CommLrPointdB +  CommLrCircuitdB +
    CommLrPhasedB + CommLrFeeddB + CommLrWaveDistortdB ;


CommPtdB = TodB[CommPt] ;
Commlambdat = 2.998 10^8 / Commfreqt ;
commHPBWt = 21.0 10^9 / (Commfreqt *CommDt) ; 
CommAt = Pi * CommDt^2 / 4 ;
CommGt = 4 * Pi * CommAt / Commlambdat^2 * Commetat ;
CommGtdB = TodB[CommGt] ;

Commlambdar = 2.998 10^8 / Commfreqr ;

commHPBWr = 58.5 * Commlambdar / CommDr ;
Commfactor = 1/(1 + Sin[ (Commtheta0Max + 1.5*commHPBWr) * Pi/180. ]) ;
Commdx = Commfactor * Commlambdar ;
Commdy = Commfactor/Sqrt[3.] * Commlambdar ;
CommNumElements = CommAr / (2 * Commdx * Commdy) ;

CommEIRPdB = CommPtdB + CommGtdB - CommLtdB ;
CommEIRP = FromdB[CommEIRPdB] ;
CommLs = (4 * Pi * CommDist / Commlambdat) ^ 2 ;
CommLsdB = TodB[CommLs] ;

CommAr = Pi * CommDr * CommDr / 4. ;
CommGr = 4 * Pi * CommAr / (Commlambdar^2) * Commetar *
Cos[Commtheta0Max*Pi/180.] ;   
CommGrdB  = TodB[CommGr] ;
CommPrdB = CommEIRPdB - CommLsdB - CommLpdB + CommGrdB ;
CommPr = FromdB[CommPrdB] ;
CommFluxdB = CommEIRPdB - CommLpdB - TodB[4*Pi*CommDist^2] ;
CommFlux = FromdB[CommFluxdB] ;
CommTedB = TodB[CommTe] ;
CommRdB = TodB[CommR] ;
CommEbN0 = CommPrdB - CommLrdB + 228.6 - CommTedB - CommRdB ;
CommM = CommEbN0 - CommEbN0Req ;


Print[""] ; Print[""] ;
Print["..................... Constants
......................................"]; 
Print[""] ; Print[""] ;
Print["                          Frequency: ", N[Commfreqt/.subs], " Hz"];
Print["                         Wavelength: ", N[Commlambdat/.subs], " m"];
Print["                           Datarate: ", N[CommR/.subs], " bps"];
Print["            Propogation Path Length: ", N[CommDist/.subs], " m"];
Print[""] ; Print[""] ;


Print["...................... Path Losses
..................................."]; 
Print[""] ;Print[""] ;
Print["                   Ionospheric loss: ", N[CommLpIonospheredB/.subs], " dB"] ;
Print["            Atmospheric Attenuation: ", N[CommLpAtmospheredB/.subs], " dB"] ;
Print["                   Rain Attenuation: ", N[CommLpRaindB/.subs], " dB"] ;
Print["                    Total Path Loss: ", N[CommLpdB/.subs], " dB"] ;
Print["                         Space Loss: ", N[CommLsdB/.subs], " dB"] ;
Print[""] ; Print[""] ;


Print["................ Transmitter
Losses..................................."]; 
Print[""] ; Print[""] ;
Print["                      Pointing loss: ", N[CommLtPointdB/.subs], " dB"] 
(* Print["                  Bandlimiting loss: ", N[CommLtBanddB/.subs], " dB"]*)
Print["               Antenna circuit loss: ", N[CommLtCircuitdB/.subs], " dB"] 
Print["           Total Transmitter Losses: ", N[CommLtdB/.subs], "
dB"] ;
Print[""] ; Print[""] ;


Print["................ Receiver Losses
....................................."]; 
Print[""] ; Print[""] ;
Print["           Waveform Distortion Loss: ", N[CommLrWaveDistortdB/.subs], " dB"] ;
Print["                Demod/Detector loss: ", N[CommLrModdB/.subs], " dB"] ;
Print["                  Polarization loss: ", N[CommLrPolardB/.subs], " dB"] ;
Print["                      Pointing loss: ", N[CommLrPointdB/.subs], " dB"] ;
Print["               Antenna circuit loss: ", N[CommLrCircuitdB/.subs], " dB"] ;
Print["                       Phase jitter: ", N[CommLrPhasedB/.subs], " dB"] ;
Print["                 Receiver Feed loss: ", N[CommLrFeeddB/.subs], " dB"] ;
Print["                Total Receiver Loss: ", N[CommLrdB/.subs], " dB"] ;
Print[""] ; Print[""] ;


Print["................ Transmitter Antenna
................................."]; 
Print[""] ; Print[""] ;
Print["                  Transmitter Power: ", N[CommPt/.subs], " W"] ;
Print["               Transmitter Diameter: ", N[CommDt/.subs], " m"] ;
Print["                      Half Power BW: ", N[commHPBWt/.subs], " degrees"] ;
Print["                   Transmitter Gain: ", N[CommGtdB/.subs], " dB"] ;
Print["        Transmit Antenna Efficiency: ", N[Commetat/.subs], ""];
Print["                               EIRP: ", N[CommEIRPdB/.subs], " dB"] ;
Print[""] ; Print[""] ;


Print["................ Receiver Antenna
...................................."]; 
Print[""] ; Print[""] ;
Print["                     Received Power: ", N[CommPr/.subs], " W"] ;
Print["                  Receiver Diameter: ", N[CommDr/.subs], " m"] ;
Print["                      Receiver Gain: ", N[CommGrdB/.subs], "
dB"] ;
Print["                        NumElements: ", N[CommNumElements/.subs] ] ;
Print["                      Half Power BW: ", N[commHPBWr/.subs], "
degrees"] ;
Print["                     Steering Angle: ", N[Commtheta0Max/.subs], "deg"];
Print["                    Receiver Losses: ", N[CommLrdB/.subs], " dB"] ;
Print["              Equivalent Noise Temp: ", N[CommTe/.subs], " K"] ;
Print["         Receive Antenna Efficiency: ", N[Commetar/.subs], ""];
Print[""] ; Print[""] ;


Print["...................... Results
......................................."]; 
Print[""] ; Print[""] ;
Print["                     Required Eb/N0: ", N[CommEbN0Req/.subs], " dB"] ;
Print["                     Achieved Eb/N0: ", N[CommEbN0/.subs], " dB"] ;
Print["                               EIRP: ", N[CommEIRPdB/.subs], " dBW"] ;
Print["                       Flux Density: ", N[CommFluxdB/.subs], " dB W/m^2"] ;
Print["                        Link Margin: ", N[CommM/.subs], " dB"] ;
Print[""] ; Print[""] ;
Print["...............................
......................................."]; 


