ifn(r)==0
ifn(r+1)==1
cm=[cm0];
else
cm=[cm1];
end
else
cm=[cm1];
end
r=r+2;
end
3.HDB3码型转换
%%%%%%%%%%%%编码%%%%%%%%%%%%%
yn=xn;%输出yn初始化
num=0;%计数器初始化
fork=1:
length(xn)
ifxn(k)==1
num=num+1;%"1"计数器
ifnum/2==fix(num/2)%奇数个1时输出-1,进行极性交替
yn(k)=1;
else
yn(k)=-1;
end
end
end
%HDB3编码
num=0;%连零计数器初始化
yh=yn;%输出初始化
sign=0;%极性标志初始化为0
V=zeros(1,length(yn));%V脉冲位置记录变量
B=zeros(1,length(yn));%B脉冲位置记录变量
fork=1:
length(yn)
ifyn(k)==0
num=num+1;%连“0”个数计数
ifnum==4%如果4连“0”
num=0;%计数器清零
yh(k)=1*yh(k-4);
%让0000的最后一个0改变为与前一个非零符号相同极性的符号
V(k)=yh(k);%V脉冲位置记录
ifyh(k)==sign%如果当前V符号与前一个V符号的极性相同
yh(k)=-1*yh(k);%则让当前V符号极性反转,以满足V符号间相互极性反转要求
yh(k-3)=yh(k);%添加B符号,与V符号同极性
B(k-3)=yh(k);%B脉冲位置记录
V(k)=yh(k);%V脉冲位置记录
yh(k+1:
length(yn))=-1*yh(k+1:
length(yn));
%并让后面的非零符号从V符号开始再交替变化
end
sign=yh(k);%记录前一个V符号的极性
end
else
num=0;%当前输入为“1”则连“0”计数器清零
end
end
%%%%%%%%%%%%译码%%%%%%%%%%%%%
input=yh;%HDB3码输入
decode=input;%输出初始化
sign=0;%极性标志初始化
fork=1:
length(yh)
ifinput(k)~=0
ifsign==yh(k)%如果当前码与前一个非零码的极性相同
decode(k-3:
k)=[0000];%则该码判为V码并将*00V清零
end
sign=input(k);%极性标志
end
end
decode=abs(decode);%整流
实验现象:
总体界面:
AMI:
CMI:
HDB3:
总结:
1.本次实验中因为对画方波不太熟悉,在使用stairs函数时,方波图总是在最后少了一位,后来经查询后才知道stairs的正确用法。
2.由于对GUI界面设置的不熟悉,很多细节都没做到位,界面的美观有待提高。
3.在编代码是经常没有写注释,导致后来有些变量混淆了。
附录:
全部代码
functionvarargout=untitled1(varargin)
%UNTITLED1M-filefor
%UNTITLED1,byitself,createsanewUNTITLED1orraisestheexisting
%singleton*.
%
%H=UNTITLED1returnsthehandletoanewUNTITLED1orthehandleto
%theexistingsingleton*.
%
%UNTITLED1('CALLBACK',hObject,eventData,handles,...)callsthelocal
%functionnamedCALLBACKinwiththegiveninputarguments.
%
%UNTITLED1('Property','Value',...)createsanewUNTITLED1orraisesthe
%existingsingleton*.Startingfromtheleft,propertyvaluepairsare
%appliedtotheGUIbeforeuntitled1_OpeningFunctiongetscalled.An
%unrecognizedpropertynameorinvalidvaluemakespropertyapplication
%stop.Allinputsarepassedtountitled1_OpeningFcnviavarargin.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelpuntitled1
%LastModifiedbyGUIDE21-Dec-201419:
55:
20
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@untitled1_OpeningFcn,...
'gui_OutputFcn',@untitled1_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&&ischar(varargin{1})
=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbeforeuntitled1ismadevisible.
functionuntitled1_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%varargincommandlineargumentstountitled1(seeVARARGIN)
%Choosedefaultcommandlineoutputforuntitled1
=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakesuntitled1waitforuserresponse(seeUIRESUME)
%uiwait;
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=untitled1_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=;
functionedit_Callback(hObject,eventdata,handles)
%hObjecthandletoedit(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Hints:
get(hObject,'String')returnscontentsofeditastext
%str2double(get(hObject,'String'))returnscontentsofeditasadouble
%---Executesduringobjectcreation,aftersettingallproperties.
functionedit_CreateFcn(hObject,eventdata,handles)
%hObjecthandletoedit(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesempty-handlesnotcreateduntilafterallCreateFcnscalled
%Hint:
editcontrolsusuallyhaveawhitebackgroundonWindows.
%SeeISPCandCOMPUTER.
ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%---Executesonbuttonpressinami.
functionami_Callback(hObject,eventdata,handles)
user_string=get,'string');
a=dec2bin(user_string,8);
b=a';
c=b(:
);
d=c';
f=str2num(d
(1));
fori=2:
length(d)
f=[fstr2num(d(i))];
end
f=[f0];
%%%%%%%%%%%%±àÂë%%%%%%%%%%%%%
p=-1;
fori=1:
length(f);
iff(i)==1
e(i)=(-1)*p;
p=e(i);
else
e(i)=f(i);
end
end
%%%%%%%%%%%%ÒëÂë%%%%%%%%%%%%
fori=1:
length(f);
ife(i)~=0
l(i)=1;
else
e(i)=0;
end
end
axes
t=1:
length(f);
stairs(t-1,f)
axis([0length(f)-22])
xlabel('ASCIIÂë');
gridminor;
axes
t=1:
length(e);
stairs(t-1,e)
axis([0length(e)-22])
xlabel('AMI±àÂë');
gridminor;
axes
t=1:
length(l);
stairs(t-1,l)
axis([0length(l)-22])
xlabel('AMIÒëÂë');
gridminor;
%hObjecthandletoami(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressincmi.
functioncmi_Callback(hObject,eventdata,handles)
user_string=get,'string');
a=dec2bin(user_string,8);
b=a';
c=b(:
);
d=c';
f=str2num(d
(1));
fori=2:
length(d)
f=[fstr2num(d(i))];
end
f=[f0];
%%%%%%%%%%%%±àÂë%%%%%%%%%%%%%
m=1;
p=-1;
fori=1:
length(f)
if(f(i)==1&m==1)
s(i)=3;
m=m*p;
elseif(f(i)==1&m==-1)
s(i)=0;
m=m*p;
else
s(i)=1;
end
end
end
k=dec2bin(s,2);
l=k';
x=l(:
);
y=x';
n=str2num(y
(1));
fori=2:
length(y)
n=[nstr2num(y(i))];
end
%%%%%%%%%%%%ÒëÂë%%%%%%%%%%%%%
ifn
(1)==0
ifn
(2)==1
cm=[0];
else
cm=[1];
end
else
cm=[1];
end
r=3;
whilerifn(r)==0
ifn(r+1)==1
cm=[cm0];
else
cm=[cm1];
end
else
cm=[cm1];
end
r=r+2;
end
axes
t=1:
length(f);
stairs(t-1,f)
axis([0length(f)-22])
xlabel('ASCIIÂë');
gridminor;
axes
t=1:
length(n);
stairs(t-1,n)
axis([0length(n)-22])
xlabel('CMI±àÂë');
gridminor;
axes
t=1:
length(cm);
stairs(t-1,cm)
axis([0length(cm)-22])
xlabel('CMIÒëÂë');
gridminor;
%hObjecthandletocmi(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinhdb3.
functionhdb3_Callback(hObject,eventdata,handles)
user_string=get,'string');
a=dec2bin(user_string,8);
b=a';
b=a';
c=b(:
);
d=c';
xn=str2num(d
(1));
fori=2:
length(d)
xn=[xnstr2num(d(i))];
end
xn=[xn0];
%%%%%%%%%%%%±àÂë%%%%%%%%%%%%%
yn=xn;%Êä³öyn³õʼ»¯
num=0;%¼ÆÊýÆ÷³õʼ»¯
fork=1:
length(xn)
ifxn(k)==1
num=num+1;%"1"¼ÆÊýÆ÷
ifnum/2==fix(num/2)%ÆæÊý¸ö1ʱÊä³ö-1,½øÐм«ÐÔ½»Ìæ
yn(k)=1;
else
yn(k)=-1;
end
end
end
%HDB3±àÂë
num=0;%Á¬Áã¼ÆÊýÆ÷³õʼ»¯
yh=yn;%Êä³ö³õʼ»¯
sign=0;%¼«ÐÔ±êÖ¾³õʼ»¯Îª0
V=zeros(1,length(yn));%VÂö³åλÖüǼ±äÁ¿
B=zeros(1,length(yn));%BÂö³åλÖüǼ±äÁ¿
fork=1:
length(yn)
ifyn(k)==0
num=num+1;%Á¬¡°0¡±¸öÊý¼ÆÊý
ifnum==4%Èç¹û4Á¬¡°0¡±
num=0;%¼ÆÊýÆ÷ÇåÁã
yh(k)=1*yh(k-4);
%ÈÃ0000µÄ×îºóÒ»¸ö0¸Ä±äΪÓëÇ°Ò»¸ö·ÇÁã·ûºÅÏàͬ¼«ÐԵķûºÅ
V(k)=yh(k);%VÂö³åλÖüǼ
ifyh(k)==sign%Èç¹ûµ±Ç°V·ûºÅÓëÇ°Ò»¸öV·ûºÅµÄ¼«ÐÔÏàͬ
yh(k)=-1*yh(k);%ÔòÈõ±Ç°V·ûºÅ¼«ÐÔ·´×ª,ÒÔÂú×ãV·ûºÅ¼äÏ໥¼«ÐÔ·´×ªÒªÇó
yh(k-3)=yh(k);%Ìí¼ÓB·ûºÅ,ÓëV·ûºÅͬ¼«ÐÔ
B(k-3)=yh(k);%BÂö³åλÖüǼ
V(k)=yh(k);%VÂö³åλÖüǼ
yh(k+1:
length(yn))=-1*yh(k+1:
length(yn));
%²¢ÈúóÃæµÄ·ÇÁã·ûºÅ´ÓV·ûºÅ¿ªÊ¼ÔÙ½»Ìæ±ä»¯
end
sign=yh(k);%¼Ç¼ǰһ¸öV·ûºÅµÄ¼«ÐÔ
end
else
num=0;%µ±Ç°ÊäÈëΪ¡°1¡±ÔòÁ¬¡°0¡±¼ÆÊýÆ÷ÇåÁã
end
end
%%%%%%%%%%%%ÒëÂë%%%%%%%%%%%%%
input=yh;%HDB3ÂëÊäÈë
decode=input;%Êä³ö³õʼ»¯
sign=0;%¼«ÐÔ±êÖ¾³õʼ»¯
fork=1:
length(yh)
ifinput(k)~=0
ifsign==yh(k)%Èç¹ûµ±Ç°ÂëÓëÇ°Ò»¸ö·ÇÁãÂëµÄ¼«ÐÔÏàͬ
decode(k-3:
k)=[0000];%Ôò¸ÃÂëÅÐΪVÂë²¢½«*00VÇåÁã
end
sign=input(k);%¼«ÐÔ±êÖ¾
end
end
decode=abs(decode);%ÕûÁ÷
axes
t=1:
length(x