clc;clear;
%% Parameters
M = 128;
% N = 20;
% G = 64;
M1 = 16;
M2 = 8;
K = 4;
P = 4;
b = 2;
B0 = 8;
loop_num = 100;
loop_num1 = loop_num;
SNR_table = 0:3:12;
B_table = zeros(size(SNR_table));
d = 0.5;
A = zeros(P,M,K);
A_hat = zeros(P,M,K);
Rs0=zeros(loop_num,length(SNR_table));
Rs1=zeros(loop_num,length(SNR_table));
Rs2=zeros(loop_num,length(SNR_table));
Rs3=zeros(loop_num,length(SNR_table));
% Rs4=zeros(loop_num,length(SNR_table));
R_t_sum= zeros(M,M);
U = zeros(M,M,K);
R_sqt =zeros(M,M,K);
sin_theta_table = zeros(P,K);
cos_theta_table = zeros(P,K);
sin_psi_table = zeros(P,K);
%% Channel generation
for i_K=1:1:K
theta = random('unif',-1/2,1/2,P,1)*pi;
psi = random('unif',-1/2,1/2,P,1)*pi;
sin_theta_table(:,i_K)=sin(theta);
sin_psi_table(:,i_K)=sin(psi);
cos_theta_table(:,i_K)=cos(theta);
end
for i_loop=1:loop_num1
H=zeros(K,M);
for i_K=1:1:K
for i_P=1:1:P
a1 = exp(cos_theta_table(i_P,i_K)*sin_psi_table(i_P,i_K)*(-1j*2*pi*d*[0:1:M1-1]));
a2 = exp(sin_theta_table(i_P,i_K)*(-1j*2*pi*d*[0:1:M2-1]));
A(i_P,:,i_K) = kron(a1,a2);
end
g = randn(1,P) + 1j*randn(1,P);
h = g*A(:,:,i_K);
H(i_K,:) = h;
end
R_t_sum(:,:) = R_t_sum(:,:) + H' * H;
end
R_t = R_t_sum/loop_num1;
for i_K=1:1:K
[U(:,:,i_K),S,V] = svd(R_t(:,:));
R_sqt(:,:,i_K) = U(:,:,i_K)*sqrt(S)*U(:,:,i_K)';
end
for i_loop=1:loop_num
%% Channel generation
H=zeros(K,M);
for i_K=1:1:K
sin_theta = sin_theta_table(:,i_K);
sin_theta_quant = UQ(sin_theta,B0,-1,1);
sin_psi = sin_psi_table(:,i_K);
sin_psi_quant = UQ(sin_psi,B0,-1,1);
cos_theta = cos_theta_table(:,i_K);
cos_theta_quant = UQ(cos_theta,B0,-1,1);
for i_P=1:1:P
a1 = exp(cos_theta_table(i_P,i_K)*sin_psi_table(i_P,i_K)*(-1j*2*pi*d*[0:1:M1-1]));
a2 = exp(sin_theta_table(i_P,i_K)*(-1j*2*pi*d*[0:1:M2-1]));
A(i_P,:,i_K) = kron(a1,a2);
end
for i_P=1:1:P
a1_hat = exp(cos_theta_quant(i_P)*sin_psi_quant(i_P)*(-1j*2*pi*d*[0:1:M1-1]));
a2_hat = exp(sin_theta_quant(i_P)*(-1j*2*pi*d*[0:1:M2-1]));
A_hat(i_P,:,i_K) = kron(a1_hat,a2_hat);
end
g = randn(1,P) + 1j*randn(1,P);
h= g*A(:,:,i_K);
H(i_K,:) = h;
end
for i_SNR=1:1:length(SNR_table)
SNR = SNR_table(i_SNR);
snr=10^(SNR/10);
norm_sq_sum=0;
for i_K=1:1:K
norm_sq_sum = norm_sq_sum + norm(H(i_K,:))^2;
end
norm_h_sq=norm_sq_sum/K;
% norm_h_sq=M;
Gamma = snr*K/norm_h_sq;
% B = round((P-1)/3*SNR+(P-1)*log2((K-1)/(b-1))) - B_delta;
B = 8;
B_table(i_SNR) = B;
%% RVQ
[H_RVQ,err_RVQ]= quantiz_RVQ_real(H, B);
%% SC
[H_AC,err_AC] = quantiz_SC_real(H,A_hat,B-3);
%% CRVQ
[H_CRVQ,err_CRVQ1] = quantiz_CRVQ_real(H,R_sqt,B);
% %% CS
% Phi = randn(M,N);
% H_til = H * Phi;
% [H_til_CS,err_CS] = quantiz_CS_real(H_til,R_sqt,Phi,B);
% H_CS = zeros(K,M);
% for i_K=1:1:K
% h_til_CS = H_til_CS(i_K,:);
% s_ori = H(i_K,:) * U(:,:,i_K);
% s = OMP(h_til_CS',(U(:,:,i_K)'*Phi)',P);
% H_CS(i_K,:) = s' * U(:,:,i_K)';
% end
% %% AG
% GP = zeros(M,G);
% for i=1:1:G
% GP(2*i-1,i)=1;
% GP(2*i,i)=1;
% end
% EP=GP'/2;
% Hr = H*GP;
% for i=1:1:K
% AGP(:,:,i) = A(:,:,i)*GP;
% end
% Hr_AG = quantiz_AC_real(Hr,AGP,B);
% % Hr_AG = quantiz_RVQ_real(Hr,B);
% H_CS = Hr_AG * EP;
%% RS
HK=H;
HK1= H_RVQ;
HK2 = H_AC;
HK3 = H_CRVQ;
% HK4 = H_CS;
% HK5(i_K,:) = H_squant5;
% precode
% W0=randn(M,K) + j*randn(M,K);
W0 = HK'/(HK*HK');
% W0=HK';
W0 = W0./repmat(sqrt(sum(abs(W0).^2,1)),M,1);
W1 = HK1'/(HK1*HK1');
% W1=HK1';
W1 = W1./repmat(sqrt(sum(abs(W1).^2,1)),M,1);
W2 = HK2'/(HK2*HK2');
% W2=HK2';
W2 = W2./repmat(sqrt(sum(abs(W2).^2,1)),M,1);
W3 = HK3'/(HK3*HK3');
% W3=HK3';
W3 = W3./repmat(sqrt(sum(abs(W3).^2,1)),M,1);
% W4 = HK4'/(HK4*HK4');
% % W4=HK4';
% W4 = W4./repmat(sqrt(sum(abs(W4).^2,1)),M,1);
% W5 = HK5'/(HK5*HK5');
% W5 = W5./repmat(sqrt(sum(abs(W5).^2,1)),M,1);
p=Gamma/K;
for i_K = 1:K
SINR0 = p*norm(HK(i_K,:)*W0(:,i_K))^2/(1 + p*HK(i_K,:)*W0*(HK(i_K,:)*W0)'-p*norm(HK(i_K,:)*W0(:,i_K))^2);
SINR1 = p*norm(HK(i_K,:)*W1(:,i_K))^2/(1 + p*HK(i_K,:)*W1*(HK(i_K,:)*W1)'-p*norm(HK(i_K,:)*W1(:,i_K))^2);
SINR2 = p*norm(HK(i_K,:)*W2(:,i_K))^2/(1 + p*HK(i_K,:)*W2*(HK(i_K,:)*W2)'-p*norm(HK(i_K,:)*W2(:,i_K))^2);
SINR3 = p*norm(HK(i_K,:)*W3(:,i_K))^2/(1 + p*HK(i_K,:)*W3*(HK(i_K,:)*W3)'-p*norm(HK(i_K,:)*W3(:,i_K))^2);
% SINR4 = p*norm(HK(i_K,:)*W4(:,i_K))^2/(1 + p*HK(i_K,:)*W4*(HK(i_K,:)*W4)'-p*norm(HK(i_K,:)*W4(:,i_K))^2);
% SINR5 = p*norm(HK(i_K,:)*W5(:,i_K))^2/(1 + p*HK(i_K,:)*W5*(HK(i_K,:)*W5)'-p*norm(HK(i_K,:)*W5(:,i_K))^2);
Rs0(i_loop,i_SNR) = Rs0(i_loop,i_SNR)+log2(1+SINR0);
Rs1(i_loop,i_SNR) = Rs1(i_loop,i_SNR)+log2(1+SINR1);
Rs2(i_loop,i_SNR) = Rs2(i_loop,i_SNR)+log2(1+SINR2);
Rs3(i_loop,i_SNR) = Rs3(i_loop,i_SNR)+log2(1+SINR3);
% Rs4(i_loop,i_SNR) = Rs4(i_loop,i_SNR)+log2(1+SINR4);
end
fprintf('i_loop=%d,i_SNR=%d\n',i_loop,i_SNR);
end
end
mRs0=mean(Rs0,1)/K;
mRs1=mean(Rs1,1)/K;
mRs2=mean(Rs2,1)/K;
mRs3=mean(Rs3,1)/K;
% mRs4=mean(Rs4,1)/K;
% mRs5=mean(Rs5,1)/K;
%% plot
figure;
plot(SNR_table,mRs0,'k--','LineWidth',1.5);
hold on;
plot(SNR_table,mRs2,'rd-','LineWidth',1.5);
hold on;
plot(SNR_table,mRs3,'bs-','LineWidth',1.5);
% hold on;
% plot(SNR_table,mRs4,'g^-','LineWidth',1.5);
% hold on;
% plot(SNR_table,mRs1,'mo-','LineWidth',1.5);
% hold on;
% plot(B_table,mRs5,'ms-','LineWidth',1.5);
% had=legend('$Perfect$','$RVQ$','$DC(\mathbf{A}=\hat{\mathbf{A}})$','$DC(\mathbf{A}\neq\hat{\mathbf{A}})$');
% set(had,'Interpreter','latex')
legend('Perfect CSIT','Proposed AoD-adaptive subspace codebook','Conventional channel statistics-based codebook [12]');
% axis([3 15 0 4]);
grid on;
xlabel('SNR (dB)');
ylabel('Per-user rate (bps/Hz)');