(完整word版)matlab编程 下载本文

内容发布更新时间 : 2024/11/15 9:45:59星期一 下面是文章的全部内容请认真阅读。

【程序代码】

figure

subplot(3,1,1) plot(x_t(1:20*8))

axis([1,20*8,-1.5,1.5]); title('initial code')

subplot(3,1,2)

plot(y_AMI(1:20*8)) axis([1,20*8,-1.5,1.5]); title('AMI code')

subplot(3,1,3)

plot(y_HDB3(1:20*8)) axis([1,20*8,-1.5,1.5]); title('HDB3 code')

figure

plot_spectrum(x_t,'b'); hold on

plot_spectrum(y_AMI,'r');

第 6 页 共 9 页

plot_spectrum(y_HDB3,'g');

title('Power spectrum of NRZ,AMI and HDB3 Code') legend('NRZ','AMI','HDB3')

%Code.m N=1000; fs=8;

x=ceil(rand(1,N)-0.8); AMI=AMI_Code(x); HDB3=HDB3_Code(x); Miller=Miller_Code(x);

y=zeros(1,N*fs); x_t=zeros(1,N*fs); y_AMI=zeros(1,N*fs); y_HDB3=zeros(1,N*fs); y_Miller=zeros(1,N*fs); for i=1:N*fs

if(mod(i-1,8)<=5 & mod(i-1,8)>=2) y(i)=1; else

y(i)=0; end

%分别给画图序列赋值 y_AMI(i)=y(i)*AMI(ceil(i/8)); y_HDB3(i)=y(i)*HDB3(ceil(i/8)); y_Miller(i)=Miller(ceil(i/4)); x_t(i)=x(ceil(i/8)); end

%AMI_Code.m

function AMI=AMI_Code(x) m=length(x);

%初始化AMI编码 AMI=zeros(1,m); %反转标志初始化 f=0; for i=1:m

%若原码为0,直接将AMI码置0 if x(i)==0

AMI(i)=0;

第 7 页 共 9 页

%否则,使得AMI码交替为+1,-1 else

f=XOR(f,1); AMI(i)=2*f-1; end end return;

%HDB3.m

function HDB3=HDB3_Code(x) m=length(x);

%初始化HDB3编码 HDB3=zeros(1,m);

%初始化信息“1”标志B和连0数标志V B=-1; V=-1; i=1;

while (i<=m-3)

%若原码为1,交替为+1,-1 if(x(i)==1)

HDB3(i)=-1*B; B=HDB3(i); i=i+1; else

%原码只有一个0 if(x(i+1)==1) HDB3(i)=0;

HDB3(i+1)=-1*B; B=HDB3(i+1); i=i+2; else

%原码有两个连续0 if(x(i+2)==1) HDB3(i)=0; HDB3(i+1)=0; HDB3(i+2)=-1*B; B=HDB3(i+2); i=i+3; else

%原码有三个连续0 if(x(i+2)==1) HDB3(i)=0; HDB3(i+1)=0; HDB3(i+2)=0; HDB3(i+3)=-1*B;

第 8 页 共 9 页

B=HDB3(i+3); i=i+4;

%原码出现了四个连续0 else

%原始B、V标志不相同 if(Xor(B,V))

HDB3(i)=01; HDB3(i+1)=0; HDB3(i+2)=0; HDB3(i+3)=B; V=B;i=i+4;

%B、V标志相同 else

HDB3(i)=-1*B; HDB3(i+1)=0; HDB3(i+2)=0; HDB3(i+3)=-1*B; B=-1*B;V=B;i=i+4; end end end end end end for j=i:m

if (x(j)==1)

HDB3(j)=-1*B; B=HDB3(j); else

HDB3(j)=0; end end return;

%plot_spectrum.m

function plot_spectrum(x,str)

P=spectrum(x,8000,0,hanning(300)); plot(10*log10(P(:,1)),str);

return; function plot_spectrum(x,str) P=spectrum(x,8000,0,hanning(300)); plot(10*log10(P(:,1)),str); return;

第 9 页 共 9 页