通信原理中2fsk与2psk调制解调系统仿真

%部分代码参考网络

clear all

clc

s=menu('选择方式','2fsk','2psk')

switch s

   case 1,scolor='2fsk'

       n=8

       N=100

       a=randint(1,n)

       bita=[]

      for i=1:length(a)

           if a(i)==0

                bit1=zeros(1,N)

           else

                bit1=ones(1,N)

           end

          bita=[bita,bit1]

         

       end

       figure(1)

       subplot(3,1,1)

       plot(bita)

       title('基带信号')

       grid on

       axis([0,N*length(a),-2.5,2.5])

       

       bitrate=1000%每个码元中采样点的宽度间隔为0.001秒

       fc=1000

       t=linspace(0,1/bitrate,N)

       tz=[]

       c1=sin(2*pi*fc*t)%表示1

       c2=sin(2*pi*2*fc*t)%表示0

       for i=1:length(a)

           if a(i)==1%a中元素为1时

                tz=[tz,c1]

           else

               tz=[tz,c2]

           end

       end

       subplot(3,1,2)

       plot(tz)

       title('2fsk已调信号')

       grid on

       

       %加噪

       signal=awgn(tz,20,'measured')

    

       

       %解调

       %带通滤波

       fs=5000%根据香浓采样定理知道采样频率fs>2fc

       [b1,a1]=ellip(4,0.1,40,[999.9,1000.1]*2/fs)%椭圆滤波器

       [b2,a2]=ellip(4,0.1,40,[1999.9,2000.1]*2/fs)

       sa=filter(b1,a1,signal)%用设计的滤波器进行滤波

       sb=filter(b2,a2,signal)

       figure(2)

       subplot(6,1,1)

       plot(sa)

       title('解调过程')

       grid on

       subplot(6,1,2)

       plot(sb)

       grid on

       

       %相乘器

       ssa=[]

       ssb=[]

       for i=1:n

            ssa=[ssa,c1]

           ssb=[ssb,c2]

       end

          ssaa=sa.*ssa

          ssbb=sb.*ssb

          subplot(6,1,3)

          plot(ssaa)

          subplot(6,1,4)

          plot(ssbb)

          

        %低通滤波

        fs=5000

        [b11,a11]=ellip(4,0.1,30,[40]*2/fs)

        [b22,a22]=ellip(4,0.1,30,[40]*2/fs)

        sfa=filter(b11,a11,ssaa)

        sfb=filter(b22,a22,ssbb)

        subplot(6,1,5)

        plot(sfa)

        subplot(616)

        plot(sfb,'r')

       %抽样判决

       s2a=[]

       s2b=[]

       L=fc/bitrate*N

       i1=L/2

       i2=L

       bitb=[]

       while(i1<=length(sfa))

           s2a=[s2a,sfa(i1)>=0]

           i1=i1+L

       end

       while(i2<=length(sfb))

           s2b=[s2b,sfb(i2)<=0]

           i2=i2+L

       end

       for i=1:n

           if s2a(i)>s2b(i)

                bit1=zeros(1,N)

           else

                bit1=ones(1,N)

           end

           bitb=[bitb,bit1]

       end

       figure(1)

       subplot(3,1,3)

       plot(bitb)

       axis([0,N*length(a),-2.5,2.5])

       grid on

       title('解调后的信号')

   

   

   case 2,scolor='2psk'

       clear all

       clc

       num=10; %码元个数

       tnum=200;%码元长度

       N=num*tnum;%10个码元整体长度

       a=randint(1,num,2); %产生1行num列的矩阵,矩阵内0和1随机出现

       fc=0.5;  %载波频率为0.5

       t=0:0.05:9.99;%t从0到9.99,间隔为0.05

       s=[];c=[];

       for i=1:num  %i从1到10循环

           if(a(i)==0)

           A=zeros(1,tnum); %i=0时,产生一个码元长度为tnum(200)的0码元

           else

            A=ones(1,tnum); %i=1时,产生一个码元长度为tnum(200)的1码元

           end

           s=[s A]; %s为随机基带信号

           cs=sin(2*pi*fc*t);

           c=[c cs];   %c为载波信号

       end

       %采用模拟调制方法得到调制信号

       s_NRZ=[];

       for i=1:num  %i从1到num(10)循环

           if(a(i)==0)

            A=ones(1,tnum); %i=0时,产生一个码元长度为tnum(200)的1码元

           else

            A=-1*ones(1,tnum); %i非0时,产生一个码元长度为tnum(200)的-1码元

           end

           s_NRZ=[s_NRZ,A]; %s_NRZ为双极性非归零码

       end

       e=s_NRZ.*c; %e为BPSK调制信号

       figure(1);

       subplot(411); 

       plot(s);   %作s(基带信号)的波形图

       grid on;

       axis([0 N -2 2]); %横轴长度为0到N,纵轴范围为-2到+2

       xlabel('基带信号s(t)'); 

       ylabel('基带信号幅值'); 

       subplot(412);plot(c);grid on;

       axis([0 N -2 2]);

       xlabel('2PSK载波信号');

       ylabel('2PSK载波信号幅值');  %作c(BPSK载波信号)的波形图

       subplot(413);plot(e);grid on;

       axis([0 N -2 2]);

       xlabel('2PSK调制信号');

       ylabel('2PSK调制信号幅值');  %作e(BPSK调制信号)的波形图

       

        %加高斯噪声

       yi=awgn(e,20,'measured')

       figure(2);

       subplot(2,1,1);

       plot(yi);

       grid on;

       xlabel('加入高斯白噪声的已调信号yi(t)');

       

       %带通滤波器

       [b1,a1]=butter(3,[2*pi*0.0001,2*pi*0.01]); %计算带通滤波器的H(z)系数

       y=filter(b1,a1,yi);  %对信号yi进行滤波,得到信号y

       figure(2);

       subplot(2,1,2);plot(y);grid on;

       xlabel('经带通滤波器后信号');

 

       %与恢复载波相乘

       x1=2*c.*y

       figure(2);

       subplot(2,1,1);

       plot(x1);

       grid on;

       xlabel('与恢复载波相乘后的信号x1(t)');

       

       %低通滤波器

       [b2,a2]=butter(2,0.005);  %计算H(z)系数 ,频率为(1/200)

       x=filter(b2,a2,x1); %对信号x1滤波,得到信号x

       subplot(2,1,2);

       plot(x)

       grid on;

       axis([0 N -2 2]);

       xlabel('经低通滤波器后信号波形')

 

       %抽样判决

       V=length(x);

       if x(200)>0

           x(1:200)=1;

       else

           x(1:200)=0;

       end

       for i=201:199:V

           if x(i)>0;

              

                x(i-199:i)=1

           else

                x(i-199:i)=0;

           end

       end

figure(1);

       subplot(4,1,4)

       plot(x);grid on;

       xlabel('解调信号x(t)');

       axis([0 N -2 2]);

 

end


你可能感兴趣的:(通信原理中2fsk与2psk调制解调系统仿真)