% Non-causal DFE of Gersho & Lim

% Initialization
rand('seed',0);
block_num=[1000*ones(1,8) 1000*ones(1,12)];   
% Number of blocks
N=1000;           % block length
Lf=4;            % Number of feedforward taps
Lb=3;             % Number of feedback taps
P=1;              % Fractional sampling factor
snr=[8:1:14];            % in dB

% Generate Channel & equalizer

C1=[0.3162 1 0.3162];
C3=[0.227 0.46 0.688 0.46 0.227];
C=C1;
C=C./sqrt(C*C');

tic
for snr_index=1:length(snr)
snr(snr_index)
sig=10^(-snr(snr_index)/20);

[f_opt,b_opt,delay,mmse]=dfe_tap_estimator(C,Lf,Lb,10^(-snr(snr_index)/10),P,0);
Lc=length(C);

for iter=1:block_num(snr_index)
iter
% Generate Source Sequence
symb=pam(N,2,1);
symb_up=zeros(1,P*N-P+1);
symb_up(1:P:P*N-P+1)=symb;

% Transmit through channel
rec_seq=conv(symb_up,C);
rec_seq=rec_seq+noise(length(rec_seq),snr(snr_index),1);

% Feedforward filtering
rec_seq1=conv(rec_seq,transpose(f_opt));
r=rec_seq1(delay:P:end); % downsampled signal

% Feedback filtering

% Hard Decision feedback
symb_hat(1:Lb)=symb(1:Lb);
for k=Lb+1:N
y(k)=r(k)-symb_hat(k-1:-1:k-Lb)*b_opt;
symb_hat(k)=pamdet(y(k),2,1);
end;

%Non-causal DFE
b_ncdfe=transpose(conv(C,fliplr(C)));
b_ncdfe(Lc)=0;
% Forward filtering
rec_seq1=conv(rec_seq,fliplr(C));
r=rec_seq1(Lc:P:end); % downsampled signal

%Feedback filtering
symb_hat_ncdfe(1:Lc-1)=symb(1:Lc-1);
symb_hat_ncdfe(N-Lc+2:N)=symb(N-Lc+2:N);
for k=Lc:N-Lc+1
y_ncdfe(k)=r(k)-symb_hat_ncdfe(k-Lc+1:1:k-1)*b_ncdfe(1:Lc-1)-symb_hat(k+1:k+Lc-1)*b_ncdfe(Lc+1:2*Lc-1);
symb_hat_ncdfe(k)=pamdet(y_ncdfe(k),2,1);
end;

% Plots & Performance measures


ser(iter)=symberror(symb,symb_hat);
ser_ncdfe(iter)=symberror(symb,symb_hat_ncdfe);

end;

SER(snr_index)=mean(ser)
SER_ncdfe(snr_index)=mean(ser_ncdfe)

end;

set(0,'DefaultAxesFontSize',18);
set(0,'DefaultTextFontSize',18);
set(0,'DefaultTextFontName','times');
set(0,'DefaultAxesFontName','times');

figure;
pl1=semilogy(snr,SER_ncdfe,'r');
hold on;
pl2=semilogy(snr,SER,'b-.');
xlabel('SNR  in  dB','FontSize',18);
ylabel('Symbol  Error  Rate','FontSize',18);
legend([pl1,pl2],'Non-causal DFE','Normal Mode DFE');
grid;

toc







