通信原理课程设计
专业年级 姓名学号_________________
:_________________ :_________________
:
第一章
1.1 信号的能量和功率计算 %信号的能量计算或功率计算 clear all; close all;
dt=0.01; %最小分度值
t=0:dt:5; %函数的时域区间取值 s1=exp(-5*t).*cos(20*pi*t); s2=cos(20*pi*t);
E1=sum(s1.*s1)*dt;%s1(t)的信号能量
P2=sum(s2.*s2)*dt/(length(t)*dt);%s2(t)的信号功率s [f1 s1f]=T2F(t,s1); %对s1进行fft运算 [f2 s2f]=T2F(t,s2); %对s2进行fft运算 df=f1(2)-f1(1);
E1_f=sum(abs(s1f).^2)*df/df;%s1(t)的能量,用频域方式计算 df=f1(2)-f1(1); T=t(end);
P2_f=sum(abs(s2f).^2)*df/T;%s2(t)功率,用频域方式计算 figure(1) %打印第一个图形
subplot(211) %在第一个图形中打印第一个小图 plot(t,s1,'LineWidth',4); %用四号线打印s1的图形
xlabel('t');ylabel('s1(t)');axis([0 2 -1.2 1.2]); %x轴写t,y轴写s1(t), % y轴显示范围为-1.2——1.2,x轴显示范围为0——2
subplot(212) %在第一个图形中打印第二个小图 plot(t,s2,'LineWidth',4); %用四号线打印s2的图形
xlabel('t');ylabel('s2(t)');axis([0 2 -1.2 1.2]); %x轴写t,y轴写s2(t), % y轴显示范围为-1.2——1.2,x轴显示范围为0——2 figure(2) %画第二个图形
plot(f1,abs(s1f)); %在第二个图形中画s1的频谱图
function [f,sf]=T2F(t,st)
%This is a function using the FFT function to calculate a signal's Fourier %Translatation
%Input is the time and the signal vectors,the length of time must greater %than2
%Output is the frequency and the signal spectrum dt=t(2)-t(1); %时域微分 T=t(end);
df=1/T; %频域微分 N=length(st); %st的长度 %f=-N/2*df:df:N/2*df-df;
f=-N/2*df:df:N/2*df-df; %频率的计算
sf=fft(st); %对st进行fft运算
sf=T/N*fftshift(sf); %将fft的DC分量移到频谱中心 %dff=df/8;
%ff=-50*df:dff:50*df; %mf=sinc(ff*T); %sf=cov(mf,sf);
%NN=length(f)+length(ff)-1; %f=-NN/2*dff:dff:NN/2*dff-dff;
x=-+17左右取得最大值 所计算的值:
E1 =0.0554 P2 =0.5010 E1_f =12.5250 P2_f =5010
1.2 周期方波的傅立叶级数展开
close all; clear all; T=1;
N_sample=128; %采样点的个数 dt=T/N_sample; %采样间隔 t=0:dt:T-dt;
y=[ones(1,N_sample/2) -ones(1,N_sample/2)]; %函数的分布,由左半边的1和右半边%的-1构成方波 ft=y;
ft=reshape(ft,1,N_sample); %串并变换
subplot(211); %将图形分为两部分,并画第一部分图形 plot(t,ft); %画函数的图形
axis([0 1 -2 2]); %y轴显示范围为0——1,x轴显示范围为-2——2
df=1/T;
f=-df*(N_sample/2):df:df*(N_sample)/2-df; %频率的范围和分度值 subplot(212);
计算
第四章:信道
4.1 信道失真示意
clear all; close all; Ts=1;
N_sample=8; %每个码元的抽样点数 dt=Ts/N_sample; %抽样间隔 N=1000; %码元数 t=0:dt:(N*N_sample-1)*dt;
gt1=ones(1,N_sample); %NRZ非归零波形 gt2=ones(1,N_sample/2); %RZ归零波形 gt2=[gt2 zeros(1,N_sample/2)];
mt3=sinc((t-5)/Ts); %sinc(pi*t/Ts)波形,截断取10 %个码元 gt3=mt3(1:10*N_sample);
d=(sign(randn(1,N))+1)/2; %产生序列
data=sigexpand(d,N_sample); %对序列间隔插入N_sample-1个0 st1=conv(data,gt1); %data和gt1进行线性 st2=conv(data,gt2); %data和gt2进行线性 d=2*d-1; %变成双极性序列
data=sigexpand(d,N_sample); %对序列间隔插入N_sample-1个0 st3=conv(data,gt3); %data和gt3进行线性 xt=st1; %无失真信道
[f,xf]=T2F(t,xt); %对xt进行fft运算 hf1=exp(-j*pi*f); yf1=xf.*hf1;
[t1,yt1]=F2T(f,yf1); %幅频失真信道
hf2=sinc(f).*exp(-j*pi*f);
yf2=xf.*hf2;
[t2,yt2]=F2T(f,yf2); %相频失真、群时延无失真信道 f1=find(f
hf3=exp(-j*pi*f+j*pi); yf3=xf.*hf3;
[t3,yt3]=F2T(f,yf3); %相频、群时延失真信道 figure(1) subplot(221)
plotyy(f,abs(hf1),f,angle(hf1)); ylabel('幅频、相频特性') title('线性无失真信道') grid on
subplot(222)
plot(t1,real(yt1))
title('经过信道后的输出信号') axis([0,20,-1.2 1.2]) grid on
subplot(223)
plotyy(f,abs(hf2),f,angle(hf2)/pi) ylabel('幅频、相频特性') title('·幅频失真信道') grid on
xlabel('f') subplot(224)
plot(t2,real(yt2)) axis([0,20,-1.2 1.2]) grid on
xlabel('t') figure(2) subplot(211)
plotyy(f,abs(hf3),f,angle(hf3)/pi) ylabel('幅频、相频特性')
title('相频失真、群时延无失真信道') grid on
subplot(212)
plot(t3,real(yt3))
title('经过信道后的输出信号') axis([0,20,-1.2 1.2]) grid on;
%subplot(223)
%plotyy(f,abs(hf4),f,angle(hf4)/pi;ylabel(‘幅频、相频特性’); %title(‘相频失真、群时延失真信道');grid on;xlabel(‘f’); %subplot(224)
%plot(t4,real(yt4));
%axis([0,20,-1.2 1.2]);grid on;xlabel(‘t’) 函数中调用的sigexpand函数
function [out]=sigexpand(d,M) N=length(d) out=zeros(M,N) out(1,:)=d
out=reshape(out,1,M*N) 函数中调用的T2F函数
function [f,sf]=T2F(t,st) 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) 函数中调用的F2T函数
function [t st]=F2T(f,sf) df=f(2)-f(1)
Fmx=(f(end)-f(1)+df) dt=1/Fmx
N=length(sf) T=dt*N
t=0:dt:T-dt
sff=ifftshift(sf) st=Fmx*ifft(sff)
Figure 2
第五章
%ÏÔʾģÄâµ÷ÖƵIJ¨Ðμ°½âµ÷·½·¨AM %ÐÅÔ´
close all; clear all;
dt=0.001;%ʱ¼ä²ÉÑù¼ä¸ô fm=1;%ÐÅÔ´×î¸ßƵÂÊ fc=10;%Ôز¨ÖÐÐÄƵÂÊ T=5;%ÐźÅʱ³¤ t=0:dt:T;
mt=sqrt(2)*cos(2*pi*fm*t);%ÐÅÔ´ %N0=0.01;%°×Ôëµ¥±ß¹¦ÂÊÆÕÃÜ¶È %AM modulation A=2;
s_am=(A+mt).*cos(2*pi*fc*t); B=2*fm;%´øͨÂ˲¨Æ÷´ø¿í
%noise=noise_nb(fc,B,N0,t)£»%Õ-´ø¸ß˹ÔëÉù²úÉú %s_am=s_am+noise; figure(1) subplot(311)
plot(t,s_am);hold on;%»-³öAMÐźŲ¨ÐÎ plot(t,A+mt,'r--');%±êʾAMµÄ°üÂç title('AMµ÷ÖÆÐźż°Æä°üÂç'); xlabel('t');
%AM demodulation
rt=s_am.*cos(2*pi*fc*t);%Ïà¸É½âµ÷ rt=rt-mean(rt); [f,rf]=T2F(t,rt);
[t,rt]=lpf(f,rf,2*fm);%µÍͨÂ˲¨ subplot(312)
plot(t,rt);hold on; plot(t,mt/2,'r--');
title('Ïà¸É½âµ÷ºóµÄÐźŲ¨ÐÎÓëÊäÈëÐźŵıȽÏ'); xlabel('t') subplot(313)
[f,sf]=T2F(t,s_am); psf=(abs(sf).^2)/T; plot(f,psf);
axis([-2*fc 2*fc 0 max(psf)]); title('AMµÄÐźŹ¦ÂÊÆÕ'); xlabel('f');
AM调制信号及其包络
50
-50
0.5
2.533.54t
相干解调后的信号波形与输入信号的比较1
1.5
2
4.5
5
10
-10
0.5
1
1.5
2.53t
AM的信号功率普2
3.5
4
4.5
5
42
0-20
-15
-10
-5
0f
5
10
15
20
function[t st]=lpf(f,sf,B)
%This function filter an input data using a lowpass filter at frequency %domain %Inputs:
% f:frequency samples
% sf:input data spectrum samples
% B:lowpass's bandwidth with a rectangle lowpass %Outputs:
% t:frequency samples
% st:output data's 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[out]=noise_nb(fc,B,N0,t)
%output the narrow band gaussian noise sample with single-sided %powerspectrum N0
%at carrier frequency equals fc and bandwidth equals B
dt=t(2)-t(1); Fmx=1/dt;
n_len=length(t); p=N0*Fmx;
rn=sqrt(p)*randn(1,n_len); [f,rf]=T2F(t,rn);
[t,out}=bpf(f,rf,fc-B/2,fc+B/2);
function [t st]=F2T(f,sf) df=f(2)-f(1);
Fmx=(f(end)-f(1)+df); dt=1/Fmx;
N=length(sf); T=dt*N;
t=0:dt:T-dt;
sff=ifftshift(sf); st=Fmx*ifft(sff);
5.2
%FM modulation and demodulation clear all; close all; Kf=5; fc=10; T=5;
dt=0.001; t=0:dt:T; %信源 fm=1;
%mt=cos(2*pi*fm*t)+1.5*sin(2*pi*0.3*fm*t);%信源信号 mt=cos(2*pi*fm*t);%信源信号 %FM 调制 A=sqrt(2);
%mti=1/2/pi/fm*sin(2*pi*fm*t)-3/4/pi/0.3/fm*cos(2*pi*0.3*fm*t); %mt的积分函数
mti=1/2/pi/fm*sin(2*pi*fm*t);%mt的积分函数 st=A*cos(2*pi*fc*t+2*pi*Kf*mti); figure(1)
subplot(311);
plot(t,st);hold on; plot(t,mt,'r--');
%xlabel('t');ylabel('调频信号')
xlabel('t');ylabel('FM modulated singnal') subplot(312)
[f sf]=T2F(t,st); plot(f,abs(sf)); axis([-25 25 0 3])
%xlabel('f');ylabel('调频信号幅度谱')
xlabel('f');ylabel('Spectrum of the FM signal') %FM 解调
for k=1:length(st)-1
rt(k)=(st(k+1)-st(k))/dt; end
rt(length(st))=0; subplot(313)
plot(t,rt);hold on;
plot(t,A*2*pi*Kf*mt+A*2*pi*fc,'r--'); %xlabel('t');ylabel('调频信号微分后包络')
xlabel('t');ylabel('signal envelope after differentiator')
第六章、
%6.1Êý×Ö»ù´øÐźŹ¦ÂÊÆ×ÃÜ¶È clear all; close all; Ts=1;
N_sample=8;%ÿ¸öÂëÔªµÄ³éÑùµãÊý dt=Ts/N_sample;%³éÑùʱ¼ä¼ä¸ô N=1000;%ÂëÔªÊý
t=0:dt:(N*N_sample-1)*dt; T=N*N_sample*dt;
gt1=ones(1,N_sample);%NRZ·Ç¹éÁ㲨ÐÎ gt2=ones(1,N_sample/2);%NRZ¹éÁ㲨ÐÎ
gt2=[gt2 zeros(1,N_sample/2)];
mt3=sinc((t-5)/Ts);%sin(pi*t/Ts)/pi*t/Ts²¨ÐΣ¬½Ø¶ÎÈ¡10¸öÂëÔª gt3=mt3(1:10*N_sample); d=(sign(randn(1,N))+1)/2;
data=sigexpand(d,N_sample);%¶ÔÐòÁмä¸ô²åÈëN_sample-1¸ö0 st1=conv(data,gt1); st2=conv(data,gt2);
d=2*d-1;%±ä³ÉË«¼«ÐÔÐòÁÐ
data=sigexpand(d,N_sample); st3=conv(data,gt3);
[f,st1f]=T2F(t,[st1(1:length(t))]); [f,st2f]=T2F(t,[st2(1:length(t))]); [f,st3f]=T2F(t,[st3(1:length(t))]); figure(1) subplot(321)
plot(t,[st1(1:length(t))]);grid axis;([0 20 -1.5 1.5]); ylabel('µ¥¼«ÐÔNRZ²¨ÐÎ'); subplot(322)
plot(f,10*log10(abs(st1f).^2/T));grid axis([-5 5 -40 10]);
ylabel('µ¥¼«ÐÔNRZ¹¦ÂÊÆ×Ãܶȣ¨dB/Hz£'); subplot(323)
plot(t,[st2(1:length(t))]); axis;([0 20 -1.5 1.5]);grid ylabel('µ¥¼«ÐÔRZ²¨ÐÎ'); subplot(324)
plot(f,10*log10(abs(st2f).^2/T)); axis([-5 5 -40 10]);grid
ylabel('µ¥¼«ÐÔRZ¹¦ÂÊÆ×Ãܶȣ¨dB/Hz£'); subplot(325)
plot(t-5,[st3(1:length(t))]); axis;([0 20 -2 2]);grid
ylabel('Ë«¼«ÐÔsinc²¨ÐÎ');xlabel('t/Ts'); subplot(326)
plot(f,10*log10(abs(st3f).^2/T)); axis([-5 5 -40 10]);grid
ylabel('sinc²¨Ðι¦ÂÊÆ×Ãܶȣ¨dB/Hz£');xlabel('f*Ts');
补充定义函数(1)
function [out]=sigexpand(d,M) N=length(d); out=zeros(M,N); out(1,:)=d;
out=reshape(out,1,M*N)
(2)
function [f,sf]=T2F(t,st) 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);
%6.2示意双极性NRZ基带信号经过带宽受限信号造成的码间干扰影响及其眼图,文件 clear all; close all; N=1000;
N_sample=8;%每码元抽样点数 Ts=1;
dt=Ts/N_sample;
t=0:dt:(N*N_sample-1)*dt;
gt=ones(1,N_sample);%输入数字序列
d=sign(randn(1,N));
a=sigexpand(d,N_sample); st=conv(a,gt);%数字基带信号 ht1=5*sinc(5*(t-5)/Ts); rt1=conv(st,ht1); ht2=sinc((t-5)/Ts); rt2=conv(st,ht2);
eyediagram(rt1+j*rt2,40,5);%调用Matlab画眼图的函数,行40点,表示5只眼
补充函数
function [out]=sigexpand(d,M) N=length(d); out=zeros(M,N); out(1,:)=d;
out=reshape(out,1,M*N);
第七章
clear all; close all; A=1;
fc=2; %2HZ N_sample=8; N=500;%码元数 Ts=1;%1 baud/s
dt=Ts/fc/N_sample;%波形采集间隔 t=0:dt:N*Ts-dt; T=dt*length(t);
Lt=length(t);%产生二进制信源 d=sign(randn(1,N));
dd=sigexpand((d+1)/2,fc*N_sample); gt=ones(1,fc*N_sample);%NRZ波形 figure(1)
subplot(221);%输入NRZ信号波形(单极性) d_NRZ=conv(dd,gt);
plot(t,d_NRZ(1:length(t)));
axis([0 10 0 1.2]);ylabel('ÊäÈëÐźÅ'); subplot(222);%输入NRZ频谱
[f,d_NRZf]=T2F(t,d_NRZ(1:length(t))); plot(f,10*log10(abs(d_NRZf).^2/T));
axis([-2 2 -50 10]);ylabel('ÊäÈëÐźŹ¦ÂÊÆ×Ãܶȣ¨DB/HZ£');%2ASK ht=A*cos(2*pi*fc*t); s_2ask=d_NRZ(1:Lt).*ht; subplot(223); plot(t,s_2ask);
axis([0 10 -1.2 1.2]); ylabel('2ASK'); [f,s_2askf]=T2F(t,s_2ask); subplot(224);
plot(f,10*log10(abs(s_2askf).^2/T));
axis([-fc-4 fc+4 -50 10]);ylabel('2ASK¹¦ÂÊÆ×Ãܶȣ¨DB/HZ£'); figure(2) %2PSK信号
d_2psk=2*d_NRZ-1;
s_2psk=d_2psk(1:Lt).*ht; subplot(221) plot(t,s_2psk);
axis([0 10 -1.2 1.2]);ylabel('2PSK'); subplot(222)
[f,s_2pskf]=T2F(t,s_2psk);
plot(f,10*log10(abs(s_2pskf).^2/T));
axis([-fc-4 fc+4 -50 10]);ylabel('2PSK¹¦ÂÊÆ×Ãܶȣ¨DB/HZ£'); %2FSK
% s_2fsk=A*cos(2*pi*fc*t+IN(2*2FSK(1:LENGTH(T)).*t); sd_2fsk=2*d_NRZ-1;
s_2fsk=A*cos(2*pi*fc*t+2*pi*sd_2fsk(1:length(t)).*t); subplot(223) plot(t,s_2fsk);
axis([0 10 -1.2 1.2]); xlabel('t');ylabel('2FSK'); subplot(224)
[f,s_2fskf]=T2F(t,s_2fsk);
plot(f,10*log10(abs(s_2fskf).^2/T)); axis([-fc-4 fc+4 -50 10]);xlabel('f');ylabel('2FSK¹¦ÂÊÆ×Ãܶȣ¨DB/HZ£');%随机相位2FSK
fai=2*pi*rand(1,N);
fai_2fsk=sigexpand(fai,fc*N_sample); fai_2fsk=conv(fai_2fsk,gt);
s_2fskd=A*cos(2*pi*fc*t+2*pi*sd_2fsk(1:length(t)).*t+fai_2fsk(1:length(t)));
figure(3)
subplot(221); plot(t,s_2fskd);
axis([0 10 -1.2 1.2]);
[f,s_2fskdf]=T2F(t,s_2fskd); subplot(212);
plot(f,10*log10(abs(s_2fskdf).^2/T)); axis([-fc-4 fc+4 -50 10]);
function [out]=sigexpand(d,M) N=length(d); out=zeros(M,N); out(1,:)=d;
out=reshape(out,1,M*N);
function [f,sf]=T2F(t,st) 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);
2ASK功率谱密度(DB/HZ)输入信号功率谱密度(DB/HZ)
100
-10-20-30-40-50-2100
-10-20-30-40-50
-5
5
-1
1
2
1
输入信号
0.8
0.60.40.200
5
10
10.5
2ASK
0-0.5-10
5
10
2PSK功率谱密度(DB/HZ)
100
-10-20-30-40-50
-5
5
10.5
2PSK
0-0.5-10
5
10
2FSK功率谱密度(DB/HZ)
10.5
100
-10-20-30-40-50
-5
0f
5
2FSK
0-0.5-10
5t
10
10.5
0-0.5-10100
-10-20-30-40-50-6
-4
-2
2
4
6
5
10
第九章
第九章
9.1非均匀量化
%demo for u and A law for quantize, filename:a_u_law.m %u=255 y=ln(1+ux)/ln(1+u)
%A=87.6 y=Ax/(1+lnA)(0
yu=sign(x).*log(1+u*abs(x))/log(1+u); %A Law
for i=1:length(x) if abs(x(i))
ya(i)=A*x(i)/(1+log(A)); else
ya(i)=sign(x(i))*(1+log(A*abs(x(i))))/(1+log(A)); end end
figure(1)
plot(x,yu,'k.:'); title('u Law') xlabel('x'); ylabel('y'); grid on
hold on
xx=[-1,-127/255,-63/255,-31/255,-15/255,-7/255,-3/255,-1/255,1/255,3/255, 7/255,15/255,31/255,63/255,127/255,1];
yy=[-1,-7/8,-6/8,-5/8,-4/8,-3/8,-2/8,-1/8,1/8,2/8,3/8,4/8,5/8,6/8,7/8,1];
plot(xx,yy,'r'); stem(xx,yy,'b-.');
legend('u律压缩特性','折线近似u律'); figure(2)
plot(x,ya,'k.:'); title('A Law') xlabel('x'); ylabel('y'); grid on hold on
xx=[-1,-1/2,-1/4,-1/8,-1/16,-1/32,-1/64,-1/128,1/128,1/64,1/32,1/16,1/8,1/4,1/2,1];
yy=[-1,-7/8,-6/8,-5/8,-4/8,-3/8,-2/8,-1/8,1/8,2/8,3/8,4/8,5/8,6/8,7/8,1];
plot(xx,yy,'r'); stem(xx,yy,'b-.');
通信原理课程设计
专业年级 姓名学号_________________
:_________________ :_________________
:
第一章
1.1 信号的能量和功率计算 %信号的能量计算或功率计算 clear all; close all;
dt=0.01; %最小分度值
t=0:dt:5; %函数的时域区间取值 s1=exp(-5*t).*cos(20*pi*t); s2=cos(20*pi*t);
E1=sum(s1.*s1)*dt;%s1(t)的信号能量
P2=sum(s2.*s2)*dt/(length(t)*dt);%s2(t)的信号功率s [f1 s1f]=T2F(t,s1); %对s1进行fft运算 [f2 s2f]=T2F(t,s2); %对s2进行fft运算 df=f1(2)-f1(1);
E1_f=sum(abs(s1f).^2)*df/df;%s1(t)的能量,用频域方式计算 df=f1(2)-f1(1); T=t(end);
P2_f=sum(abs(s2f).^2)*df/T;%s2(t)功率,用频域方式计算 figure(1) %打印第一个图形
subplot(211) %在第一个图形中打印第一个小图 plot(t,s1,'LineWidth',4); %用四号线打印s1的图形
xlabel('t');ylabel('s1(t)');axis([0 2 -1.2 1.2]); %x轴写t,y轴写s1(t), % y轴显示范围为-1.2——1.2,x轴显示范围为0——2
subplot(212) %在第一个图形中打印第二个小图 plot(t,s2,'LineWidth',4); %用四号线打印s2的图形
xlabel('t');ylabel('s2(t)');axis([0 2 -1.2 1.2]); %x轴写t,y轴写s2(t), % y轴显示范围为-1.2——1.2,x轴显示范围为0——2 figure(2) %画第二个图形
plot(f1,abs(s1f)); %在第二个图形中画s1的频谱图
function [f,sf]=T2F(t,st)
%This is a function using the FFT function to calculate a signal's Fourier %Translatation
%Input is the time and the signal vectors,the length of time must greater %than2
%Output is the frequency and the signal spectrum dt=t(2)-t(1); %时域微分 T=t(end);
df=1/T; %频域微分 N=length(st); %st的长度 %f=-N/2*df:df:N/2*df-df;
f=-N/2*df:df:N/2*df-df; %频率的计算
sf=fft(st); %对st进行fft运算
sf=T/N*fftshift(sf); %将fft的DC分量移到频谱中心 %dff=df/8;
%ff=-50*df:dff:50*df; %mf=sinc(ff*T); %sf=cov(mf,sf);
%NN=length(f)+length(ff)-1; %f=-NN/2*dff:dff:NN/2*dff-dff;
x=-+17左右取得最大值 所计算的值:
E1 =0.0554 P2 =0.5010 E1_f =12.5250 P2_f =5010
1.2 周期方波的傅立叶级数展开
close all; clear all; T=1;
N_sample=128; %采样点的个数 dt=T/N_sample; %采样间隔 t=0:dt:T-dt;
y=[ones(1,N_sample/2) -ones(1,N_sample/2)]; %函数的分布,由左半边的1和右半边%的-1构成方波 ft=y;
ft=reshape(ft,1,N_sample); %串并变换
subplot(211); %将图形分为两部分,并画第一部分图形 plot(t,ft); %画函数的图形
axis([0 1 -2 2]); %y轴显示范围为0——1,x轴显示范围为-2——2
df=1/T;
f=-df*(N_sample/2):df:df*(N_sample)/2-df; %频率的范围和分度值 subplot(212);
计算
第四章:信道
4.1 信道失真示意
clear all; close all; Ts=1;
N_sample=8; %每个码元的抽样点数 dt=Ts/N_sample; %抽样间隔 N=1000; %码元数 t=0:dt:(N*N_sample-1)*dt;
gt1=ones(1,N_sample); %NRZ非归零波形 gt2=ones(1,N_sample/2); %RZ归零波形 gt2=[gt2 zeros(1,N_sample/2)];
mt3=sinc((t-5)/Ts); %sinc(pi*t/Ts)波形,截断取10 %个码元 gt3=mt3(1:10*N_sample);
d=(sign(randn(1,N))+1)/2; %产生序列
data=sigexpand(d,N_sample); %对序列间隔插入N_sample-1个0 st1=conv(data,gt1); %data和gt1进行线性 st2=conv(data,gt2); %data和gt2进行线性 d=2*d-1; %变成双极性序列
data=sigexpand(d,N_sample); %对序列间隔插入N_sample-1个0 st3=conv(data,gt3); %data和gt3进行线性 xt=st1; %无失真信道
[f,xf]=T2F(t,xt); %对xt进行fft运算 hf1=exp(-j*pi*f); yf1=xf.*hf1;
[t1,yt1]=F2T(f,yf1); %幅频失真信道
hf2=sinc(f).*exp(-j*pi*f);
yf2=xf.*hf2;
[t2,yt2]=F2T(f,yf2); %相频失真、群时延无失真信道 f1=find(f
hf3=exp(-j*pi*f+j*pi); yf3=xf.*hf3;
[t3,yt3]=F2T(f,yf3); %相频、群时延失真信道 figure(1) subplot(221)
plotyy(f,abs(hf1),f,angle(hf1)); ylabel('幅频、相频特性') title('线性无失真信道') grid on
subplot(222)
plot(t1,real(yt1))
title('经过信道后的输出信号') axis([0,20,-1.2 1.2]) grid on
subplot(223)
plotyy(f,abs(hf2),f,angle(hf2)/pi) ylabel('幅频、相频特性') title('·幅频失真信道') grid on
xlabel('f') subplot(224)
plot(t2,real(yt2)) axis([0,20,-1.2 1.2]) grid on
xlabel('t') figure(2) subplot(211)
plotyy(f,abs(hf3),f,angle(hf3)/pi) ylabel('幅频、相频特性')
title('相频失真、群时延无失真信道') grid on
subplot(212)
plot(t3,real(yt3))
title('经过信道后的输出信号') axis([0,20,-1.2 1.2]) grid on;
%subplot(223)
%plotyy(f,abs(hf4),f,angle(hf4)/pi;ylabel(‘幅频、相频特性’); %title(‘相频失真、群时延失真信道');grid on;xlabel(‘f’); %subplot(224)
%plot(t4,real(yt4));
%axis([0,20,-1.2 1.2]);grid on;xlabel(‘t’) 函数中调用的sigexpand函数
function [out]=sigexpand(d,M) N=length(d) out=zeros(M,N) out(1,:)=d
out=reshape(out,1,M*N) 函数中调用的T2F函数
function [f,sf]=T2F(t,st) 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) 函数中调用的F2T函数
function [t st]=F2T(f,sf) df=f(2)-f(1)
Fmx=(f(end)-f(1)+df) dt=1/Fmx
N=length(sf) T=dt*N
t=0:dt:T-dt
sff=ifftshift(sf) st=Fmx*ifft(sff)
Figure 2
第五章
%ÏÔʾģÄâµ÷ÖƵIJ¨Ðμ°½âµ÷·½·¨AM %ÐÅÔ´
close all; clear all;
dt=0.001;%ʱ¼ä²ÉÑù¼ä¸ô fm=1;%ÐÅÔ´×î¸ßƵÂÊ fc=10;%Ôز¨ÖÐÐÄƵÂÊ T=5;%ÐźÅʱ³¤ t=0:dt:T;
mt=sqrt(2)*cos(2*pi*fm*t);%ÐÅÔ´ %N0=0.01;%°×Ôëµ¥±ß¹¦ÂÊÆÕÃÜ¶È %AM modulation A=2;
s_am=(A+mt).*cos(2*pi*fc*t); B=2*fm;%´øͨÂ˲¨Æ÷´ø¿í
%noise=noise_nb(fc,B,N0,t)£»%Õ-´ø¸ß˹ÔëÉù²úÉú %s_am=s_am+noise; figure(1) subplot(311)
plot(t,s_am);hold on;%»-³öAMÐźŲ¨ÐÎ plot(t,A+mt,'r--');%±êʾAMµÄ°üÂç title('AMµ÷ÖÆÐźż°Æä°üÂç'); xlabel('t');
%AM demodulation
rt=s_am.*cos(2*pi*fc*t);%Ïà¸É½âµ÷ rt=rt-mean(rt); [f,rf]=T2F(t,rt);
[t,rt]=lpf(f,rf,2*fm);%µÍͨÂ˲¨ subplot(312)
plot(t,rt);hold on; plot(t,mt/2,'r--');
title('Ïà¸É½âµ÷ºóµÄÐźŲ¨ÐÎÓëÊäÈëÐźŵıȽÏ'); xlabel('t') subplot(313)
[f,sf]=T2F(t,s_am); psf=(abs(sf).^2)/T; plot(f,psf);
axis([-2*fc 2*fc 0 max(psf)]); title('AMµÄÐźŹ¦ÂÊÆÕ'); xlabel('f');
AM调制信号及其包络
50
-50
0.5
2.533.54t
相干解调后的信号波形与输入信号的比较1
1.5
2
4.5
5
10
-10
0.5
1
1.5
2.53t
AM的信号功率普2
3.5
4
4.5
5
42
0-20
-15
-10
-5
0f
5
10
15
20
function[t st]=lpf(f,sf,B)
%This function filter an input data using a lowpass filter at frequency %domain %Inputs:
% f:frequency samples
% sf:input data spectrum samples
% B:lowpass's bandwidth with a rectangle lowpass %Outputs:
% t:frequency samples
% st:output data's 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[out]=noise_nb(fc,B,N0,t)
%output the narrow band gaussian noise sample with single-sided %powerspectrum N0
%at carrier frequency equals fc and bandwidth equals B
dt=t(2)-t(1); Fmx=1/dt;
n_len=length(t); p=N0*Fmx;
rn=sqrt(p)*randn(1,n_len); [f,rf]=T2F(t,rn);
[t,out}=bpf(f,rf,fc-B/2,fc+B/2);
function [t st]=F2T(f,sf) df=f(2)-f(1);
Fmx=(f(end)-f(1)+df); dt=1/Fmx;
N=length(sf); T=dt*N;
t=0:dt:T-dt;
sff=ifftshift(sf); st=Fmx*ifft(sff);
5.2
%FM modulation and demodulation clear all; close all; Kf=5; fc=10; T=5;
dt=0.001; t=0:dt:T; %信源 fm=1;
%mt=cos(2*pi*fm*t)+1.5*sin(2*pi*0.3*fm*t);%信源信号 mt=cos(2*pi*fm*t);%信源信号 %FM 调制 A=sqrt(2);
%mti=1/2/pi/fm*sin(2*pi*fm*t)-3/4/pi/0.3/fm*cos(2*pi*0.3*fm*t); %mt的积分函数
mti=1/2/pi/fm*sin(2*pi*fm*t);%mt的积分函数 st=A*cos(2*pi*fc*t+2*pi*Kf*mti); figure(1)
subplot(311);
plot(t,st);hold on; plot(t,mt,'r--');
%xlabel('t');ylabel('调频信号')
xlabel('t');ylabel('FM modulated singnal') subplot(312)
[f sf]=T2F(t,st); plot(f,abs(sf)); axis([-25 25 0 3])
%xlabel('f');ylabel('调频信号幅度谱')
xlabel('f');ylabel('Spectrum of the FM signal') %FM 解调
for k=1:length(st)-1
rt(k)=(st(k+1)-st(k))/dt; end
rt(length(st))=0; subplot(313)
plot(t,rt);hold on;
plot(t,A*2*pi*Kf*mt+A*2*pi*fc,'r--'); %xlabel('t');ylabel('调频信号微分后包络')
xlabel('t');ylabel('signal envelope after differentiator')
第六章、
%6.1Êý×Ö»ù´øÐźŹ¦ÂÊÆ×ÃÜ¶È clear all; close all; Ts=1;
N_sample=8;%ÿ¸öÂëÔªµÄ³éÑùµãÊý dt=Ts/N_sample;%³éÑùʱ¼ä¼ä¸ô N=1000;%ÂëÔªÊý
t=0:dt:(N*N_sample-1)*dt; T=N*N_sample*dt;
gt1=ones(1,N_sample);%NRZ·Ç¹éÁ㲨ÐÎ gt2=ones(1,N_sample/2);%NRZ¹éÁ㲨ÐÎ
gt2=[gt2 zeros(1,N_sample/2)];
mt3=sinc((t-5)/Ts);%sin(pi*t/Ts)/pi*t/Ts²¨ÐΣ¬½Ø¶ÎÈ¡10¸öÂëÔª gt3=mt3(1:10*N_sample); d=(sign(randn(1,N))+1)/2;
data=sigexpand(d,N_sample);%¶ÔÐòÁмä¸ô²åÈëN_sample-1¸ö0 st1=conv(data,gt1); st2=conv(data,gt2);
d=2*d-1;%±ä³ÉË«¼«ÐÔÐòÁÐ
data=sigexpand(d,N_sample); st3=conv(data,gt3);
[f,st1f]=T2F(t,[st1(1:length(t))]); [f,st2f]=T2F(t,[st2(1:length(t))]); [f,st3f]=T2F(t,[st3(1:length(t))]); figure(1) subplot(321)
plot(t,[st1(1:length(t))]);grid axis;([0 20 -1.5 1.5]); ylabel('µ¥¼«ÐÔNRZ²¨ÐÎ'); subplot(322)
plot(f,10*log10(abs(st1f).^2/T));grid axis([-5 5 -40 10]);
ylabel('µ¥¼«ÐÔNRZ¹¦ÂÊÆ×Ãܶȣ¨dB/Hz£'); subplot(323)
plot(t,[st2(1:length(t))]); axis;([0 20 -1.5 1.5]);grid ylabel('µ¥¼«ÐÔRZ²¨ÐÎ'); subplot(324)
plot(f,10*log10(abs(st2f).^2/T)); axis([-5 5 -40 10]);grid
ylabel('µ¥¼«ÐÔRZ¹¦ÂÊÆ×Ãܶȣ¨dB/Hz£'); subplot(325)
plot(t-5,[st3(1:length(t))]); axis;([0 20 -2 2]);grid
ylabel('Ë«¼«ÐÔsinc²¨ÐÎ');xlabel('t/Ts'); subplot(326)
plot(f,10*log10(abs(st3f).^2/T)); axis([-5 5 -40 10]);grid
ylabel('sinc²¨Ðι¦ÂÊÆ×Ãܶȣ¨dB/Hz£');xlabel('f*Ts');
补充定义函数(1)
function [out]=sigexpand(d,M) N=length(d); out=zeros(M,N); out(1,:)=d;
out=reshape(out,1,M*N)
(2)
function [f,sf]=T2F(t,st) 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);
%6.2示意双极性NRZ基带信号经过带宽受限信号造成的码间干扰影响及其眼图,文件 clear all; close all; N=1000;
N_sample=8;%每码元抽样点数 Ts=1;
dt=Ts/N_sample;
t=0:dt:(N*N_sample-1)*dt;
gt=ones(1,N_sample);%输入数字序列
d=sign(randn(1,N));
a=sigexpand(d,N_sample); st=conv(a,gt);%数字基带信号 ht1=5*sinc(5*(t-5)/Ts); rt1=conv(st,ht1); ht2=sinc((t-5)/Ts); rt2=conv(st,ht2);
eyediagram(rt1+j*rt2,40,5);%调用Matlab画眼图的函数,行40点,表示5只眼
补充函数
function [out]=sigexpand(d,M) N=length(d); out=zeros(M,N); out(1,:)=d;
out=reshape(out,1,M*N);
第七章
clear all; close all; A=1;
fc=2; %2HZ N_sample=8; N=500;%码元数 Ts=1;%1 baud/s
dt=Ts/fc/N_sample;%波形采集间隔 t=0:dt:N*Ts-dt; T=dt*length(t);
Lt=length(t);%产生二进制信源 d=sign(randn(1,N));
dd=sigexpand((d+1)/2,fc*N_sample); gt=ones(1,fc*N_sample);%NRZ波形 figure(1)
subplot(221);%输入NRZ信号波形(单极性) d_NRZ=conv(dd,gt);
plot(t,d_NRZ(1:length(t)));
axis([0 10 0 1.2]);ylabel('ÊäÈëÐźÅ'); subplot(222);%输入NRZ频谱
[f,d_NRZf]=T2F(t,d_NRZ(1:length(t))); plot(f,10*log10(abs(d_NRZf).^2/T));
axis([-2 2 -50 10]);ylabel('ÊäÈëÐźŹ¦ÂÊÆ×Ãܶȣ¨DB/HZ£');%2ASK ht=A*cos(2*pi*fc*t); s_2ask=d_NRZ(1:Lt).*ht; subplot(223); plot(t,s_2ask);
axis([0 10 -1.2 1.2]); ylabel('2ASK'); [f,s_2askf]=T2F(t,s_2ask); subplot(224);
plot(f,10*log10(abs(s_2askf).^2/T));
axis([-fc-4 fc+4 -50 10]);ylabel('2ASK¹¦ÂÊÆ×Ãܶȣ¨DB/HZ£'); figure(2) %2PSK信号
d_2psk=2*d_NRZ-1;
s_2psk=d_2psk(1:Lt).*ht; subplot(221) plot(t,s_2psk);
axis([0 10 -1.2 1.2]);ylabel('2PSK'); subplot(222)
[f,s_2pskf]=T2F(t,s_2psk);
plot(f,10*log10(abs(s_2pskf).^2/T));
axis([-fc-4 fc+4 -50 10]);ylabel('2PSK¹¦ÂÊÆ×Ãܶȣ¨DB/HZ£'); %2FSK
% s_2fsk=A*cos(2*pi*fc*t+IN(2*2FSK(1:LENGTH(T)).*t); sd_2fsk=2*d_NRZ-1;
s_2fsk=A*cos(2*pi*fc*t+2*pi*sd_2fsk(1:length(t)).*t); subplot(223) plot(t,s_2fsk);
axis([0 10 -1.2 1.2]); xlabel('t');ylabel('2FSK'); subplot(224)
[f,s_2fskf]=T2F(t,s_2fsk);
plot(f,10*log10(abs(s_2fskf).^2/T)); axis([-fc-4 fc+4 -50 10]);xlabel('f');ylabel('2FSK¹¦ÂÊÆ×Ãܶȣ¨DB/HZ£');%随机相位2FSK
fai=2*pi*rand(1,N);
fai_2fsk=sigexpand(fai,fc*N_sample); fai_2fsk=conv(fai_2fsk,gt);
s_2fskd=A*cos(2*pi*fc*t+2*pi*sd_2fsk(1:length(t)).*t+fai_2fsk(1:length(t)));
figure(3)
subplot(221); plot(t,s_2fskd);
axis([0 10 -1.2 1.2]);
[f,s_2fskdf]=T2F(t,s_2fskd); subplot(212);
plot(f,10*log10(abs(s_2fskdf).^2/T)); axis([-fc-4 fc+4 -50 10]);
function [out]=sigexpand(d,M) N=length(d); out=zeros(M,N); out(1,:)=d;
out=reshape(out,1,M*N);
function [f,sf]=T2F(t,st) 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);
2ASK功率谱密度(DB/HZ)输入信号功率谱密度(DB/HZ)
100
-10-20-30-40-50-2100
-10-20-30-40-50
-5
5
-1
1
2
1
输入信号
0.8
0.60.40.200
5
10
10.5
2ASK
0-0.5-10
5
10
2PSK功率谱密度(DB/HZ)
100
-10-20-30-40-50
-5
5
10.5
2PSK
0-0.5-10
5
10
2FSK功率谱密度(DB/HZ)
10.5
100
-10-20-30-40-50
-5
0f
5
2FSK
0-0.5-10
5t
10
10.5
0-0.5-10100
-10-20-30-40-50-6
-4
-2
2
4
6
5
10
第九章
第九章
9.1非均匀量化
%demo for u and A law for quantize, filename:a_u_law.m %u=255 y=ln(1+ux)/ln(1+u)
%A=87.6 y=Ax/(1+lnA)(0
yu=sign(x).*log(1+u*abs(x))/log(1+u); %A Law
for i=1:length(x) if abs(x(i))
ya(i)=A*x(i)/(1+log(A)); else
ya(i)=sign(x(i))*(1+log(A*abs(x(i))))/(1+log(A)); end end
figure(1)
plot(x,yu,'k.:'); title('u Law') xlabel('x'); ylabel('y'); grid on
hold on
xx=[-1,-127/255,-63/255,-31/255,-15/255,-7/255,-3/255,-1/255,1/255,3/255, 7/255,15/255,31/255,63/255,127/255,1];
yy=[-1,-7/8,-6/8,-5/8,-4/8,-3/8,-2/8,-1/8,1/8,2/8,3/8,4/8,5/8,6/8,7/8,1];
plot(xx,yy,'r'); stem(xx,yy,'b-.');
legend('u律压缩特性','折线近似u律'); figure(2)
plot(x,ya,'k.:'); title('A Law') xlabel('x'); ylabel('y'); grid on hold on
xx=[-1,-1/2,-1/4,-1/8,-1/16,-1/32,-1/64,-1/128,1/128,1/64,1/32,1/16,1/8,1/4,1/2,1];
yy=[-1,-7/8,-6/8,-5/8,-4/8,-3/8,-2/8,-1/8,1/8,2/8,3/8,4/8,5/8,6/8,7/8,1];
plot(xx,yy,'r'); stem(xx,yy,'b-.');