《基于matlab的码型转换.pdf》由会员分享,可在线阅读,更多相关《基于matlab的码型转换.pdf(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、 利用 Matlab 软件的 GUI 界面编程,做一个简单的界面,通过此界面调用相关的程序去实现由抽样判决后的 AMI 码型、CMI 码型和 HDB3码型数字序列恢复出原始的 PCM 脉冲编码信号。实验目的:1,熟悉 Matlab 的 GUI 的编程操作,学会运用 GUI 来建立一个界面 2.掌握 AMI,CMI,HDB3 码型的特点和意义,学会将 PCM 脉冲编码信号转化为 AMI,CMI,HDB3 的编码以及 AMI,CMI,HDB3 解码的方式。实验方法:实验中采用的方法是先建立大概的界面,其中包括输入框、按钮、提示内容及坐标系等。然后再对各种组件设置好属性,并对各个组件编写回调函数,最
2、后进行调试验证饥渴。实验代码:1.AMI 码型转换:%编码%p=-1;for i=1:length(f);if f(i)=1 e(i)=(-1)*p;p=e(i);else e(i)=f(i);end end%译码%for i=1:length(f);if e(i)=0 l(i)=1;else e(i)=0;end end 编码:代码中的 f 代表着输入字符串转化后的 ASCII 码,用变量 p 来实现原信号中 1 的正负交替。例如,f(1)=1,则 e(1)=1,p=1;f(2)=1,则e(2)=-1,p=-1。如此就可以得到正负 1 交替了。译码:当 e(i)不等于 0,就把结果等于 1。
3、2.CMI 码型转换%编码%m=1;p=-1;for i=1:length(f)if(f(i)=1&m=1)s(i)=3;m=m*p;else if(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);for i=2:length(y)n=n str2num(y(i);end%译码%if n(1)=0 if n(2)=1 cm=0;else cm=1;end else cm=1;end r=3;while rlength(n)if n(r)=0 if n(r+
4、1)=1 cm=cm 0;else cm=cm 1;end else cm=cm 1;end r=r+2;end 3.HDB3 码型转换%编码%yn=xn;%输出 yn 初始化 num=0;%计数器初始化 for k=1:length(xn)if xn(k)=1 num=num+1;%1计数器 if num/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
5、 脉冲位置记录变量 B=zeros(1,length(yn);%B 脉冲位置记录变量 for k=1:length(yn)if yn(k)=0 num=num+1;%连“0”个数计数 if num=4%如果 4 连“0”num=0;%计数器清零 yh(k)=1*yh(k-4);%让 0000 的最后一个 0 改变为与前一个非零符号相同极性的符号 V(k)=yh(k);%V 脉冲位置记录 if yh(k)=sign%如果当前 V 符号与前一个 V 符号的极性相同 yh(k)=-1*yh(k);%则让当前 V 符号极性反转,以满足 V 符号间相互极性反转要求 yh(k-3)=yh(k);%添加 B
6、 符号,与 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;%极性标志初始化 for k=1:length(yh)if input(k)=0 if sign=yh(k)%如果
7、当前码与前一个非零码的极性相同 decode(k-3:k)=0 0 0 0;%则该码判为 V 码并将*00V 清零 end sign=input(k);%极性标志 end end decode=abs(decode);%整流 实验现象:总体界面:AMI:CMI:HDB3:总结:1.本次实验中因为对画方波不太熟悉,在使用 stairs 函数时,方波图总是在最后少了一位,后来经查询后才知道 stairs 的正确用法。2.由于对 GUI 界面设置的不熟悉,很多细节都没做到位,界面的美观有待提高。3.在编代码是经常没有写注释,导致后来有些变量混淆了。附录:全部代码 function varargout
8、=untitled1(varargin)%UNTITLED1 M-file for%UNTITLED1,by itself,creates a new UNTITLED1 or raises the existing%singleton*.%H=UNTITLED1 returns the handle to a new UNTITLED1 or the handle to%the existing singleton*.%UNTITLED1(CALLBACK,hObject,eventData,handles,.)calls the local%function named CALLBACK
9、in with the given input arguments.%UNTITLED1(Property,Value,.)creates a new UNTITLED1 or raises the%existing singleton*.Starting from the left,property value pairs are%applied to the GUI before untitled1_OpeningFunction gets called.An%unrecognized property name or invalid value makes property applic
10、ation%stop.All inputs are passed to untitled1_OpeningFcn via varargin.%*See GUI Options on GUIDEs Tools menu.Choose GUI allows only one%instance to run(singleton).%See also:GUIDE,GUIDATA,GUIHANDLES%Edit the above text to modify the response to help untitled1%Last Modified by GUIDE 21-Dec-2014 19:55:
11、20%Begin initialization code-DO NOT EDIT 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,);if nargin&ischar(varargin1)=str2func(varargin1);end if nargout varargout1:n
12、argout=gui_mainfcn(gui_State,varargin:);else gui_mainfcn(gui_State,varargin:);end%End initialization code-DO NOT EDIT%-Executes just before untitled1 is made visible.function untitled1_OpeningFcn(hObject,eventdata,handles,varargin)%This function has no output args,see OutputFcn.%hObject handle to fi
13、gure%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%varargin command line arguments to untitled1(see VARARGIN)%Choose default command line output for untitled1 =hObject;%Update handles structure guidata(hObject,handles);%UIWAI
14、T makes untitled1 wait for user response(see UIRESUME)%uiwait;%-Outputs from this function are returned to the command line.function varargout=untitled1_OutputFcn(hObject,eventdata,handles)%varargout cell array for returning output args(see VARARGOUT);%hObject handle to figure%eventdata reserved-to
15、be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%Get default command line output from handles structure varargout1=;function edit_Callback(hObject,eventdata,handles)%hObject handle to edit(see GCBO)%eventdata reserved-to be defined in a future versio
16、n of MATLAB%handles structure with handles and user data(see GUIDATA)%Hints:get(hObject,String)returns contents of edit as text%str2double(get(hObject,String)returns contents of edit as a double%-Executes during object creation,after setting all properties.function edit_CreateFcn(hObject,eventdata,h
17、andles)%hObject handle to edit(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles empty-handles not created until after all CreateFcns called%Hint:edit controls usually have a white background on Windows.%See ISPC and COMPUTER.if ispc&isequal(get(hObject,BackgroundColor
18、),get(0,defaultUicontrolBackgroundColor)set(hObject,BackgroundColor,white);end%-Executes on button press in ami.function ami_Callback(hObject,eventdata,handles)user_string=get,string);a=dec2bin(user_string,8);b=a;c=b(:);d=c;f=str2num(d(1);for i=2:length(d)f=f str2num(d(i);end f=f 0;%p=-1;for i=1:len
19、gth(f);if f(i)=1 e(i)=(-1)*p;p=e(i);else e(i)=f(i);end end%for i=1:length(f);if e(i)=0 l(i)=1;else e(i)=0;end end axes t=1:length(f);stairs(t-1,f)axis(0 length(f)-2 2)xlabel(ASCII );grid minor;axes t=1:length(e);stairs(t-1,e)axis(0 length(e)-2 2)xlabel(AMI );grid minor;axes t=1:length(l);stairs(t-1,
20、l)axis(0 length(l)-2 2)xlabel(AMI );grid minor;%hObject handle to ami(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%-Executes on button press in cmi.function cmi_Callback(hObject,eventdata,handles)user_string=get,st
21、ring);a=dec2bin(user_string,8);b=a;c=b(:);d=c;f=str2num(d(1);for i=2:length(d)f=f str2num(d(i);end f=f 0;%m=1;p=-1;for i=1:length(f)if(f(i)=1&m=1)s(i)=3;m=m*p;else if(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);for i=2:length(y)n=n str2num(y(i);end%i
22、f n(1)=0 if n(2)=1 cm=0;else cm=1;end else cm=1;end r=3;while rlength(n)if n(r)=0 if n(r+1)=1 cm=cm 0;else cm=cm 1;end else cm=cm 1;end r=r+2;end axes t=1:length(f);stairs(t-1,f)axis(0 length(f)-2 2)xlabel(ASCII );grid minor;axes t=1:length(n);stairs(t-1,n)axis(0 length(n)-2 2)xlabel(CMI );grid mino
23、r;axes t=1:length(cm);stairs(t-1,cm)axis(0 length(cm)-2 2)xlabel(CMI );grid minor;%hObject handle to cmi(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%-Executes on button press in hdb3.function hdb3_Callback(hObject
24、,eventdata,handles)user_string=get,string);a=dec2bin(user_string,8);b=a;b=a;c=b(:);d=c;xn=str2num(d(1);for i=2:length(d)xn=xn str2num(d(i);end xn=xn 0;%yn=xn;%yn num=0;%for k=1:length(xn)if xn(k)=1 num=num+1;%1 if num/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
25、=zeros(1,length(yn);%V B=zeros(1,length(yn);%B for k=1:length(yn)if yn(k)=0 num=num+1;%0 if num=4%4 0 num=0;%yh(k)=1*yh(k-4);%0000 0 V(k)=yh(k);%V if yh(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
26、else num=0;%1 0 end end%input=yh;%HDB3 decode=input;%sign=0;%for k=1:length(yh)if input(k)=0 if sign=yh(k)%decode(k-3:k)=0 0 0 0;%V *00V end sign=input(k);%end end decode=abs(decode);%axes t=1:length(xn);stairs(t-1,xn)axis(0 length(xn)-2 2)xlabel(ASCII );grid minor;axes t=1:length(yh);stairs(t-1,yh)
27、axis(0 length(yh)-2 2)xlabel(HDB3 );grid minor;axes t=1:length(decode);stairs(t-1,decode)axis(0 length(decode)-2 2)xlabel(HDB3 );grid minor;%hObject handle to hdb3(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%-Exec
28、utes when plent is resized.function plent_ResizeFcn(hObject,eventdata,handles)%hObject handle to plent(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%-function plent_ButtonDownFcn(hObject,eventdata,handles)%hObject h
29、andle to plent(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles structure with handles and user data(see GUIDATA)%-Executes during object creation,after setting all properties.function plent_CreateFcn(hObject,eventdata,handles)%hObject handle to plent(see GCBO)%eventdata reserved-to be defined in a future version of MATLAB%handles empty-handles not created until after all CreateFcns called