%%%%%%%%%%%%%%%%%%%%%DECISION FEEDBACK EQUALIZER%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%TRAINING MODE + DECISION DIRECTED MODE%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% DEGREE: 5 for PRECURSOR EQUALIZER %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% DEGREE: 3 for POSTCURSOR EQUALIZER %%%%%%%%%%%%%%%%%%%
% Designed by Hulya Seferoglu
% Faculty of Engineering and Natural Science
% Sabanci University, Istanbul, Turkey 
% Spring 2004

clear; close all;

p = .5;
x = binornd(1, p, 1, 5000);

n = 1:3;

W = 3.5;
h = (1/2) * (1 + cos((2*(pi/W))*(n-2)));
figure, plot(abs(fft(h,128))); title('channel frequency response')

v = 0.0316 * randn(1,5002);

u = conv(x,h);

y = u + v;




mu1 = 0.08;
mu2 = 0.00008;

param = 0;
c0(5) = param;
c1(5) = param;
c2(5) = param;
c3(5) = param;
c4(5) = param;

d1(5) = param;
d2(5) = param;
d3(5) = param;


for n=5:997
    e(n) = x(n) - [c4(n) c3(n) c2(n) c1(n) c0(n) -d1(n) -d2(n) -d3(n)] * [y(n+4); y(n+3); y(n+2); y(n+1); y(n); x(n-1); x(n-2); x(n-3)];
    
    xes(n) = [c4(n) c3(n) c2(n) c1(n) c0(n) -d1(n) -d2(n) -d3(n)] * [y(n+4); y(n+3); y(n+2); y(n+1); y(n); x(n-1); x(n-2); x(n-3)];
          
    if xes(n) > 0.5
        xes2(n) = 1;
    elseif xes(n) < 0.5
        xes2(n) = 0;
    end
    c4(n+1) = c4(n) + mu1 * e(n) * y(n+4);
    c3(n+1) = c3(n) + mu1 * e(n) * y(n+3);
    c2(n+1) = c2(n) + mu1 * e(n) * y(n+2);
    c1(n+1) = c1(n) + mu1 * e(n) * y(n+1);
    c0(n+1) = c0(n) + mu1 * e(n) * y(n);
    d1(n+1) = d1(n) + mu2 * e(n) * x(n-1);
    d2(n+1) = d2(n) + mu2 * e(n) * x(n-2);
    d3(n+1) = d3(n) + mu2 * e(n) * x(n-3);
    
end


figure 
subplot(411), plot(x); title('the sent signal')
subplot(412), plot(y); title('observed signal')
subplot(413), plot(xes); title('equalizer output before slicer for first 997 sample')
subplot(414), plot(xes2); title('equalizer output after slicer for first 997 sample')
    

for i = 1:997
    error1(i) = (x(i) - xes2(i))^2;
    error2(i) = (x(i) - xes(i))^2;
end

figure,
subplot(211), plot(error1); title('error signal after slicer');
subplot(212), plot(error2); title('error signal before slicer')


figure, 
subplot(211), plot(abs(fft([c0(997), c1(997), c2(997), c3(997), c4(997) ],512)));  title('magnitude spectrum of precursor equalizer at 997th sample')
subplot(212), plot([c0(997), c1(997), c2(997), c3(997), c4(997)]);  title('impulse response of precursor equalizer at 997th sample')

figure, 
subplot(211), plot(abs(fft([d1(997), d2(997), d3(997) ],512)));  title('magnitude spectrum of postcursor equalizer at 997th sample')
subplot(212), plot([d1(997), d2(997), d3(997)]); title('impulse response of postcursor equalizer at 997th sample')




mu1 = 0.000001;
mu2 = 0.00002;

for n=997:4997
    e(n) = xes2(n) - [c4(n) c3(n) c2(n) c1(n) c0(n) -d1(n) -d2(n) -d3(n)] * [y(n+4); y(n+3); y(n+2); y(n+1); y(n); xes2(n-1); xes2(n-2); xes2(n-3)];
    
    xes(n+1) = [c4(n) c3(n) c2(n) c1(n) c0(n) -d1(n) -d2(n) -d3(n)] * [y(n+4); y(n+3); y(n+2); y(n+1); y(n); xes2(n-1); xes2(n-2); xes2(n-3)];
          
    if xes(n+1) > 0.5
        xes2(n+1) = 1;
    elseif xes(n+1) < 0.5
        xes2(n+1) = 0;
    end
    c4(n+1) = c4(n) + mu1 * e(n) * y(n+4);
    c3(n+1) = c3(n) + mu1 * e(n) * y(n+3);
    c2(n+1) = c2(n) + mu1 * e(n) * y(n+2);
    c1(n+1) = c1(n) + mu1 * e(n) * y(n+1);
    c0(n+1) = c0(n) + mu1 * e(n) * y(n);
    d1(n+1) = d1(n) + mu2 * e(n) * xes(n-1);
    d2(n+1) = d2(n) + mu2 * e(n) * xes(n-2);
    d3(n+1) = d3(n) + mu2 * e(n) * xes(n-3);
    
end


figure 
subplot(411), plot(x); title('the sent signal')
subplot(412), plot(y); title('observed signal')
subplot(413), plot(xes); title('equalizer output before slicer')
subplot(414), plot(xes2); title('equalizer output after slicer')
    

for i = 998:4495
    error1(i) = (x(i-1) - xes2(i))^2;
    error2(i) = (x(i-1) - xes(i))^2;
end

figure,
subplot(211), plot(error1); title('error signal after slicer');
subplot(212), plot(error2); title('error signal before slicer');



figure, 
subplot(211), plot(abs(fft([c0(4997), c1(4997), c2(4997), c3(4997), c4(4997) ],512)));  title('magnitude spectrum of precursor equalizer at 4997th sample')
subplot(212), plot([c0(4997), c1(4997), c2(4997), c3(4997), c4(4997)]);  title('impulse response of precursor equalizer at 4997th sample')

figure, 
subplot(211), plot(abs(fft([d1(4997), d2(4997), d3(4997) ],512)));  title('magnitude spectrum of postcursor equalizer at 4997th sample')
subplot(212), plot([d1(4997), d2(4997), d3(4997)]); title('impulse response of postcursor equalizer at 4997th sample')