function [PtxMR_dBm,PtxMR_mW,PtxMR2_dBm,PtxMR2_mW] = link(PrxATAinput,BWinput)
% usage: [PtxMR_dBm,PtxMR_mW,PtxMR2_dBm,PtxMR2_mW] = link(PrxATAinput,BWinput);
%
% where
%   PtxMR_dBm = MR transmit power necessary by Shannon Limit in dBm
%   PtxMR_mW = above param in mW
%   PtxMR2_dBm = MR transmit power necessary by user input in dBm
%   PtxMR2_mW = above param in mW
% with
%   PrxATAinput = desired receive power at ATA in dB, desired = -120dB
%   BWinput = desired bandwidth for link in Hz, desired = 50MHz
%
% M-file to calculate various parameters for link budget.
% abbreviations in code:
%   HCRO = Hat Creek Radio Observatory
%   ATA = Allen Telescope Array
%   Apol = Apollo
%   MR = Moon Rover
%   ufa = uplink frequency used in Apollo
%   dfa = downlink frequency used in Apollo
%   
%   Some parameters (i.e. moon radii, max and min distances, etc.)are left
%   left in this file in case user would like to edit file for future use
%   Ben Clarke, Georgia Institute of Technology
%   c/o VagaLune SarL (c) 2007.
% desired values:
%PrxATAinput = -120;     BWinput = 50e6;

fprintf(1,['User has input ' num2str(PrxATAinput) ' dB and ' num2str(BWinput) ' Hz.\n'])

% Universal constants
c = 299792458;          % speed of light, m/s
k = 1.38065 * 10^-23;   % Stefan-Boltzman constant, J/K

% Hat Creek Radio Observatory (HCRO)
% Allen Telescope Array for use in LunarX and SETI
% Dish type = Gregorian offset feed, HCRO parameters
ATAdiam = 6.1;          % Diameter of ATA in m
ATAsysT = 45;           % system temperature in Kelvin
ATAeff = 0.72;          % ARBITRARY antenna efficiency, 72%

% Link frequencies
BW = BWinput;                   % ARBITRARY Bandwidth, 50MHz
C = 20e6;                       % Bit rate HDTV signal, 20 Mbps
bitrate = C*1.07;               % Bit rate plus 7% overhead
upfreq_apol = 2119e6;           % Apollo uplink S-band frequency, 2.119GHz
downfreq_apol = 2278.5e6;       % Apollo downlink S-band frequency, 2.2785GHz
lambda_ufa = c/upfreq_apol;     % lambda_up-freq-apol
lambda_dfa = c/downfreq_apol;   % lambda_down-freq-apol

% (Moon) Rover parameters
MRdiam = 0.3;           % ARBITRARY MR satellite radius, 0.3m
MReff = 0.10;           % ARBITRARY MR efficiency, 10%
MRsysT = 274.5;         % black-body temp of moon, Kelvin
Rmoon = 1738.1e3;       % equitorial radius of moon, m
Rpmoon = 1736e3;        % polar radius of moon, m

% Earth-Moon parameters
EMdistance = 378e6;     % Earth-Moon distance, m
EMmin = 357e6;          % Earth-Moon mindistance, m
EMmax = 407e6;          % Earth-Moon maxdistance, m

% reference path loss/wavelength loss
Ploss_ref = 20*log10(EMdistance);   % pathloss in dB
Ploss_refmax = 20*log10(EMmax);     % maxpathloss in dB
Ploss_refmin = 20*log10(EMmin);     % minpathloss in dB
Ploss_ufa = 20*log10(lambda_ufa/(4*pi));    % wavelength loss upfreq in dB
Ploss_dfa = 20*log10(lambda_dfa/(4*pi));    % wavelength loss upfreq in dB

% Noise temp
NoisePATA = 10*log10(k*ATAsysT*BW);      % Noise power dB, earth station
NoisePMR = 10*log10(k*MRsysT*BW);       % Noise power dB, moon rover

% Antenna gains
GATA_ufa = ATAeff*(4*pi^2 * (ATAdiam/2)^2) /lambda_ufa^2;   % Gain ATA dish_ufa
GATA_dfa = ATAeff*(4*pi^2 * (ATAdiam/2)^2) /lambda_dfa^2;   % Gain ATA dish_dfa
GMR_ufa = MReff*(4*pi^2 * (MRdiam/2)^2) /lambda_ufa^2;   % Gain MR dish_ufa
GMR_dfa = MReff*(4*pi^2 * (MRdiam/2)^2) /lambda_dfa^2;   % Gain MR dish_dfa

GainATA_ufa = 10*log10( GATA_ufa );   % Gain ATA dish_ufa in dB
GainATA_dfa = 10*log10( GATA_dfa );   % Gain ATA dish_dfa in dB
GainMR_ufa = 10*log10( GMR_ufa );   % Gain MR dish_ufa in dB
GainMR_dfa = 10*log10( GMR_dfa );   % Gain MR dish_dfa in dB

%Linear Link Budget (in dB)
%Prx = Ptx + Grx + Gtx - 20log(lamdba/4pi) - 20log(r)

%Uplink Budget (in dB)
%PrxMR = PtxATA + GATA + GMR - 20log(lambda/4pi) - 20log(r);
%PrxMR - PtxATA = GATA + GMR - 20log(lambda/4pi) - 20log(r);
%Pdiff_uplink = PrxMR - PtxATA
Pdiff_uplink = GainATA_ufa + GainMR_ufa - Ploss_ufa - Ploss_ref;

%Downlink Budget (in dB)
%PrxATA = PtxMR + GMR + GATA - 20log(lambda/4pi) - 20log(r);
%PrxATA - PtxMR = GMR + GATA - 20log(lambda/4pi) - 20log(r);
%Pdiff_downlink = PrxATA - PtxMR
Pdiff_downlink = GainMR_dfa + GainATA_dfa - Ploss_dfa - Ploss_ref;

% Shannon Channel Cap. Method
% bitrate = BW*log2(1+SNR)
% SNR = 2^(bitrate/BW)-1 = Signal[W]/Noise[W]
SNR = 2^(bitrate/BW) -1;            % SNR in Watts/Watts
PrxATA = 10*log10(SNR)+NoisePATA;   % RX signal power at ATA, in dB
PtxMR = PrxATA - Pdiff_downlink;    % TX signal power from MR, in dB
PtxMR_dBm = PtxMR + 30;             % MR TX power in dBm
PtxMR_mW = 10^(PtxMR_dBm/10);       % MR TX power in mW
begin = 'By Shannon, MR needs to transmit ';
back = [num2str(PtxMR_dBm) ' dBm or ' num2str(PtxMR_mW) ' mW.\n'];
fprintf(1,[begin back])

% Must receive ~80dBm at ATA Method
PrxATA2 = PrxATAinput;              %user input power in dB
PtxMR2 = PrxATA2 - Pdiff_downlink;
PtxMR2_dBm = PtxMR2 + 30;
PtxMR2_mW = 10^(PtxMR2_dBm/10);    % MR TX power in mW
begin2 = 'By user input, MR needs to transmit ';
back2 = [num2str(PtxMR2_dBm) ' dBm or ' num2str(PtxMR2_mW) ' mW.\n'];
fprintf(1,[begin2 back2])

