%%%%%%%%%%%%%%%%%%%%ADAPTIVE EQUALIZER%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%TRAINING MODE and DECISION DIRECTED MODE%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% 11th DEGREE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%% Channel Vales [0.2, -0.15, 1.0, 0.21, 0.03] %%%%%%%%%%

% Designed by Hulya Seferoglu
% Faculty of Engineering and Natural Science
% Sabanci University, Istanbul, Turkey 
% Spring 2004

clear; close all;

% source produces 1's and 0's according to bernoulli random process 
% with probability of producing 1 is p = 0.5
p = .5;

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

% the channel has a spectra having nulls on the frequency domain

h = [0.2, -0.15, 1.0, 0.21, 0.03];
ww = 0:2*pi/128:2*pi;
ww = ww(1:128);
figure, plot(ww, abs(fft(h,128))); title('channel frequency response'), xlabel('frequncy between 0-2*pi'), ylabel('amplitude')

% noise is gaussian with mean 0 and variance 

v = 0.116 * rand(1,5004);

u = conv(x,h);


y = u + v;

mu = 0.08;

param = 0;
c0(11) = param;
c1(11) = param;
c2(11) = param;
c3(11) = param;
c4(11) = param;
c5(11) = param;
c6(11) = param;
c7(11) = param;
c8(11) = param;
c9(11) = param;
c10(11) = param;



for n=11:1000
    e(n) = x(n-6) - [c0(n), c1(n), c2(n), c3(n), c4(n), c5(n), c6(n), c7(n), c8(n), c9(n), c10(n)] * [y(n); y(n-1); y(n-2); y(n-3); y(n-4); y(n-5); y(n-6); y(n-7); y(n-8); y(n-9); y(n-10)];
    
    xes(n-6) = [c0(n) c1(n) c2(n) c3(n) c4(n) c5(n) c6(n) c7(n) c8(n) c9(n) c10(n)] * [y(n); y(n-1); y(n-2); y(n-3); y(n-4); y(n-5); y(n-6); y(n-7); y(n-8); y(n-9); y(n-10)];
          
    if xes(n-6) > 0.5
        xes2(n-6) = 1;
    elseif xes(n-6) < 0.5
        xes2(n-6) = 0;
    end
    
    c0(n+1) = c0(n) + mu * e(n) * y(n);
    c1(n+1) = c1(n) + mu * e(n) * y(n-1);
    c2(n+1) = c2(n) + mu * e(n) * y(n-2);
    c3(n+1) = c3(n) + mu * e(n) * y(n-3);
    c4(n+1) = c4(n) + mu * e(n) * y(n-4);
    c5(n+1) = c5(n) + mu * e(n) * y(n-5);
    c6(n+1) = c6(n) + mu * e(n) * y(n-6);
    c7(n+1) = c7(n) + mu * e(n) * y(n-7);
    c8(n+1) = c8(n) + mu * e(n) * y(n-8);
    c9(n+1) = c9(n) + mu * e(n) * y(n-9);
    c10(n+1) = c10(n) + mu * e(n) * y(n-10);
    
    
  
    
end


figure 
subplot(411), plot(x(1:1000)); title('sent sequence Ak')
subplot(412), plot(y(1:1000)); title('the sequence passed through the channel and noise is added, Rk')
subplot(413), plot(xes); title('estimated sequence before slicer, Qk')
subplot(414), plot(xes2); title('estimated sequence after slicer, A_hat_k') 

for i = 1:990
    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');

www = 0:2*pi/512:2*pi;
www= www(1:512);

figure, 
subplot(211), plot(www, abs(fft([c0(1000), c1(1000), c2(1000), c3(1000), c4(1000), c5(1000), c6(1000), c7(1000), c8(1000), c9(1000), c10(1000)],512))); title('frequence response of equalizer at 1000th sample')
subplot(212), plot([c0(1000), c1(1000), c2(1000), c3(1000), c4(1000), c5(1000), c6(1000), c7(1000), c8(1000), c9(1000), c10(1000)]); title('impulse response of equalizer at 1000th sample')


mu2 = 0.0003;

for n=1000:5000
    e(n) = xes2(n-6) - [c0(n), c1(n), c2(n), c3(n), c4(n), c5(n), c6(n), c7(n), c8(n), c9(n), c10(n)] * [y(n); y(n-1); y(n-2); y(n-3); y(n-4); y(n-5); y(n-6); y(n-7); y(n-8); y(n-9); y(n-10)];
    
    xes(n-5) = [c0(n) c1(n) c2(n) c3(n) c4(n) c5(n) c6(n) c7(n) c8(n) c9(n) c10(n)] * [y(n); y(n-1); y(n-2); y(n-3); y(n-4); y(n-5); y(n-6); y(n-7); y(n-8); y(n-9); y(n-10)];
          
    if xes(n-5) > 0.5
        xes2(n-5) = 1;
    elseif xes(n-5) < 0.5
        xes2(n-5) = 0;
    end
    
    c0(n+1) = c0(n) + mu2 * e(n) * y(n);
    c1(n+1) = c1(n) + mu2 * e(n) * y(n-1);
    c2(n+1) = c2(n) + mu2 * e(n) * y(n-2);
    c3(n+1) = c3(n) + mu2 * e(n) * y(n-3);
    c4(n+1) = c4(n) + mu2 * e(n) * y(n-4);
    c5(n+1) = c5(n) + mu2 * e(n) * y(n-5);
    c6(n+1) = c6(n) + mu2 * e(n) * y(n-6);
    c7(n+1) = c7(n) + mu2 * e(n) * y(n-7);
    c8(n+1) = c8(n) + mu2 * e(n) * y(n-8);
    c9(n+1) = c9(n) + mu2 * e(n) * y(n-9);
    c10(n+1) = c10(n) + mu2 * e(n) * y(n-10);
    
    
end

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

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


figure, 
subplot(211), plot(error1(1001:4990)); title('error signal after slicer');
subplot(212), plot(error2(1001:4990)); title('error signal before slicer'); axis([1 3990 0 1])

www = 0:2*pi/512:2*pi;
www= www(1:512);


figure, 
subplot(211), plot(www, abs(fft([c0(5000), c1(5000), c2(5000), c3(5000), c4(5000), c5(5000), c6(5000), c7(5000), c8(5000), c9(5000), c10(5000)],512))); title('frequence response of equalizer at 5000th sample')
subplot(212), plot([c0(5000), c1(5000), c2(5000), c3(5000), c4(5000), c5(5000), c6(5000), c7(5000), c8(5000), c9(5000), c10(5000)]); title('impulse response of equalizer at 5000th sample')