%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Experiments with OFDMA Resource Allocation %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PLOT_CHAN =0;
close all
% M = 2;                                  % No. of users
Mvec = [2:2:20];
% Nfftvec = [2,4,6,16];                  % No. of subcarriers
% colorvec = ['m','b','r','g'];
% Nfftvec = [2,256];                  % No. of subcarriers
% colorvec = ['m','b'];
Nfftvec = 256;
colorvec = 'b';
Nchan = 100;
BW = 5e6;                               % Bandwidth (Hz)
fc = 2.6e9;                             % Carrier frequency (Hz)
[Tau,gnorm] = Pdp('vehicularA');        % Power Delay Profile
c = 3e8;                                % Speed of light (m/s)
Velvec = [30, 75, 120, 180, 250]*1000/3600;
v = Velvec(2);                          % Velocity (m/s)
fd = v/c*fc;                            % Doppler Frequency (Hz)
tcoh = 1/(2*fd);                        % Coherence time (s)
lambda = 3e8/fc;                        % Wavelength (m)
fs = floor(8/7*BW/8000)*8000;           % Sampling Frequency (Hz)

% SNRvec = [5:5:20];
SNR = 0;    % dB
sigma = 10^(-SNR/20); % standard deviation

% Generate channel
% wvec = [0:0.05:1];
% wvec = linspace(0,1,5);
% wvec = [0.25];
wvec = 0;
userweight = ones(1,M);
C = zeros(M,length(wvec));
G = zeros(1,length(wvec));
Gb = G;
I = G;
Lmin = G;
L = G;
Lmax = G;

for n = 1 :length(Nfftvec),
  Nfft = Nfftvec(n);
  nidx = [-Nfft/2:Nfft/2-1]';
  W = exp(-sqrt(-1)*2*pi*fs/Nfft*nidx*Tau);
  for c = 1 : Nchan,
    c
      h = (gnorm'*userweight).*(randn(length(gnorm),M)+j*randn(length(gnorm),M))/sqrt(2);
      H = W*h;
%     H = (randn(Nfft,M)+sqrt(-1)*randn(Nfft,M))/sqrt(2);
      if PLOT_CHAN,
        figure(4)
        plot(10*log10(abs(H)))
      end;

    for i = 1:length(wvec),
%           i = 2;
%       w = [wvec(i),1-wvec(i)];
%       w = 
      [Gtmp, Gbndtmp,Itmp, Lmintmp, Ltmp, Lmaxtmp, Ctmp]=WeightedPerfectCSI(H,w,sigma,Nfft,1);
      C(:,i) = C(:,i) + Ctmp'/Nchan;
      G(i) = G(i) + Gtmp/Nchan;
      Gb(i) = Gb(i) + Gbndtmp/Nchan;
      I(i) = I(i) + Itmp/Nchan;
      Lmin(i) = Lmin(i) + Lmintmp/Nchan;
      L(i) = L(i) + Ltmp/Nchan;
      Lmax(i) = Lmax(i) + Lmaxtmp/Nchan;
    end;
  end;
  figure(1)
  plot(C(1,:),C(2,:),colorvec(n))
  hold on
  grid on
  title('Capacity Region for 2 Users')
  xlabel('Capacity for user 1 (bps/Hz)')
  ylabel('Capacity for user 2 (bps/Hz)')
  figure(2)
  semilogy(wvec,G,colorvec(n))
  hold on
  figure(3)
  plot(wvec,I,colorvec(n))
  hold on
  figure(4)
  plot(wvec,Lmin,[colorvec(n),'--'],wvec,L,[colorvec(n),'-'],wvec,Lmax,[colorvec(n),':'])
  hold on   
end;

% figure
% plot(wvec,G)
% grid on
% title('Duality Gap')
% xlabel('User 1 Weight')
% figure
% plot(wvec,10*log10(I))
% title('No. of Iterations')


