clear all;
clc;
Nt =4;
K = 2;
Nri = 2;
S = 2;
Tc = 200;
sigma2=1;
SNRdbs=-10:3:30;
sum_rate1=zeros(1,length(SNRdbs));
sum_rate2=zeros(1,length(SNRdbs));
sum_rate3=zeros(1,length(SNRdbs));
sum_rate4=zeros(1,length(SNRdbs));
sum_rate5=zeros(1,length(SNRdbs));
sum_rate6=zeros(1,length(SNRdbs));
BER1=zeros(1,length(SNRdbs));
BER2=zeros(1,length(SNRdbs));
BER3=zeros(1,length(SNRdbs));
BER4=zeros(1,length(SNRdbs));
BER5=zeros(1,length(SNRdbs));
BER6=zeros(1,length(SNRdbs));
for i_SNR=1:length(SNRdbs)
SNR=10^(SNRdbs(1,i_SNR)/10);
P=sigma2*SNR;
count1=0;
count2=0;
count3=0;
count4=0;
count5=0;
count6=0;
capacity1=0;
capacity2=0;
capacity3=0;
capacity4=0;
capacity5=0;
capacity6=0;
loop_num=400;
for i_loop=1:loop_num
q=randint(K*S,Tc);
s=QPSK_mapper(q,Tc,S,K);
H_real=randn(K*Nri,Nt);
H_imag=randn(K*Nri,Nt);
H=(1/sqrt(2))*complex(H_real,H_imag);
n_real=sqrt(sigma2/2)*randn(K*Nri,Tc/2);
n_imag=sqrt(sigma2/2)*randn(K*Nri,Tc/2);
n=complex(n_real,n_imag);
%svd
[W1,M1]=svdprecoding(H,K,S,P);
[U,D,V]=svd(H);
p1 = ones(1,K*S);
% p1 = waterfilling(D,SNR,Nt,K,S);
[count_temp] = receiver(H,Nt,S,K,Tc,P,n,W1,M1,s,q);
count1=count1+count_temp;
G=M1*H*W1;
capacity1=capacity1+log2(det(eye(K*S,K*S)+P/(sigma2*Nt)*G*G'));
% BD
[W2,D2 M2] = BD2(Nt,Nri,S,K,H,P);
p2 = ones(1,K*S);
[count_temp] = receiver(H,Nt,S,K,Tc,P,n,W2,M2,s,q);
count2=count2+count_temp;
capacity2=capacity2+sumrate(H,W2,M2,K,S,Nri,Nt,sigma2,P);
%ZF
[W3]=ZF(H,P,K,S);
[count_temp] = receiver(H,Nt,S,K,Tc,P,n,W3,eye(K*Nri),s,q);
count3=count3+count_temp;
capacity3=capacity3+sumrate(H,W3,eye(K*Nri),K,S,Nri,Nt,sigma2,P);
%MF
[W4]=MF(H,P,K,S);
[count_temp] = receiver(H,Nt,S,K,Tc,P,n,W4,eye(K*Nri),s,q);
count4=count4+count_temp;
capacity4=capacity4+sumrate(H,W4,eye(K*Nri),K,S,Nri,Nt,sigma2,P);
%SLNR
%[W5] = SLNR(Nt,Nri,S,K,H,sigma2,P);
[W5]=SLNR2(H,sigma2,P,K,Nri,S,Nt);
[count_temp] = receiver(H,Nt,S,K,Tc,P,n,W5,eye(K*Nri),s,q);
count5=count5+count_temp;
capacity5=capacity5+sumrate(H,W5,eye(K*Nri),K,S,Nri,Nt,sigma2,P);
%MMSE
[W6,M6]=MMSE(H,sigma2,P,K,Nri,S,Nt);
[count_temp] = receiver(H,Nt,S,K,Tc,P,n,W6,M6,s,q);
count6=count6+count_temp;
capacity6=capacity6+sumrate(H,W6,M6,K,S,Nri,Nt,sigma2,P);
end
BER1(1,i_SNR)=count1/(loop_num*K*S*Tc);
BER2(1,i_SNR)=count2/(loop_num*K*S*Tc);
BER3(1,i_SNR)=count3/(loop_num*K*S*Tc);
BER4(1,i_SNR)=count4/(loop_num*K*S*Tc);
BER5(1,i_SNR)=count5/(loop_num*K*S*Tc);
BER6(1,i_SNR)=count6/(loop_num*K*S*Tc);
sum_rate1(1,i_SNR)=capacity1/loop_num/K;
sum_rate2(1,i_SNR)=capacity2/loop_num/K;
sum_rate3(1,i_SNR)=capacity3/loop_num/K;
sum_rate4(1,i_SNR)=capacity4/loop_num/K;
sum_rate5(1,i_SNR)=capacity5/loop_num/K;
sum_rate6(1,i_SNR)=capacity6/loop_num/K;
end
figure(1)
semilogy(SNRdbs,BER1(1,:),'b-o');
hold on;
semilogy(SNRdbs,BER2(1,:),'r-<');
hold on;
semilogy(SNRdbs,BER3(1,:),'c-p');
hold on;
semilogy(SNRdbs,BER4(1,:),'k-s');
hold on;
semilogy(SNRdbs,BER5(1,:),'y-*');
grid on;
semilogy(SNRdbs,BER6(1,:),'g-o');
grid on;
legend('SVD','BD','ZF','MF','SLNR','MMSE');
xlabel('SNR (dB)');
ylabel('BER');
hold on;
title('SNR=10dB时2个2天线用户MIMO系统误码率随Nt变化');
figure(2)
plot(SNRdbs,sum_rate1(1,:),'b-o');
hold on;
plot(SNRdbs,sum_rate2(1,:),'r-<');
hold on;
plot(SNRdbs,sum_rate3(1,:),'c-p');
hold on;
plot(SNRdbs,sum_rate4(1,:),'k-s');
hold on;
plot(SNRdbs,sum_rate5(1,:),'y-*');
grid on;
plot(SNRdbs,sum_rate6(1,:),'g-o');
grid on;
legend('SVD','BD','ZF','MF','SLNR','MMSE');
xlabel('SNR (dB)');
ylabel('合速率(bit/s/用户)');
hold on;
title('SNR=10dB时2个2天线用户MIMO系统合速率随Nt变化');