1. 程式人生 > >MATLAB程式設計 2psk系統調製與解調

MATLAB程式設計 2psk系統調製與解調

定義一個主函式,自定義一個一位陣列,由零一組成,作為輸入訊號,先進行2psk調製,再進行2psk相干解調,然後經過低通濾波器,再經過抽樣判決得出原波形。
2psk訊號只能使用相干解調,包絡檢波無法區分同相相位。

主函式

i=10;%基帶訊號碼元數
j=5000;
t=linspace(0,5,j);%0-5之間產生5000個點行向量,即將[0,5]分成5000份
fc=5;%載波頻率
fm=i/5;%碼元速率
B=2*fm;%訊號頻寬
 
%產生基帶訊號
a=input('請輸入二進位制基帶訊號');
%a=inputdlg('請輸入十位二級制基帶訊號','輸入訊號');

%a=[1,1,1,0,0,0,1,0,1,0];%隨機序列,基帶訊號
%figure(3);stem(a);
st1=t;
for n=1:10
    if a(n)<1
        for m=j/i*(n-1)+1:j/i*n
            st1(m)=0;
        end
    else
        for m=j/i*(n-1)+1:j/i*n
            st1(m)=1;
        end
    end
end
figure(1);
subplot(411);
plot(t,st1);
title('基帶訊號st1');
axis([0,5,-1,2]);
 
%基帶訊號求反
%由於PSK中的是雙極性訊號,因此對上面所求單極性訊號取反來與之一起構成雙極性碼
st2=t;
for k=1:j
    if st1(k)>=1
        st2(k)=0;
    else
        st2(k)=1;
    end
end
subplot(412);
plot(t,st2);
title('基帶訊號反碼st2');
axis([0,5,-1,2]);
 %基帶訊號變成雙極性
st3=st1-st2;
subplot(413);
plot(t,st3);
title('雙極性基帶訊號st3');
axis([0,5,-2,2]);
 
%載波訊號
s1=sin(2*pi*fc*t);
subplot(414);
plot(s1);
title('載波訊號s1');
 
%調製
e_psk=st3.*s1;
figure(2);
subplot(511);
plot(t,e_psk);
title('調製後波形e-2psk');
 
%加噪
 noise=rand(1,j);
 psk=e_psk+noise;%加入噪聲
subplot(512);
plot(t,psk);
title('加噪後波形');

%相干解調
psk=e_psk.*s1;%與載波相乘
subplot(513);
plot(t,psk);
title('與載波s1相乘後波形');
 [f,af] = T2F(t,psk);%傅立葉變換
 %subplot(614);
%plot(t,psk);
%title('傅立葉變換後波形');
[t,psk] = lpf(f,af,B);%通過低通濾波器
subplot(514);
plot(t,psk);
title('低通濾波後波形');
 
%抽樣判決
for m=0:i-1
    if psk(1,m*500+250)<0
        for j=m*500+1:(m+1)*500
            psk(1,j)=0;
        end
    else
        for j=m*500+1:(m+1)*500
            psk(1,j)=1;
        end
    end
end
subplot(515);
plot(t,psk);
axis([0,5,-1,2]);
title('抽樣判決後波形');

傅立葉變換函式

function [f,sf]= T2F(t,st)
%利用FFT計算訊號的頻譜並與訊號的真實頻譜的抽樣比較。
%指令碼檔案T2F.m定義了函式T2F,計算訊號的傅立葉變換。
%Input is the time and the signal vectors,the length of time must greater
%than 2
%Output is the frequency and the signal spectrum
dt = t(2)-t(1);
T=t(end);
df = 1/T;
N = length(st);
f=-N/2*df : df : N/2*df-df;
sf = fft(st);
sf = T/N*fftshift(sf);

低通濾波器

function [t,st]=lpf(f,sf,B)
%This function filter an input data using a lowpass filter
%Inputs: f: frequency samples
% sf: input data spectrum samples
% B: lowpass bandwidth with a rectangle lowpass
%Outputs: t: time samples
% st: output data time samples
df = f(2)-f(1);
T = 1/df;
hf = zeros(1,length(f));%全零矩陣
bf = [-floor( B/df ): floor( B/df )] + floor( length(f)/2 );
hf(bf)=1;
yf=hf.*sf;
[t,st]=F2T(f,yf);
st = real(st);

傅立葉反變換函式

function [t,st]=F2T(f,sf)
%指令碼檔案F2T.m定義了函式F2T,計算訊號的反傅立葉變換。
%This function calculate the time signal using ifft function for the input
df = f(2)-f(1);
Fmx = ( f(end)-f(1) +df);
dt = 1/Fmx;
N = length(sf);
T = dt*N;
%t=-T/2:dt:T/2-dt;
t = 0:dt:T-dt;
sff = fftshift(sf);
st = Fmx*ifft(sff);

傅立葉變換並不參與到解調與調製中,只是低通濾波器,需要頻率引數,才進行傅立葉變換。