% This script calculates the SNR along the line that goes from the crystal
% oscilaltor to the EOM of the 40 Upgrade RF plan.
% It is intended for the RF plan as shown in the yEd schematic;

s = [];
n = [];
gdB = [0 -1 8 -3.5 -3 7 -15 11 -3.5 -1.5 28.5];
nf = [];
numelements = 11;

nf = zeros(numelements,1);
nf(3) = 1.5;
nf(8) = 7.5;
nf(10) = 9;

nf = 10.^(nf/10);

s(1) = dBmtoWatts(13);
n(1) = dBmtoWatts(-170 + 10*log10(s(1)*1e3));

g = 10.^(gdB/10);

Ptn = 4*1.38e-23*290*1e3;     % thermal noise power from a 50Ohm resistor

for i = 2 : numelements
    s(i) = g(i) * s(i-1);
    n(i) = g(i) * ( n(i-1) + nf(i) * Ptn );
end

sdBm = Watts2dBm(s)'
ndBm = Watts2dBm(n)'
snrdBc = sdBm - ndBm