% Equalizer assignment.  Note that a couple of pieces of the following code
% are missing.  Please answer the questions designated Q1..Q19 below and
% supply the missing brief code snippets.

clear all
rand( 'state', 54321 );
randn( 'state', 54321 );

% Various channel models.  Uncomment one.
% c = [ 1 1 ].';
c = [ 1 0 .9*exp(j*1.1*pi) zeros(1,30) ].';
% c = [ 1 .9*exp(j*.1*pi) .1 -.6 -.5*exp(j*.2*pi) 0 0 .3 .1 zeros(1,30) ].';
figure(11); stem( abs( c ) )

% Q1: Plot an FFT of the channel response (use 20log10() to show the power 
% rather than the voltage response.  How deep is the deepest null from 
% highest point to deepest point?  
% Assume a nominal EbNo = 10 dB.  What is the SNR in this null?  Would a 
% system run error free at that frequncy?
C = fft(c);
figure(1); plot( 20*log10( abs(C) ) );

% Create a bit stream of length N = 1000 bits.
N = 1000;
x = 1 - 2 * round(rand( N, 1 ));

% The signal arriving at the receiver experiences multipath in the channel:
r = filter( c, 1, x );

% Compute Eb
Eb = mean( abs(r).^2 );

% Assume an Eb/No of 10 dB due to path losses and add noise at the receiver
EbNo = 10; % (dB)
% Q2: Compute sigma.  Hint1: sigma^2 = No/2 and you've already computed Eb.
% Hint #2, you need linear EbNo not EbNo in dB.  Is this a power or voltage
% relationship?

y = r + sigma * randn(size(x));

% Q3: Are the nulls in the noisy spectrum the same depth as in Q1?  Is this
% related to the noise?  What does this tell you about the quality of any
% equalization?
figure(2); pwelch( y, 256,[],[],10, 'twosided')

% Let's use an equalizer of length L = 16 taps
L = 16;

% Let's do data-aided equalization over a training sequence of length M =
% 50 points.
M = 50; %GSM length 100;
% Q4: Is data aided better than decision directed?  Are we using more of
% fewer samples than the GSM midamble?

% Q5: Show how to construct the Y matrix discussed in class to represent 
% which samples are convolved with an equalizing filter to give estimates 
% of x[n], ... x[n-M+1]

% Q6: show how to compute h, the MSE equalizer filter, h

% figure(3); stem( abs( conv( h, c ) ) );

% Q7: how does the spectrum of the equalizing filter compare to the
% spectrum of the channel?  What shape is the combination of the channel
% and the equalization?  What shape should it be?
H = fft([ h ; zeros( length(c) - length(h), 1 ) ]);
HC = fft(conv(h,c));
figure(4); plot( 20*log10( abs([C H]) ) ); legend( '|C(f)|^2', '|H(f)|^2' )
figure(5); plot( 20*log10( abs(HC) ) );

% Q8: How many errors would we have without equalization?
errs_noEQ = sum( sign(real(y)) ~= x )

% Now apply the equalizer
xhat = filter( h, 1, y );
figure(6); pwelch( x, 256, [],[],10, 'twosided' )

% Q9: How many errors do we have after equalization?
errs_EQ = sum( sign(real(xhat)) ~= x )

% Q10: What do these plots tell you about why the performance differs in
% each case?
figure(8); stem( [ x real( y ) ] );
figure(9); stem( [ x real(xhat) ] );

% Q11: How many errors would we have if we set L = 32, 30, 8?
% Q12: How much does equalizer length dictate performance?

% Q13: Set EbNo = 20, M = 50, L = 16.  How many erros do we get?
% Q14: How much does performance depend on EbNo?



% Suppose we equalized adaptively.
% Run the commeqsim.mdl demo 

% Q15: When do the RLS and LMS algorithms converge?  (See the MSE
% Convergency plot.  (You may have to shorten the simulation time to see it
% well.)
% Q16: Which algorithm has the smallest steady-state error?  What shows you
% this?

% Q17: Please draw/print the channel, equalizer and combined frequency
% response.  Why might the combined response not be flat?

% Q18: What do you think the MSE cost function plot is showing?

% Q19: Why are adaptive equalizers more common than using a pseudo inverse
% as we did above?
