clear
echo on


% Demonstrate a MMSE-FSE+DFE design

% Load channel
load /home/combine/raulc/research/sims/comms/channels/chan3.mat
C=C/norm(C);

% Select FSE and DFE lengths
Nf=96;
Nd=32;
symbol='x'; 

% Define set of SNR (dB) to calculate SER
snrdb=35;

% Select cursor as optimum delay of MMSE-FSE 
% (NOTE: this is not the optimum delay choice, 
%  but it is fairly good and saves time.)
[dummyf,delta1,dummyumse] = ummse_fse(C,snrdb,Nf);


% Add nT delay for decision feedback
% to channel-FSE combination delay
nu=2;
delta2=(delta1+1)+nu;


% Normalize channel for unit received power 
snr=10^(snrdb/10);
desirednorm=2*(1-1/snr);
h=C*sqrt(desirednorm); 

% Find UMMSE-FSE+DFE design 
[f,d,ummse]=ummse_fse_dfe(h,snrdb,Nf,Nd,delta1,delta2);

% Plot results
figure(1)
tt=conv(f,h);
t=tt(1:2:length(tt));
dd=[zeros(delta2-1,1); t(delta2:delta2+Nd); ...
               zeros(length(t)-Nd-delta2,1)];
subplot(411);
stem(real(h));
title('(a) Channel Impulse Response (real part)');
subplot(412);
stem(real(f));
title('(b) FSE Impulse Response (real part)');
subplot(413);
stem(real(t));
title('(c) Channel-FSE Impulse Response (real part)');
subplot(414);
stem(real(dd));
title('(d) Augmented DFE Impulse Response (real part)');
axis([0 200 -0.5 1]);

figure(2)
M=64;
L=1000;
s=qam(M,L);
ss=zeros(2*L,1);
ss(1:2:2*L)=s;
n=1/sqrt(2*snr)*(randn(2*L,1)+i*randn(2*L,1));
r=filter(h,1,ss)+n;
r=r(200:2*L);
nn=filter(f,1,n);
z=filter(t-dd,1,s)+nn(1:2:2*L);
z=z(200:L);
subplot(121)
plot(r,'.');
title('(e)');
axis('square');
xlabel('Re');
ylabel('Im');
subplot(122)
plot(z,'.');
title('(f)');
axis('square');
xlabel('Re');
ylabel('Im');
