%%%%%%%%%%%%%%%%%%%%ADAPTIVE EQUALIZER%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%TRAINING MODE and DECISION DIRECTED MODE%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% 3rd DEGREE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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)));
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')


v = 0.0316 * randn(1,5002);

u = conv(x,h);

y = u + v;

mu = 0.08;

param = 0;
c0(3) = param;
c1(3) = param;
c2(3) = param;


for n=3:1000
    e(n) = x(n-2) - [c0(n) c1(n) c2(n)] * [y(n); y(n-1); y(n-2)];
    
    xes(n-2) = [c0(n) c1(n) c2(n) ] * [y(n); y(n-1); y(n-2)];
          
    if xes(n-2) > 0.5
        xes2(n-2) = 1;
    elseif xes(n-2) < 0.5
        xes2(n-2) = 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);
    
    
end


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

for i = 1:998
    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) ],512))), title('magnitude spectrum of equalizer at 1000th sample'), xlabel('frequncy between 0-2*pi'), ylabel('amplitude')
subplot(212), plot([c0(1000), c1(1000), c2(1000)]), title('impulse response of the filter at 1000th filter'), xlabel('time'), ylabel('amplitude')



mu2 = .0003;

for n=1000:5000
    e(n) = xes2(n-2) - [c0(n) c1(n) c2(n)] * [y(n); y(n-1); y(n-2)];
    
    xes(n-1) = [c0(n) c1(n) c2(n) ] * [y(n); y(n-1); y(n-2)];
          
    if xes(n-1) > 0.5
        xes2(n-1) = 1;
    elseif xes(n-1) < 0.5
        xes2(n-1) = 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);
    
    
end

figure 
subplot(411), plot(x(1001:4994)); title('the sent signal')
subplot(412), plot(y(1001:4994)); title('observed signal')
subplot(413), plot(xes(1002:4995)); title('equalizer output before slicer after 5000 sample')
subplot(414), plot(xes2(1002:4995)); title('equalizer output after slicer after 5000 sample')
    

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


figure, 
subplot(211), plot(error1(1001:4995)), title('error signal after slicer');
subplot(212), plot(error2(1001:4995)), title('error signal before slicer');axis([1 3994 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) ],512))), title('magnitude spectrum of equalizer at 5000th sample'), xlabel('frequncy between 0-2*pi'), ylabel('amplitude')
subplot(212), plot([c0(5000), c1(5000), c2(5000)]), title('impulse response of the filter at 5000th filter'), xlabel('time'), ylabel('amplitude')
