% Channel 2 
% Shengli Zhou, 11/20/2002

f2=[0.407, 0.815, 0.407]';
f=f2;

EbN0dB=[0:2:35];
Eb=1;
N0s=Eb./(10.^(EbN0dB/10));
Ks=[5, 10, 20, 40];

for ik=1:length(Ks)
    K=Ks(ik);
    Length=2*K+1;
    for isnr=1:length(EbN0dB);
   	N0=N0s(isnr);
 	L=length(f)-1;
        x=xcorr(f); x=x(:)';
        Row=[x(L+1:end), zeros(1,Length-L-1)];
        % BPSK constellation, we restrict the noise to be real
        Gam=toeplitz(Row)+N0/2*eye(Length);
        xi=[zeros(K-L,1); fliplr(f); zeros(K,1)];
        rho2=xi'*(Gam\xi);
	SNR=rho2/(1-rho2);
	BER(ik,isnr)=Q(sqrt(SNR));
	BER_awgn(isnr)=Q(sqrt(2*1/N0));
    end
end
        
figure(1)
semilogy(EbN0dB,BER(1,:),'k--'); hold on
semilogy(EbN0dB,BER(2,:),'k-'); hold on
semilogy(EbN0dB,BER(3,:),'k-'); hold on
semilogy(EbN0dB,BER(4,:),'k-'); hold on
semilogy(EbN0dB,BER_awgn,'k'); hold on
hold off
xlabel('SNR');
ylabel('Probability of Error');
axis([0 35 1e-4 4e-1]);
legend('K=5','K=10','K=20','K=40','AWGN');
title('Channel b), Performance versus equalizer length');

