%本程序模拟2用户CDMA系统,信道为加性高斯信道,可以正确解扩解调,
%信噪比(dB)约为5的时候,误码率基本在0.1以内
%信噪比(dB)大于9的时候,基本不会出现误码了
%本程序中时间单位是微秒
%频率单位为MHz
%码元速率单位是Mb/s
global dt df t f N
close all
N=2^14; %采样点数
L=64; %每码元的采样点数
M=N/L; %码元数
Rb=2;
Ts=0.5; %码元宽度是0.5us
dt=Ts/L;
df=1/(N*dt); %MHz
T=N*dt; %截短时间
Bs=N*df/2; %系统带宽
w0=28*pi;
Eb_N0=input('请输入信噪比dB(5以上基本无误码)');
t=[-T/2+dt/2:dt:T/2]; %时域横坐标
f=[-Bs+df/2:df:Bs]; %频域横坐标
figure(1)
set(1,'Position',[10,600,400,150])
%设定图1的窗口位置及大小
figure(2)
set(2,'Position',[10,400,400,150])
%设定图2的窗口位置及大小
figure(3)
set(3,'Position',[10,200,400,150])
%设定图3的窗口位置及大小
figure(4)
set(4,'Position',[10,0,400,150])
%设定图4的窗口位置及大小
figure(5)
set(5,'Position',[430,600,400,150])
%设定图1的窗口位置及大小
figure(6)
set(6,'Position',[430,400,400,150])
%设定图2的窗口位置及大小
figure(7)
set(7,'Position',[430,200,400,150])
%设定图3的窗口位置及大小
figure(8)
set(8,'Position',[430,0,400,150])
%设定图4的窗口位置及大小
figure(9)
set(9,'Position',[850,600,400,150])
%设定图3的窗口位置及大小
figure(10)
set(10,'Position',[850,400,400,150])
%设定图4的窗口位置及大小
figure(11)
set(11,'Position',[850,200,400,150])
%设定图3的窗口位置及大小
figure(12)
set(12,'Position',[850,0,400,150])
%设定图4的窗口位置及大小
m1=[1 1 1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 -1]; %用户一的m序列
m2=[1 1 -1 -1 1 -1 1 1 1 -1 -1 1 -1 -1 1]; %用户二的m序列
%%%%%%%%%%%%%%%%%%%%%%%%%% 生成噪声,信噪比为11
%Eb_N0=11 %Eb/N0 in dB
eb_n0=10^(Eb_N0/10);
Eb=1;
n0=Eb/eb_n0; %信道的噪声谱密度
sita=n0*Bs; %信道中噪声功率
n_ch=sqrt(sita)*randn(size(t)); %信道噪声
NOISE=abs(t2f(n_ch));
%%%%%%%%%%%%%%%%%%%%%%%%%%%产生用户1的数字基带信号供传输
n=1; %每次有偶数个1
while rem(n,2)~=0
a=round(rand(1,M));
n=length(find(a==1));
end
aaa=a; %保存原序列(0、1),供最后比较误码率使用
aa=find(a==0); %将(0、1)序列变换为(+1、-1)序列,以方便扩频使用
a(aa)=-ones(size(aa));
nrz1=zeros(L,M); %将序列扩展成为时域,方便做F变换和时域显示
for loop=1:L,nrz1(loop,:)=a; end
nrz1=reshape(nrz1,1,N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%产生用户2的数字基带信号供传输
n=1; %每次有偶数个1
while rem(n,2)~=0
b=round(rand(1,M));
n=length(find(b==1));
end
bbb=b;
bb=find(b==0); %bb是a中奇序数1的位置
b(bb)=-ones(size(bb)); %所有位于cc的0变成-1
nrz2=zeros(L,M);
for loop=1:L
nrz2(loop,:)=b;
end
nrz2=reshape(nrz2,1,N);
%%%%%%%%%%%%%%%%%%%%%%生成扩频要用的M序列
mx1=zeros(1,16384);
for i=1:64:16384
mx1(i:i+4)=m1(1);
mx1(i+5:i+8)=m1(2);
mx1(i+9:i+12)=m1(3);
mx1(i+13:i+16)=m1(4);
mx1(i+17:i+20)=m1(5);
mx1(i+21:i+24)=m1(6);
mx1(i+25:i+28)=m1(7);
mx1(i+29:i+32)=m1(8);
mx1(i+33:i+36)=m1(9);
mx1(i+37:i+40)=m1(10);
mx1(i+41:i+44)=m1(11);
mx1(i+45:i+48)=m1(12);
mx1(i+49:i+52)=m1(13);
mx1(i+53:i+56)=m1(14);
mx1(i+57:i+63)=m1(15);
end
mx2=zeros(1,16384);
for i=1:64:16384
mx2(i:i+4)=m2(1);
mx2(i+5:i+8)=m2(2);
mx2(i+9:i+12)=m2(3);
mx2(i+13:i+16)=m2(4);
mx2(i+17:i+20)=m2(5);
mx2(i+21:i+24)=m2(6);
mx2(i+25:i+28)=m2(7);
mx2(i+29:i+32)=m2(8);
mx2(i+33:i+36)=m2(9);
mx2(i+37:i+40)=m2(10);
mx2(i+41:i+44)=m2(11);
mx2(i+45:i+48)=m2(12);
mx2(i+49:i+52)=m2(13);
mx2(i+53:i+56)=m2(14);
mx2(i+57:i+63)=m2(15);
end
%%%%%%%%%%%%%%%%%%%%%%%得到扩频码
user11=nrz1.*mx1;
user22=nrz2.*mx2;
user1=(user11+1)/2;
user2=(user22+1)/2;
%%%%%%%%%%%%%%%%%%%%%% 直接PSK调制产生的信号频谱
psk1=sin(w0*t+nrz1.*pi);
PSK1=t2f(psk1);
aPSK1=abs(PSK1); %PSK调制频谱的幅值
psk2=sin(w0*t+nrz2.*pi);
PSK2=t2f(psk2);
aPSK2=abs(PSK2); %PSK调制频谱的幅值
%%%%%%%%%%%%%%%%%%%%% 基带信号的频谱
NRZ1=t2f(nrz1); %nrz1信号的傅氏变换
aNRZ1=abs(NRZ1);
NRZ2=t2f(nrz2); %nrz2信号的傅氏变换
aNRZ2=abs(NRZ2);
%%%%%%%%%%%%%%%%%%%% 对扩频码进行PSK调制
USER1=t2f(sin(w0*t+user1.*pi)); %nrz信号的傅氏变换
aUSER1=abs(USER1);
USER2=t2f(sin(w0*t+user2.*pi)); %nrz信号的傅氏变换
aUSER2=abs(USER2);
%%%%%%%%%%%%%%%%%%% 每个用户接受到的信号
recieve=f2t(USER1+USER2+NOISE);
%%%%%%%%%%%%%%%%%%% 用户1解调
y1=recieve.*mx1;
y11=y1.*sin(w0*t)*2;
x1=zeros(1,256);
for i=1:256
x1(i)=sum(y11(64*i-63:64*i)); %解扩序列(+1、-1)
end
k1=sign(x1); %解扩序列重生(0,1)
for i=1:256
if k1(i)==1
x1(i)=0;
else
x1(i)=1;
end
end
x11=zeros(L,M);
for loop=1:L,x11(loop,:)=x1; end
x11=reshape(x11,1,N); %解扩信号时域
p1=abs(x1-aaa);
wrong1=sum(p1)/256 %误码率
X1=t2f(x11); %解扩信号的频谱
X1=abs(X1);
%%%%%%%%%%%%%%%%%%% 用户2解调
y2=recieve.*mx2;
y22=y2.*sin(w0*t)*2;
x2=zeros(1,256);
for i=1:256
x2(i)=sum(y22(64*i-63:64*i));
end
k2=sign(x2);
for i=1:256
if k2(i)==1
x2(i)=0;
else
x2(i)=1;
end
end
x22=zeros(L,M);
for loop=1:L,x22(loop,:)=x2; end
x22=reshape(x22,1,N);
p2=abs(x2-bbb);
wrong2=sum(p2)/256
X2=t2f(x22); %解扩信号的频谱
X2=abs(X2);
%%%%%%%%%%%%%%%%%%%%%%%%
figure(1)
plot(t,n_ch,'g')
hold on
plot(t,nrz1,'r')
grid
axis([-2,+2,-1.5,1.5])
title('用户1的基带信号')
xlabel('t (us)')
ylabel('s(t) (V)')
figure(2)
plot(t,n_ch,'g')
hold on
plot(t,user1,'r')
grid
axis([-2,+2,-1.5,1.5])
title('用户1的扩频时域信号')
xlabel('t (us)')
ylabel('psk (V)')
figure(3)
plot(t,n_ch,'g')
hold on
plot(t,nrz2,'r')
grid
axis([-2,+2,-1.5,1.5])
title('用户2的基带信号')
xlabel('t (us)')
ylabel('s(t) (V)')
figure(4)
plot(t,n_ch,'g')
hold on
plot(t,user2,'r')
grid
axis([-2,+2,-1.5,1.5])
title('用户2的扩频时域信号')
xlabel('t (us)')
ylabel('s(t) (V)')
figure(5)
plot(f,NOISE,'g')
hold on
plot(f,aNRZ1,'r')
grid
axis([-10,+10,-1,15])
title('用户1的基带信号的频谱')
xlabel('f (Hz)')
ylabel('NRZ')
figure(6)
plot(f,NOISE,'g')
hold on
plot(f,aUSER1,'r')
grid
axis([-30,+30,-1,15])
title('用户1的扩频信号的频谱')
xlabel('f (Hz)')
ylabel('psk')
figure(7)
plot(f,NOISE,'g')
hold on
plot(f,aNRZ2,'r')
grid
axis([-10,+10,-1,15])
title('用户2的基带信号的频谱')
xlabel('f (Hz)')
ylabel('psk')
figure(8)
plot(f,NOISE,'g')
hold on
plot(f,aUSER2,'r')
grid
axis([-30,+30,-1,15])
title('用户2的扩频信号的频谱')
xlabel('f (Hz)')
ylabel('psk')
figure(9)
plot(t,x11,'r')
grid
axis([-2,+2,-0.25,1.25])
title('解调的用户1的信号')
xlabel('t (us)')
ylabel('s(t) (V)')
figure(10)
plot(f,X1,'r')
grid
axis([-10,+10,-1,15])
title('用户1的解扩以后的信号的频谱')
xlabel('f (Hz)')
figure(11)
plot(t,x22,'r')
grid
axis([-2,+2,-0.25,1.25])
title('解调的用户2的信号')
xlabel('t (us)')
ylabel('s(t) (V)')
figure(12)
plot(f,X2,'r')
grid
axis([-10,+10,-1,15])
title('用户2的解扩以后的信号的频谱')
xlabel('f (Hz)')