%%%%%%%%%%%%%%%%%%%%%%%%%%% Bit loading Module %%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the Users' bit loading capacity of three kinds of links
% According to the Real SINR with Fastfading or MCS
% Author: Cao Lu
% Date: 20090114
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [nARQTransmitProcess,nUEScheduler,RelayUser_Scheduled]=bit_loading(t_index,dBS2UE_LargeScale,dBS2RN_LargeScale,...
dRN2UE_LargeScale,nBSScheduler,nRNScheduler_TS1,nRNScheduler_TS2,nUEScheduler,dSNR_gap,dLenthofSubframe,...
nTrafficBufferAtBS,nARQTransmitProcess,ServingSector,ServingSector_Relay,nNumofCell,nNumofSector,nTotalNumofUser,nNumofRelayPerSector,...
nTotalNumofRelay,nNumofRB,nNumofUserPerSector,nNumofRelayPerCell,dwPerSubcarrierWhiteNoise,dHzSubcarrierBandWidth,nNumofSubcarrierPerRB,...
ddBRNNoisefigure,ddBUENoisefigure,dFastFading,pdusize,Mbest_TS1,Mbest_TS2,nSCRNumPerRBinDefination,granularity,RelayUser_Scheduled)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 15种调制方式对应的码速率表(由高到低)
% coderate=[pdusize*2*1/3,pdusize*2*1/2,pdusize*2*2/3,pdusize*4*1/3,pdusize*2*3/4,pdusize*2*4/5,pdusize*4*1/2,pdusize*6*1/3,...
% pdusize*4*2/3,pdusize*4*3/4,pdusize*6*1/2,pdusize*4*4/5,pdusize*6*2/3,pdusize*6*3/4,pdusize*6*4/5];
%β为1*15的矩阵,各种调制方式的尺度因子
bita=[1.49,1.57,1.69,3.36,1.69,1.65,4.56,9.2047,6.42,7.33,13.7614,7.68,20.57,25.16,28.38];
%%%% 修改后的coderate(每RB的速率):
SINRTg=[-0.7382,1.4708,3.4776,3.9652,4.4791,5.2177,6.8587,7.8397,9.3914,10.4850,11.275,11.3776,14.3897,15.8813,16.8015];
for m=1:length(SINRTg)
coderate(m)=dHzSubcarrierBandWidth.*nSCRNumPerRBinDefination.*log2(1+10^(SINRTg(m)./10)./dSNR_gap).*dLenthofSubframe/2;
end
%%%%
dBS2UE_PowerReceived = zeros(nNumofSector,nNumofRB,nNumofSubcarrierPerRB); % 直连情况,UE在子载波的接收功率
dBS2RN_PowerReceived = zeros(nNumofSector,nNumofRB,nNumofSubcarrierPerRB); % 两跳情况,RN在子载波的接收功率
% dRN2UE_PowerReceived = zeros(nNumofCell,nNumofRelayPerCell,nTotalNumofUser,nNumofRB,nNumofSubcarrierPerRB); % 两跳情况,UE在子载波的接收功率
dRN2UE_PowerReceived = zeros(nNumofSector,nNumofRelayPerSector,nNumofRB,nNumofSubcarrierPerRB);
dBS2UElink_TransmitBitinRB = zeros(1,nTotalNumofUser); % 每个直连用户被发送的bit数
dBS2RNlink_TransmitBitinRB = zeros(nNumofSector,nNumofRelayPerSector); % 每个中继在第一跳被发送的bit数
dRN2UElink_TransmitBitinRB = zeros(nNumofRelayPerSector,nTotalNumofUser); % 每个两跳用户在一个中继上第二跳被发送的bit数
% dRN2UElink_TransmitBitinRB = zeros(nNumofRelayPerCell,nTotalNumofUser);
dReal_TransmitBitinRB = zeros(1,nTotalNumofUser); % 每用户(直连与中继)的发送bit数
buffer = zeros(1,nTotalNumofUser);
T = 100;
Channel_Capacity = zeros(1,nTotalNumofUser);
TempSecondHopTransmitBit = zeros(nNumofSector,nNumofRelayPerSector); % 每个中继第二跳的发送bit数
%%%%%%%%%%%%%%%%%%**** 定义真实SINR ****%%%%%%%%%%%%%%%%%%%
dDL_RealSINR_TS1 = struct('BS2UElink',repmat({zeros(nTotalNumofUser,nNumofRB,nNumofSubcarrierPerRB)},nNumofSector,1),...
'RN2UElink',repmat({zeros(nNumofRelayPerSector,nTotalNumofUser,nNumofRB,nNumofSubcarrierPerRB)},nNumofSector,1));
%zcq把结构体改了一下
dDL_RealSINR_TS2 = struct('BS2RNlink',repmat({zeros(nNumofRelayPerSector*nNumofSector,nNumofRB,nNumofSubcarrierPerRB)},nNumofSector,1),...
'BS2UElink',repmat({zeros(nTotalNumofUser,nNumofRB,nNumofSubcarrierPerRB)},nNumofSector,1));
%%%%%%%%%%%%%%%%%%%%%%%%定义量化实际SINR所需要的结构体变量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%定义量化实际SINR所需要的结构体变量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%实际SINR量化后所对应的MCS方式
dDL_RealMCS_TS1 = struct('BS2UElink',repmat({zeros(nTotalNumofUser,nNumofRB )},nNumofSector,1),...
'RN2UElink',repmat({zeros(nNumofRelayPerSector,nTotalNumofUser,nNumofRB)},nNumofSector,1));
dDL_RealMCS_TS2 = struct('BS2RNlink',repmat({zeros(nNumofRelayPerSector*nNumofSector,nNumofRB)},nNumofSector,1),...
'BS2UElink',repmat({zeros(nTotalNumofUser,nNumofRB )},nNumofSector,1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%^^^^ 计算各段链路上的传输比特数 ^^^^%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for u = 1:nTotalNumofUser
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%^^^^ 用户为直连 ^^^^%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nUEScheduler(u).TransModeFlag == 0 % 如果用户为直连情况
RBlabel = find(nBSScheduler(ServingSector(u)).dChannelAllocation_DirectLink == u); % 直连用户在第一跳分到的资源块
RBlabel2 = find(nBSScheduler(ServingSector(u)).dChannelAllocation_DirectLink_insecondhop == u); % 直连用户在第二跳分到的资源块
%%%%%%%%%%%%%%%%%%%%%%%**** 子时隙1的直连情况 ****%%%%%%%%%%%%%%%%%%%%%%%
if isempty(RBlabel) == 0 %BS-UE in 1st hop
% dBS2UE_PowerReceived = zeros(nNumofSector,nNumofRB,nNumofSubcarrierPerRB); % 初始化直连情况,UE在子载波的接收功率
dBS2UE_Interference = zeros(1,nNumofRB,nNumofSubcarrierPerRB); %初始化BS-UE干扰(子载波级)
dRN2UE_Interference = zeros(1,nNumofRB,nNumofSubcarrierPerRB);
for m = 1:length(RBlabel)
for s = 1:nNumofSector
if s == ServingSector(u) % 服务扇区
% BS-UE有用信号
dBS2UE_PowerReceived(ServingSector(u),RBlabel(m),:) = nBSScheduler(ServingSector(u)).dwChannelPowerAllcation(RBlabel(m))./nSCRNumPerRBinDefination ...
.* dBS2UE_LargeScale.TotalPathLoss(ServingSector(u),u).*dFastFading(s).BS2UElink(u,RBlabel(m),:);
else % 非服务扇区 BS->UE
if any(nBSScheduler(s).dChannelAllocation_DirectLink(RBlabel(m))) % 判断此RB在非服务扇区是否被BS使用
% BS-UE干扰信号
dBS2UE_PowerReceived(s,RBlabel(m),:) = nBSScheduler(s).dwChannelPowerAllcation(RBlabel(m))./nSCRNumPerRBinDefination ...
.* dBS2UE_LargeScale.TotalPathLoss(s,u).*dFastFading(s).BS2UElink(u,RBlabel(m),:);%其他扇区的基站在同频资源上的发射信号,对用户u的干扰
dBS2UE_Interference(1,RBlabel(m),:) = dBS2UE_Interference(1,RBlabel(m),:) + dBS2UE_PowerReceived(s,RBlabel(m),:); % 通过s循环累加,求所有非服务扇区的BS在此RB的各个子载波上,对用户u的总干扰
end
for r=1:nNumofRelayPerSector
if any( nRNScheduler_TS1(s).dChannelAllocation(r,RBlabel(m)))
dRN2UE_PowerReceived(s,r,RBlabel(m),:)=nRNScheduler_TS1(s).dwChannelPowerAllcation(r,RBlabel(m))./nSCRNumPerRBinDefination...
.* dRN2UE_LargeScale.TotalPathLoss(s,r,u).*dFastFading(s).RN2UElink(r,u,RBlabel(m),:);
dRN2UE_Interference(1,RBlabel(m),:) = dRN2UE_Interference(1,RBlabel(m),:)+reshape(dRN2UE_PowerReceived(s,r,RBlabel(m),:),1,1,nNumofSubcarrierPerRB);
end
end
end % if s == ServingSector(u)
end % for s = 1:nNumofSector
dDL_RealSINR_TS1(ServingSector(u)).BS2UElink(u,RBlabel(m),:) = dBS2UE_PowerReceived(ServingSector(u),RBlabel(m),:) ./...
(dBS2UE_Interference(1,RBlabel(m),:) + dRN2UE_Interference(1,RBlabel(m),:)+dwPerSubcarrierWhiteNoise*10^(ddBUENoisefigure*0.1));
%%%%%%%%%%%%%%%%%%对实际SINR进行量化%%%%%%%%%%%%%%%%%%%%%%%
M = 15;
%如果某链路还未找到最佳MCS,且15种MCS未试验完则继续试验
while(M>=1 & (dDL_RealMCS_TS1(Serv