3种基带传输常用编码与解码方法.docx
《3种基带传输常用编码与解码方法.docx》由会员分享,可在线阅读,更多相关《3种基带传输常用编码与解码方法.docx(14页珍藏版)》请在冰豆网上搜索。
3种基带传输常用编码与解码方法
3种基带传输常用编码与解码方法
AMI编码
functiony=AMI(x,samp)
last_one=-1;
fori=1:
length(x)
ifx(i)==1
forj=1:
samp/2
y((2*i-2)*samp/2+j)=-last_one;
y((2*i-1)*samp/2+j)=0;
end
last_one=-last_one;
else
forj=1:
samp
y((i-1)*samp+j)=0;
end
end
end
y=[y,x(i)];
NRZ编码
functionx=NRZ(y,samp)
num=0;
N=length(y)/samp;
fori=1:
N
forj=1:
samp
ify((i-1)*samp+j)==1
num=num+1;
end
end
ifnum>=samp/2
x(i)=1;
else
x(i)=0;
end
num=0;
end
HDB3编码
functiony=HDB3(x,samp)
last_V=-1;
last_one=-1;
num=0;
fori=1:
length(x)
ifx(i)==1
forj=1:
samp/2
y((2*i-2)*samp/2+j)=-last_one;%遇1则极性反转
y((2*i-1)*samp/2+j)=0;
end
last_one=-last_one;
num=0;
else
num=num+1;
ifnum==4
num=0;
temp=-last_V;%遇4连零则置为V,相邻的V极性反转
forj=1:
samp/2
y((2*i-2)*samp/2+j)=temp;
y((2*i-1)*samp/2+j)=0;
end
last_V=temp;
iftemp*last_one==-1%V与前一个非0符号必须同极性
forj=1:
samp/2
y((2*(i-3)-2)*samp/2+j)=temp;%否则置B
y((2*(i-3)-1)*samp/2+j)=0;
end
end
last_one=temp;
end
end
end
y=[y,x(i)];
主程序:
clearall;
samp=300;
%x=randint(1,N);
x=[10110000000110000001];
N=length(x);
t=0:
1/samp:
N;
y1=AMI(x,samp);
y2=NRZ(x,samp);
y3=HDB3(x,samp);
figure
(1);
subplot(3,1,1);
plot(t,y1);
axis([0,N,-0.2,1.2]);
grid;
title('AMI码');
subplot(3,1,2);
plot(t,y2);
axis([0,N,-1.2,1.2]);
grid;
title('单极性非归零码');
subplot(3,1,3);
plot(t,y3);
axis([0,N,-1.2,1.2]);
grid;
title('HDB3码');
%gtext('基带传输常用码的编码');
最后输出结果:
解码程序:
AMI解码:
>>y=[+10-100+100-10+1-100+10-1000+1-1];
>>N=length(y);
>>samp=500;
>>t=0:
1/samp:
N;
>>x=deAMI(y,samp);
>>plot(t,x);
>>axis([0,N,-1.2,1.2]);
>>grid;
>>title('AMI解码');
子函数:
functionx=deAMI(y,samp)
fori=1:
length(y)
ify(i)==1
forj=1:
samp
x((i-1)*samp+j)=1;
end
else
ify(i)==-1
forj=1:
samp
x((i-1)*samp+j)=1;
end
else
forj=1:
samp
x((i-1)*samp+j)=0;
end
end
end
end
x=[x,y(i)];
NRZ解码:
>>y=[1010010010110010100011];
>>N=length(y);
>>samp=500;
>>t=0:
1/samp:
N;
>>x=deNRZ(y,samp);
>>plot(t,x);
>>axis([0,N,-1.2,1.2]);
>>grid;
>>title('NRZ解码');
子函数:
functionx=deNRZ(y,samp)
fori=1:
length(y)
ify(i)==1
forj=1:
samp
x((i-1)*samp+j)=1;
end
else
forj=1:
samp
x((i-1)*samp+j)=0;
end
end
end
x=[x,y(i)];
HDB3解码:
functionx=HDB3(y,samp)
first=y
(1);
fori=1:
3
ify(i)==0
forj=1:
samp
x((i-1)*samp+j)=0;
end
else
first=y(i);
forj=1:
samp
x((i-1)*samp+j)=1;
end
end
end
fori=4:
length(y)
ify(i)==0
forj=1:
samp
x((i-1)*samp+j)=0;
end
else
ify(i)*first==1
forj=1:
samp
x((i-1)*samp+j)=0;
x((i-2)*samp+j)=0;
x((i-3)*samp+j)=0;
x((i-4)*samp+j)=0;
first=y(i);
end
else
first=y(i);
forj=1:
samp
x((i-1)*samp+j)=1;
end
end
end
end
x=[x,y(i)];
主函数:
y=[+10-100+100-10+1-100+10-1000-1+1]
>>N=length(y);
>>samp=500;
>>t=0:
1/samp:
N;
>>x=deHDB3(y,samp);
>>plot(t,x);
>>axis([0,N,-1.2,1.2]);
>>grid;
>>title('HDB3解码');
用窗函数设计FIR数字滤波器
滤波器的设计程序:
b=1;
clearall;
i=0;
while(b);
temp=menu(’选择窗函数长度N‘,’N=10‘,’N=15‘,’N=20‘,’N=25‘,’N=30’,‘N=33‘,’N=35‘,’N=40‘,’N=45‘,’N=50‘,’N=55‘,’N=60‘,’N=64‘);
menu1=[10,15,20,25,30,33,35,40,45,50,55,60,64];
N=menu1(temp);
temp=menu(’选择逼近理想低通滤波器截止频率Wc‘,‘Wc=pi/4’,‘Wc=pi/2’,‘Wc=3*pi/4’,‘Wc=pi’,‘Wc=0.5’,‘Wc=1.0’,‘Wc=1.5’,‘Wc=2.0’,‘Wc=2.5’,‘Wc=3.0’);
menu2=[pi/4,pi/2,3*pi/4,pi,0.5,1,1.5,2,2.5,3];
w=menu2(temp);
n=[0:
(N-1)];
hd=ideal(w,N);%得到理想低通滤波器,
k=menu(’请选择窗口类型:
‘,’boxcar‘,’hamming‘,’hanning‘,’blackman‘);
Ifk==1
B=boxcar(N);
String=[‘Boxcar’,‘N=’,num2str(N)];
elseifk==2
B=hamming(N);
String=[‘hamming’,‘N=’,num2str(N)];
elseifk==3
B=hanning(N);
String=[‘hanning’,‘N=’,num2str(N)];
elseifk==4
B=blackman(N);
String=[‘blackman’,‘N=’,num2str(N)];
end
end
end
end
h=hd.*(B)‘;%得到FIR数字滤波器
[H,m]=freqz(h,[1],1024,’whole‘);%求其频率响应
mag=abs(H);%得到幅值
db=20*log10((mag+eps)/max(mag));
pha=angle(H);%得到相位
i=i+1;
figure(i)
subplot(2,2,1);
n=0:
N-1;
stem(n,h,’。
‘);
axis([0,N-1,-0.1,0.3]);
holdon;
n=0:
N-1;
x=zeros(N);
plot(n,x,‘—’);
xlabel(‘n’);
ylabel(‘h(n)’);
title(‘实际低通滤波器的h(n)’);
text((0.3*N),0.27,string);
holdoff;
subplot(2,2,2);
plot(m/pi,db);
axis([0,1,-100,0]);
xlabel(‘w/pi’);
ylabel(‘db’);
title(‘衰减特性(db)’);
grid;
subplot(2,2,3);
plot(m,pha);
holdon;
n=0:
7;
x=zeros(8);
plot(n,x,‘-’);
title(‘相频特性’);
xlabel(‘频率(rad)’);
ylabel(‘相位(rad)’);
axis([0,3.15,-4,4]);
subplot(2,2,4);
plot(m,mag);
title(‘频率特性’)
xlabel(‘频率W(rad)’);
ylabel(‘幅值’);
axis([0,3.15,0,1.5]);