% Global constants
moon_mass=7.36e22;
G=6.67300e-11;
earth_moon_distance=384400e3;
moon_l2_distance=64500e3;
orbital_period=10*60*60;
k=1.3806503e-23;
earth_radius=6380000;

sample_rate=8e9;
quantization=6;

fprintf(1, 'Using sample rate of: %d\n', sample_rate);
fprintf(1, 'Using quanti rate of: %d\n', quantization);


observatory_radius=300;
satellite_radius=5;
observatory_effeciency=0.9;
satellite_effeciency=0.75;

% Orbital mechanics
orbital_distance=(G*moon_mass*orbital_period^2/(4*pi^2))^(1/3)
orbital_distance-1737000
me_distance_ratio=100*orbital_distance/earth_moon_distance;
l2l4_distance=earth_moon_distance;
l2_lunar_sat_distance=sqrt(moon_l2_distance^2+orbital_distance^2)
l2l4_distance_ratio=100*l2_lunar_sat_distance/l2l4_distance;
earth_lunar_sat_distance=sqrt(earth_moon_distance^2+orbital_distance^2)-earth_radius;

% Link budget

bit_rate=quantization*sample_rate;
coded_bit_rate=2*bit_rate;

% There are three links in this problem. Use an array of structures.
link(1).lambda=3e8/24.2e9;
link(2).lambda=link(1).lambda;
link(3).lambda=link(1).lambda;
link(1).t_radius=300;
link(1).r_radius=5;
link(3).t_radius=5;link(2).t_radius=5;link(2).r_radius=5;
link(3).r_radius=17;
link(1).t_effeciency=0.8;
link(1).r_effeciency=0.75;link(2).t_effeciency=0.75;link(2).r_effeciency=0.75;link(3).t_effeciency=0.75;
link(3).r_effeciency=0.9;
% link(1).g_t=10*log10(observatory_effeciency*observatory_radius^2*pi*4*pi/link(1).lambda^2);
% link(1).g_r=10*log10(satellite_effeciency*satellite_radius^2*pi*4*pi/link(1).lambda^2);
% link(2).g_r=link(1).g_r;
% link(2).g_t=link(1).g_r;
% link(3).g_t=link(1).g_r;
% link(3).g_r=link(1).g_t;
link(1).r=moon_l2_distance;
link(2).r=l2_lunar_sat_distance;
link(3).r=earth_lunar_sat_distance;
link(1).k=0.1;
link(2).k=link(1).k;
link(3).k=link(1).k;
link(1).bits_per_symbol=6;
link(2).bits_per_symbol=link(1).bits_per_symbol;
link(3).bits_per_symbol=link(2).bits_per_symbol;
link(1).temperature=17;
link(2).temperature=20;
link(3).temperature=300;
link(1).ebn0=6;
link(2).ebn0=link(1).ebn0;
link(3).ebn0=link(1).ebn0;
link(1).p_t=0;
link(2).p_t=0;
link(3).p_t=0;
link(1).snr=0;

fprintf(1, 'Using eb/n0 of: %d\n', link(1).ebn0);

for i=1:3
    % First compute symbol rate for the link
    link(i).symbol_rate=coded_bit_rate/link(i).bits_per_symbol;
    % Compute bandwidth from raised-cosine formula
    link(i).bandwidth=(1+link(i).k)*link(i).symbol_rate;
    link(i).lambda=3e8/(15.4e9+link(i).bandwidth/2);
    link(i).g_t=10*log10(link(i).t_radius^2*pi*4*pi*link(i).t_effeciency/(link(i).lambda^2));
    link(i).g_r=10*log10(link(i).r_radius^2*pi*4*pi*link(i).r_effeciency/(link(i).lambda^2));
    % Now compute noise power for each link
    link(i).noise_power=10*log10(k*link(i).temperature*link(i).bandwidth);
    link(i).snr=link(i).ebn0+10*log10(coded_bit_rate/link(i).bandwidth);
    % Use C/N ratio to compute required recieved power and compute transmit
    % power
    link(i).p_t=link(i).noise_power+link(i).snr-link(i).g_t-link(i).g_r+20*log10(link(i).r)+20*log10(4*pi/link(i).lambda);
end

link(1)
link(2)
link(3)
coded_bit_rate;