%% Fig.9 in Paper: Probability of Service Provision vs. Quantization SNR (FDD)
close all;
clear all;

% Parameters
varH = 2; % variance of channel matrix
qSNRdB = -5:8; % quantization SNR (dB)
qSNR = 10.^(qSNRdB/10); % quantization SNR
qSTEP = sqrt(6*varH./qSNR); % quantization step
nTR = [8 6 4 3 2 1]; % number of transmit and receive antennas

% Algorithms
N = 1000; % number of iterations
pSev = N*ones(6,14); % number of service provision
for a = 1:N
    for b = 1:6
        nT = nTR(b); % number of transmit antennas
        nR = nTR(b); % number of receive antennas
        H = (randn(nR,nT) + i*randn(nR,nT))*sqrt(varH/2); % actual CSI matrix
        for c = 1:14
            estH = qSTEP(c)*round(H./qSTEP(c)); % estimated CSI matrix
            if max(abs(real(estH(:)))) <= qSTEP(c)/2 && max(abs(imag(estH(:)))) <= qSTEP(c)/2
                pSev(b,c) = pSev(b,c)-1; % actual CSI matrix H=0 belongs to the uncertainty region
            end
        end
    end
end
pSev = pSev/N; % probability of service provision
save('data/probServiceFDD.mat', 'qSNRdB', 'pSev'); % save data

% Figures
output_fig = 'figure/probServiceFDD';
fig = figure;
set(fig, 'Units', 'Pixels', 'Position', [100 600 800 500]);
set(gca, 'Fontsize', 16);
hold on;
grid on;
plot(qSNRdB, pSev(1,:), '-o', qSNRdB, pSev(2,:), '-*', qSNRdB, pSev(3,:), '--o', 'linewidth', 2);
plot(qSNRdB, pSev(4,:), '--*', qSNRdB, pSev(5,:), '-.o', qSNRdB, pSev(6,:), '-.*', 'linewidth', 2);
xlim([-5 8]);
legend('n_T=8, n_R=8', 'n_T=6, n_R=6', 'n_T=4, n_R=4', ...
    'n_T=3, n_R=3', 'n_T=2, n_R=2', 'n_T=1, n_R=1', 'location', 'southeast');
xlabel('Quantization SNR (dB)');
ylabel('Probability of Service Provision');
eval(['print -depsc2 ' output_fig]);