通訊原理中2fsk與2psk調製解調系統模擬

Mrslide發表於2018-01-23

%部分程式碼參考網路

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


相關文章