clc
clear
BER=1.3e-3;
rate=60;
numbertotalsub=18;
sigma=rand(1,numbertotalsub);
%%%%初始化%%%%%%%%%
subcarrierallocate=zeros(1,numbertotalsub);
powerallocate=zeros(1,numbertotalsub);
bitallocation=zeros(1,numbertotalsub);
%%%第一次分配,增加一个比特,增加功率最少功率的子载波被选择%%%%%%%%%
%%%同时这个完成该子载波的功率加载,同时用户的速率需求降低1比特到19%%
%%假设使用QAM调制
%delta=+inf;
%numbersub=1;
for n=1:numbertotalsub
bitallocation(n)=bitallocation(n)+1;
powerallocate(n)=1/3*sigma(n)*erfcinv(BER/2)* erfcinv(BER/2)*(2^bitallocation(n)-1);
end
minpower=min(powerallocate);
minindex=find(powerallocate~=minpower);
bitallocation(1,minindex)=0;
powerallocate(1,minindex)=0;
rate=rate-1;
while rate>0
rate;
for n=1:numbertotalsub
powerallocate(n)=1/3*sigma(n)*erfcinv(BER/2)* erfcinv(BER/2)*(2^bitallocation(n)-1);
bitallocation01(n)=bitallocation(n)+1;
powerallocate01(n)=1/3*sigma(n)*erfcinv(BER/2)* erfcinv(BER/2)*(2^bitallocation01(n)-1);
chazhi(n)=powerallocate01(n)-powerallocate(n);
end
minchazhi=min(chazhi);
minindex=find(chazhi==minchazhi);
bitallocation(minindex)=bitallocation(minindex)+1;
powerallocate(minindex)=powerallocate(minindex)+minchazhi;
rate=rate-1;
minchazhi=0;
end
bitallocation