📄 cdmasim.m
字号:
%主程序
%File: cdmasim.m
function [BER,ErrorRun]=cdmasim(N,SF,EbNo,...
NumInterferers,MPathDelay,Kfactor_dB)
rand('state',sum(100*clock));randn('state',sum(100*clock));
NIterate=1e3; %default block size
NumberOfIterations=ceil(N/NIterate);
ErrorState=0;ErrorRun=[];RunCount=1;%itialize
Kfactor=10^(Kfactor_dB/10);%linear units
EbNolinear=10^(EbNo/10);%linear units
MPathComponents=length(MPathDelay);
%
%Determine amplitude in multipath components and store as
%vector. Determine the total power in all the scattered components.
%Determine LOS component.
%
MPathAmp(2:MPathComponents)=rand(MPathComponents-1,1);
ScatPower=MPathAmp*MPathAmp.';
MPathAmp(1)=sqrt(ScatPower*Kfactor);%LOS component
%
%Determine which component has the largest energy (amplitude).
%Normalize vector so that strongest component has unit amplitude.
%
[fee MaxComponent]=max(MPathAmp);MPathAmp=MPathAmp/fee;
%
%Design IIR filter for fading signal.
%
FilterOrder=4;Ripple=0.5;BW=0.01;%filter parameters
[b,a]=cheby1(FilterOrder,Ripple,BW);%4th order filter
%
%Error checking.
%
if NumInterferers>(SF-1)
error(['NumInterferers must not exceed',int2str(SF-1),'.'])
end
if length(MPathDelay)~=length(MPathAmp)
error('MPathDelay and MPathAmp must have the same length.')
end
if min(MPathDelay)<0
error('MPathDelay must not have negative components.')
end
fee=diff(MPathDelay);
if min(fee)<=0
error('MPathDelay must be monotonically increasing.')
end
clear fee
%
%End Error Checking
%
%Generate spreading sequences. The spreading sequences for
%the interferers are shifted versions of the desired sequence
%with a shift offset.
%
DesiredSequence=MSequence(SF+1);%desired signal
offset=fix(length(DesiredSequence)/(NumInterferers+1));
M=length(DesiredSequence);
for k=1:NumInterferers
InterfererSequence(k,:)=[DesiredSequence(M-(k-1)*offset:M)...
DesiredSequence(1:M-1-(k-1)*offset)];
end
%
%The simulation loop begins here.
%
zf=zeros(FilterOrder,MPathComponents);
for cnt=1:NumberOfIterations
%
%Generate and spread symbols for dsired and interfering users.
%
DesiredSymbols=sign(rand(1,NIterate)-0.5);
InterferingSymbols=sign(rand(NumInterferers,NIterate)-0.5);
DesiredChips=reshape(DesiredSequence.'*DesiredSymbols,1,...
M*NIterate);
for k=1:NumInterferers
InterferingChips(k,:)=reshape(InterfererSequence(k,:).'*...
InterferingSymbols(k,:),1,M*NIterate);
end
%
%Generate noise.
%
NoiseAmplitude=sqrt(SF/(2*EbNolinear));
MaxDelay=max(MPathDelay);
DesiredNoise=NoiseAmplitude*randn(1,M*NIterate+MaxDelay);
%
%Apply multipath.
%
MPathLinAmp=MPathAmp;
MPathComponents=length(MPathDelay);
DesiredMPathSignal=zeros(1,M*NIterate+MaxDelay);
if NumInterferers>0,
InterferingMPathSignal=...
zeros(NumInterferers,M*NIterate+MaxDelay);
for k=1:MPathComponents
index=1+MPathDelay(k):NIterate*M+MPathDelay(k);
InterferingMPathSignal(:,index)=...
InterferingMPathSignal(:,index)+...
MPathLinAmp(k)*InterferingChips;
end
end
for k=1:MPathComponents
if k==1,fading=ones(1,M*NIterate);
else
fading=randn(size(DesiredSymbols))+...
j*randn(size(DesiredSymbols));
[fading zf(:,k)]=filter(b,a,fading,zf(:,k));
fading=interp(fading,SF);
fading=abs(fading/sqrt(mean(fading.*conj(fading))));
end
if k==MaxComponent
fadesign=sign(fading);
end
faa(k,:)=MPathLinAmp(k)*fading;
index=1+MPathDelay(k):NIterate*M+MPathDelay(k);
DesiredMPathSignal(index)=...
DesiredMPathSignal(index)+...
(MPathLinAmp(k)*fading).*DesiredChips;
end
%
%Add intererence and noise
%
if NumInterferers>0
IncomingSignal=DesiredMPathSignal+...
sum(InterferingMPathSignal,1)+DesiredNoise;
else
IncomingSignal=DesiredMPathSignal+DesiredNoise;
end
%
%Receive and detect incoming signal.
%
index=1+MPathDelay(MaxComponent):M*NIterate+...
MPathDelay(MaxComponent);
IncomingChips=reshape(fadesign.*...
IncomingSignal(index),M,NIterate);
DespreadSymbols=DesiredSequence*IncomingChips;
DetectedSymbols=sign(DespreadSymbols);
%
%Compute Bit Error Rate
%
ErrorVector=0.5*abs(DetectedSymbols-DesiredSymbols);
ErrorsIterate(cnt)=sum(ErrorVector);
BERIterate(cnt)=ErrorsIterate(cnt)/NIterate;
for k=1:NIterate
if (ErrorVector(k)==0)&(ErrorState==0)
RunCount=RunCount+1;
elseif (ErrorVector(k)==0)&(ErrorState==1)
ErrorRun=[ErrorRun RunCount];
RunCount=1;ErrorState=0;
elseif (ErrorVector(k)==1)&(ErrorState==0)
ErrorRun=[ErrorRun RunCount];
RunCount=1;ErrorState=1;
elseif (ErrorVector(k)==1)&(ErrorState==1)
RunCount=RunCount+1;
else
s1=sprintf('ErrorVector(%d)=%d,ErrorState=%d! Unexpected Condition!');
error(s1);
end
end
end
Errors=sum(ErrorsIterate);BER=mean(BERIterate);
ErrorRun=[ErrorRun RunCount];
%End of funcion file.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -