正弦波的源程序:(一),用到的函数1,f2t函数function x=f2t(X)global dt df t f T N%x=f2t(X)%x为时域的取样值矢量%X为x的傅氏变换%X与x长度相同并为2的整幂%本函数需要一个全局变量dt(时域取样间隔) X=[X(N/2+1:N),X(1:N/2)];x=ifft(X)/dt;end2,t2f函数。
function X=t2f(x)global dt df N t f T%X=t2f(x)%x为时域的取样值矢量%X为x的傅氏变换%X与x长度相同,并为2的整幂。
%本函数需要一个全局变量dt(时域取样间隔) H=fft(x);X=[H(N/2+1:N),H(1:N/2)]*dt;end(二),主程序。
1,%(1)绘出正弦信号波形及频谱global dt df t f Nclose allk=input('取样点数=2^k, k取10左右');if isempty(k), k=10; endf0=input('f0=取1(kz)左右');if isempty(f0), f0=1; endN=2^k;dt=0.01; %msdf=1/(N*dt); %KHzT=N*dt; %截短时间Bs=N*df/2; %系统带宽f=[-Bs+df/2:df:Bs]; %频域横坐标t=[-T/2+dt/2:dt:T/2]; %时域横坐标s=sin(2*pi*f0*t); %输入的正弦信号S=t2f(s); %S是s的傅氏变换a=f2t(S); %a是S的傅氏反变换a=real(a);as=abs(S);subplot(2,1,1) %输出的频谱plot(f,as,'b');gridaxis([-2*f0,+2*f0,min(as),max(as)]) xlabel('f (KHz)')ylabel('|S(f)| (V/KHz)') %figure(2) subplot(2,1,2)plot(t,a,'black') %输出信号波形画图gridaxis([-2/f0,+2/f0,-1.5,1.5])xlabel('t(ms)')ylabel('a(t)(V)')gtext('频谱图')最佳基带系统的源程序:(一),用到的函数f2t函数和t2f函数。
代码>>(二),主程序globaldt t f df N Tclose allclear Eb_N0 Pek=input('取样点数=2^k, k取13左右'); if isempty(k), k=13; endz=input('每个信号取样点数=2^z, z<k'); if isempty(z), z=3; endaa=input('滚降系数=[0.5]');if aa==[],aa=0.5;endN=2^k;L=2^z;M=N/L;Rb=2; %码速率是2Mb/sTs=1/Rb; %码元间隔dt=Ts/L; %时域采样间隔df=1/(N*dt); %频域采样间隔T=N*dt; %截短时间Bs=N*df/2; %系统带宽f=[-Bs+df/2:df:Bs]; %频域横坐标t=[-T/2+dt/2:dt:T/2]; %时域横坐标g=sin(pi*t/Ts).*cos(pi*t*aa/Ts)./[pi*t/Ts.*(1-4*t.^2*aa^2/Ts^2)]; %升余弦脉冲波形GG=t2f(g);GG=abs(GG); %升余弦脉冲的傅式变换GT=sqrt(GG);GR=GT; %最佳系统的发送接收滤波器的傅式变换for l1=1:20;Eb_N0(l1)=(l1-1); %Eb/N0 in dBeb_n0(l1)=10^(Eb_N0(l1)/10);Eb=1;n0=Eb/eb_n0(l1); %信道的噪声谱密度sita=n0*Bs; %信道中噪声功率n_err=0; %误码计数for l2=1:20;b=sign(randn(1,M));s=zeros(1,N); %产生冲激序列s(L/2:L:N)=b/dt;SS=t2f(s);S=SS.*GG; %信道的傅式变化a=f2t(S);a=real(a); %不加噪声的输出n_ch=sqrt(sita)*randn(size(t)); %信道噪声N_CH=t2f(n_ch);nr=real(f2t(N_CH.*GR)); %输出噪声sr=a+nr; %接收信号y=sr(L/2-L/4:L:N-L/4); %取样bb=sign(y); %判决n_err=n_err+length(find(bb~=b));%错误累计endPe(l1)=n_err/(M*l2);semilogy(Eb_N0,Pe,'g'); %Pe~Eb/N0曲线画图xlabel('Eb/N0');ylabel('Pe');title('Pe~Eb/N0曲线');eb_n0=10.^(Eb_N0/10);hold onsemilogy(Eb_N0,0.5*erfc(sqrt(eb_n0)));axis([0,15,1e-4,1])xlabel('Eb/N0')ylabel('Pe')legend('实际的','理论的')end多径传输的源程序:(一),用到的函数f2t函数和t2f函数。
代码>>(二),主程序global dt t f df N Tclose allclear Eb_N0 Pek=input('取样点数=2^k, k取13左右');if isempty(k), k=13; endz=input('每个信号取样点数=2^z, z<k');if isempty(z), z=3; endaa=input('滚降系数=[0.5]');if aa==[],aa=0.5;endN=2^k;L=2^z;M=N/L;Rb=2; %码速率是2Mb/sTs=1/Rb; %码元间隔dt=Ts/L; %时域采样间隔df=1/(N*dt); %频域采样间隔T=N*dt; %截短时间Bs=N*df/2; %系统带宽f=[-Bs+df/2:df:Bs]; %频域横坐标t=[-T/2+dt/2:dt:T/2]; %时域横坐标g=sin(pi*t/Ts).*cos(pi*t*aa/Ts)./[pi*t/Ts.*(1-4*t.^2*aa^2/Ts^2)]; %升余弦脉冲波形GG=t2f(g);GG=abs(GG); %升余弦脉冲的傅式变换GT=sqrt(GG);GR=GT; %最佳系统的发送接收滤波器的傅式变换for l1=1:20;Eb_N0(l1)=(l1-1); %Eb/N0 in dBeb_n0(l1)=10^(Eb_N0(l1)/10);Eb=1;n0=Eb/eb_n0(l1); %信道的噪声谱密度sita=n0*Bs; %信道中噪声功率n_err=0; %误码计数for l2=1:5;dm=Ts/2;C=abs(1-0.5*exp(-j*2*pi*f*dm));%多径信道b=sign(randn(1,M));s=zeros(1,N); %产生冲激序列s(L/2:L:N)=b/dt;SS=t2f(s);S=SS.*GG.*C; %信道的傅式变化a=f2t(S);a=real(a); %不加噪声的输出n_ch=sqrt(sita)*randn(size(t)); %信道噪声N_CH=t2f(n_ch);nr=real(f2t(N_CH.*GR)); %输出噪声sr=a+nr; %接收信号y=sr(L/2:L:N); %取样bb=sign(y); %判决n_err=n_err+length(find(bb~=b));%错误累计endPe(l1)=n_err/(M*l2);semilogy(Eb_N0,Pe,'black'); %Pe~Eb/N0曲线画图xlabel('Eb/N0');ylabel('Pe');title('Pe~Eb/N0曲线'); eb_n0=10.^(Eb_N0/10);hold onsemilogy(Eb_N0,0.5*erfc(sqrt(eb_n0)));axis([0,15,1e-3,1])xlabel('Eb/N0')ylabel('Pe')legend('实际的','理论的')end高斯噪声的源程序:function[gsrv1,gsrv2]=gngauss(m,sgma)if nargin==0,m=0;sgma=1;elseif nargin==1,sgma=m;m=0;end;u=rand;z=sgma*(sqrt(2*log(1/(1-u))));u=rand;gsrv1=m+z*cos(2*pi*u);gsrv2=m+z*sin(2*pi*u);Q函数互补的源程序:function[y]=Qfunct(x)y=(1/2)*erfc(x/sqrt(2));单极性归零码的源程序:(一),用到的函数f2t函数和t2f函数。
代码>>(二),主程序global dt t f df N Tclose allk=input('取样点数=2^k, k取10左右'); if isempty(k), k=13; endz=input('每个信号取样点数=2^z,z<k'); if isempty(z), z=5; endf0=input('f0=取1(kz)左右');if isempty(f0), f0=1; endN=2^kL=2^z;M=N/L;Rb=2;Ts=0.5 %码元宽度是0.5usdt=Ts/L; %时域采样间隔df=1/(N*dt) %MHzT=N*dt %截短时间Bs=N*df/2 %系统带宽f=[-Bs+df/2:df:Bs]; %频域横坐标t=[-T/2+dt/2:dt:T/2]; %时域横坐标EP=zeros(size(f))+eps;for ii1=1:30b=round(rand(1,M)); %产生随机码s=zeros(1,N);for ii=1:L/2;s(ii+[0:M-1]*L)=b;endS=t2f(s); %S是s的傅氏变换a=f2t(S); %a是S的傅氏反变换a=abs(a);P=S.*conj(S)/T; %功率谱EP=(EP*(ii1-1)+P+eps)/ii1;figure(1) %输出的功率画图plot(f,10*log10(EP),'b');gridaxis([-15,15,-100,20]);xlabel('f (KHz)');ylabel('P (V/KHz)') ;title('功率谱图') figure(2) %输出信号波形画图plot(t,a,'b')gridaxis([-3,3,0,1.3]);xlabel('t (ms)');ylabel('s(t) (V)');title('单极性RZ的时域图') end8psk的源程序:function[pb,ps]=cm_sm32(snr_in_dB)N=10000;E=1;snr=10^(snr_in_dB/10);sgma=sqrt(E/(3*2*snr));s000=[1 0];s001=[cos(pi/4) sin(pi/4)];s011=[0 1];s010=[cos(3*pi/4) sin(3*pi/4)];s110=[-1 0];s111=[cos(5*pi/4) sin(5*pi/4)];s101=[0 -1];s100=[cos(7*pi/4) sin(7*pi/4)];for i=1:N,temp=rand;if(temp<0.125),dsource1(i)=0;dsource2(i)=0;dsource3(i)=0;elseif(temp<0.25),dsource1(i)=0;dsource2(i)=0;dsource3(i)=1;elseif(temp<0.375),dsource1(i)=0;dsource2(i)=1;dsource3(i)=0;elseif(temp<0.5),dsource1(i)=0;dsource2(i)=1;dsource3(i)=1;elseif(temp<0.625),dsource1(i)=1;dsource2(i)=0;dsource3(i)=0;elseif(temp<0.75),dsource1(i)=1;dsource2(i)=0;dsource3(i)=1;elseif(temp<0.875),dsource1(i)=1;dsource2(i)=1;dsource3(i)=0;elsedsource1(i)=1;dsource2(i)=1;dsource3(i)=1;end;end;numofsymbolerror=0;numofbiterror=0;for i=1:N,n(1)=gngauss(sgma);n(2)=gngauss(sgma);if((dsource1(i)==0)&(dsource2(i)==0)&(dsource3(i)==0)),r=s000+n;elseif((dsource1(i)==0)&(dsource2(i)==0)&(dsource3(i)==1)), r=s001+n;elseif((dsource1(i)==0)&(dsource2(i)==1)&(dsource3(i)==0)), r=s010+n;elseif((dsource1(i)==0)&(dsource2(i)==1)&(dsource3(i)==1)), r=s011+n;elseif((dsource1(i)==1)&(dsource2(i)==0)&(dsource3(i)==0)), r=s100+n;elseif((dsource1(i)==1)&(dsource2(i)==0)&(dsource3(i)==1)),r=s101+n;elseif((dsource1(i)==1)&(dsource2(i)==1)&(dsource3(i)==0)), r=s110+n;elser=s111+n;end;c000=dot(r,s000);c001=dot(r,s001);c010=dot(r,s010);c011=dot(r,s011);c100=dot(r,s100);c101=dot(r,s101);c110=dot(r,s110);c111=dot(r,s111);c_max=max([c000 c001 c010 c011 c100 c101 c110 c111]);if(c000==c_max),decis1=0;decis2=0;decis3=0;elseif(c001==c_max),decis1=0;decis2=0;decis3=1;elseif(c010==c_max),decis1=0;decis2=1;decis3=0;elseif(c011==c_max),decis1=0;decis2=1;decis3=1;elseif(c100==c_max),decis1=1;decis2=0;decis3=0;elseif(c101==c_max),decis1=1;decis2=0;decis3=1;elseif(c110==c_max),decis1=1;decis2=1;decis3=0;elsedecis1=1;decis2=1;decis3=1;end;symbolerror=0;if(decis1~=dsource1(i)),numofbiterror=numofbiterror+1;symbolerror=1;end;if(decis2~=dsource2(i)),numofbiterror=numofbiterror+1;symbolerror=1;end;if(decis3~=dsource3(i)),numofbiterror=numofbiterror+1;symbolerror=1;end;if(symbolerror==1),numofsymbolerror=numofsymbolerror+1;end;end;ps=numofsymbolerror/N;pb=numofbiterror/(3*N);4PSK的源程序:% 对M=4的PSK通信系统进行蒙特卡罗仿真%echo onSNRindB1=0:2:10;SNRindB2=0:0.1:10;for i=1:length (SNRindB1),[pb , ps]=cm_sm32 (SNRindB1 (i)); %仿真比特和符号误码率smld_bit_err_prb (i)=pb;smld_symbol_err_prb (i)=ps;end;for i=1:length (SNRindB2),SNR=exp(SNRindB2(i)*log(10)/10); %信噪比theo_err_prb(i)=Qfunct(sqrt(2*SNR)); %理论比特误码率end;%随后绘图曲线semilogy (SNRindB1,smld_bit_err_prb,'*');holdsemilogy (SNRindB1,smld_symbol_err_prb,'O');semilogy (SNRindB2,theo_err_prb);gridxlabel('Eb/No in dB')ylabel('error probability')title('4PSK通信系统的蒙特卡洛仿真')gtext('(注:“—”理论误码率;“*”误比特率;“o”误符号率)')。