function assign4
%1. A mobile radio system receives a coherent FSK signal with 
% an average SNR per bit of 18 dB. Compute the probability of 
% a word error for a 12-bit word for each of the following cases:
disp('Question 1');
N = 12;
s = 10^(18/10);
disp(sprintf('Linear SNR=%f\n', s))

disp('a) no diversity or error-correcting coding:')
p = 0.5*(1-sqrt(s/(2+s)));
pem = 1.0 - binomial(0,12,p);
disp(sprintf('p=%e pem=%e\n', p, pem));

disp('b) 3-out-of-5 multiple transmission:')
p1 = 0.0;
for i=3:5
   p1 = p1 + binomial(i,5,p);
end
pem = 1.0 - binomial(0,12,p1);
disp(sprintf('p1=%e pem=%e\n', p1, pem));

disp('c) (17,12) Hamming code:')
t=1;
pem = 1.0;
for i=0:t
   pem = pem - binomial(i,17,p);
end
disp(sprintf('p=%e pem=%e\n', p, pem));

disp('d) (23,12) Golay code:')
t = 3;
pem = 1.0;
for i=0:t
   pem = pem - binomial(i,23,p);
end
disp(sprintf('p=%e pem=%e\n', p, pem));

disp('e) two-branch spatial diversity with equal-gain combining:')
M = 2;
alpha = 0.5;  % FSK
Mf = 1;
for i=2:M  Mf = Mf * i; end
p1 = (M/2)^M / (2*(alpha*s)^M * Mf);
pem = 1.0 - binomial(0,12,p1);
disp(sprintf('p1=%e pem=%e\n', p1, pem));

%3.Consider three channels:
%     channel A: 940.1 MHz
%     channel B: 940.4 MHz
%     channel C: 940.7 MHz
% A base station is transmitting these signals with equal power
% using a power combiner having an output voltage eO given by
%     eO = 2ei + 0.05 ei2 + 0.01 ei3
% Compute the ratio of in-band IM distortion power to desired
% signal power for channel B. Assume a 25 kHz bandwidth and 
% ignore terms that cause only amplitude distortion.

disp('Question 3');
disp('Since the three channels are equally-spaced we have')
disp('from the course notes, the in-band undesired term is')
disp('3a_3/2 ABC cos(a+g-b)')
disp('Desired term is a_1 B cos(b)')
disp('Worstcase is A=B=C')

disp('SI = (a_1*B/(3/2*a_3*A*B*C))^2 = 4/9 (a_1/a_3)^2 / A^4')
a_1=2;  a_3=0.01;
SI = 4/9 * (a_1/a_3)^2;
IS = 1/SI;
disp(sprintf('SI = %e / A^4 or SI = %f / A^4 dB\n', IS, 10*log10(IS)))

%4. For a cell radius of 8 km, what is the minimum filter slope
% required to attain a carrier-to-interference ratio of 18 dB 
% when there is a mobile 300 m from the base station. 
% Assume a=4 and a channel separation of three channels.
disp('Question 4');
alpha = 4;
d1 = 8000;
d2 = 300;
CIc = 18;
sep = 3;
NEFER = alpha*10*log10(d1/d2);
f1f2 = sep*2;
loss = NEFER + CIc;
K = 0.3*loss / log10(f1f2);
disp(sprintf('NEFER=%f, Req''d Loss=%f dB, K=%f dB/oct', NEFER, loss, K));

function c=binomial(x,n,p)
num=1.0;
for i=x+1:n  num = num * i; end

den=1.0;
for i=2:n-x den = den * i; end

c = num/den * (1.0 - p)^(n-x) * p^x;
