bias=0;samp=1;
Lf1=4; Lb1=1; Lf2=8; Lb2=1;
M=2; P=2; 
%method1='joint';method2='joint';
method1='joint_sic';method2='joint_sicrev';
%method1='joint_sicrev';method2='joint_sic';
block_num=1000; % Number of packets
N=1000; %Size of the packet
rand('seed',0);

% Channels
C1 = [1 -0.9;-0.4 1;1 0.9;0.4 1]; % 1 dB improvement
C2 = [-0.4 1;1 -0.9;0.4 1;1 0.9]; % Lf1=4;Lf2=8

Lc=size(C1,1)/M;

for m=1:M,
  for p=1:P,
  c=C1((m-1)*Lc+1:m*Lc,p);
  C1((m-1)*Lc+1:m*Lc,p)=c/norm(c);
  c=C2((m-1)*Lc+1:m*Lc,p);
  C2((m-1)*Lc+1:m*Lc,p)=c/norm(c);  
end;
end;

snr=3:1:13;

for snr_ind=1:length(snr),

snr(snr_ind)

lambda=10^(-snr(snr_ind)/10);

[F1, B1, delay1, Mse1] = dfe_tap_estmimo(C1, Lf1, Lb1, M, P, lambda*eye(P*Lf1), method1, bias, samp);
[F2, B2, delay2, Mse2] = dfe_tap_estmimo(C2, Lf2, Lb2, M, P, lambda*eye(P*Lf2), method2, bias, samp);
[Mse,Alpha]=bidfe_mse_mimo(C1,F1,B1,delay1,method1,C2,F2,B2,delay2,method2,lambda,M,P);

% Simulate SER...
[ser,ser_tr,ser_soft,ser_soft_tr,ser_ideal,ser_ideal_tr,ser_bidfe,ser_soft_bidfe]=bidfe_ser_mimo(C1,F1,B1,delay1,method1,Mse1,C2,F2,B2,delay2,method2,Mse2,Alpha,snr(snr_ind),M,P,block_num,N);

SER(:,snr_ind)=mean(ser,2);
SER_tr(:,snr_ind)=mean(ser_tr,2);
SER_soft(:,snr_ind)=mean(ser_soft,2);
SER_soft_tr(:,snr_ind)=mean(ser_soft_tr,2);
SER_ideal(:,snr_ind)=mean(ser_ideal,2);
SER_ideal_tr(:,snr_ind)=mean(ser_ideal_tr,2);
SER_bidfe(:,snr_ind)=mean(ser_bidfe,2);
SER_soft_bidfe(:,snr_ind)=mean(ser_soft_bidfe,2);

end;

% Plot figures

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

for m=1:M,
figure(m);
pl1=semilogy(snr,SER(m,:),'b-.','LineWidth',2);
hold on;
pl2=semilogy(snr,SER_tr(m,:),'m-o','LineWidth',2);
pl3=semilogy(snr,SER_ideal(m,:),'r-*','LineWidth',2);
pl4=semilogy(snr,SER_bidfe(m,:),'k-^','LineWidth',2);
pl5=semilogy(snr,SER_soft_bidfe(m,:),'g-v','LineWidth',2);
grid on;
legend([pl1,pl2,pl3,pl4,pl5],'Normal Mode DFE','Time-reversal mode DFE','Ideal DFE','MIMO BiDFE - hard','MIMO BiDFE - soft');
xlabel('SNR  in  dB','FontSize',18);
ylabel('Symbol  Error  Rate','FontSize',18);
end;

save results_bidfemimo_c1 C1 C2 Lf1 Lf2 Lb1 Lb2 method1 method2 M P snr block_num N SER SER_tr SER_ideal SER_ideal_tr SER_soft SER_soft_tr SER_bidfe SER_soft_bidfe 







