1、基于某matlab地码型转换实验容:利用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); endend% 译码 %for i=1:length(f); if e(i)=0 l(i) = 1; else e(i) = 0; endend编码:代码中的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。2.
3、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; endend 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; endr=3;while rlength(n
4、) 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;end3. 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 endend % HDB3编码num=0; % 连零计数器初始化yh=yn; % 输出初始化sign=0; % 极性
5、标志初始化为0V=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符号
6、间相互极性反转要求 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”计数器清零 endend % 译码 %input=yh; % HDB3码输入decode=input; % 输出初始化sign=0; % 极性标志初始化for k=1:lengt
7、h(yh) if input(k) = 0 if sign=yh(k) % 如果当前码与前一个非零码的极性相同 decode(k-3:k)=0 0 0 0;% 则该码判为V码并将*00V清零 end sign=input(k); % 极性标志 endenddecode=abs(decode); % 整流实验现象:总体界面:AMI:CMI:HDB3:总结:1. 本次实验中因为对画方波不太熟悉,在使用stairs函数时,方波图总是在最后少了一位,后来经查询后才知道stairs的正确用法。2. 由于对GUI界面设置的不熟悉,很多细节都没做到位,界面的美观有待提高。3. 在编代码是经常没有写注释,导致
8、后来有些变量混淆了。附录:全部代码function varargout = untitled1(varargin)% UNTITLED1 M-file for untitled1.fig% 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
9、,eventData,handles,.) calls the local% function named CALLBACK in UNTITLED1.M 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
10、 gets called. An% unrecognized property name or invalid value makes property application% 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
11、 above text to modify the response to help untitled1% Last Modified by GUIDE v2.5 21-Dec-2014 19:55:20% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename, .gui_Singleton, gui_Singleton, .gui_OpeningFcn, untitled1_OpeningFcn, .gui_OutputFcn, untitled1_Out
12、putFcn, .gui_LayoutFcn, , .gui_Callback, );if nargin & ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before untitle
13、d1 is made visible.function untitled1_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin
14、command line arguments to untitled1 (see VARARGIN)% Choose default command line output for untitled1handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes untitled1 wait for user response (see UIRESUME)% uiwait(handles.maxing);% - Outputs from this function are r
15、eturned 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 be defined in a future version of MATLAB% handles structure with handles and user dat
16、a (see GUIDATA)% Get default command line output from handles structurevarargout1 = handles.output;function edit_Callback(hObject, eventdata, handles)% hObject handle to edit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (s
17、ee 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, handles)% hObject handle to edit (see GCBO)% even
18、tdata 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), get(0,defaultUicontrolBackground
19、Color) set(hObject,BackgroundColor,white);end% - Executes on button press in ami.function ami_Callback(hObject, eventdata, handles)user_string=get(handles.edit,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);endf=f 0;% %p = -1;for i=1:length(f);if f(
20、i)=1 e(i) = (-1)*p; p = e(i);else e(i) = f(i);endend% %for i=1:length(f);if e(i)=0 l(i) = 1;else e(i) = 0;endendaxes(handles.axes1)t=1:length(f);stairs(t-1,f)axis(0 length(f) -2 2)xlabel(ASCII);grid minor;axes(handles.axes2)t=1:length(e);stairs(t-1,e)axis(0 length(e) -2 2)xlabel(AMI );grid minor;axe
21、s(handles.axes3)t=1:length(l);stairs(t-1,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_Ca
22、llback(hObject, eventdata, handles)user_string=get(handles.edit,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);endf=f 0;% %m=1;p=-1;for i=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;endendend k=dec2bin(s,2);
23、 l=k; x=l(:); y=x; n=str2num(y(1);for i=2:length(y) n=n str2num(y(i);end% %if n(1)=0if n(2)=1 cm=0;else cm=1;endelse cm=1;endr=3;while rlength(n)if n(r)=0if n(r+1)=1 cm=cm 0;else cm=cm 1;endelse cm=cm 1;end r=r+2;endaxes(handles.axes1)t=1:length(f);stairs(t-1,f)axis(0 length(f) -2 2)xlabel(ASCII);gr
24、id minor;axes(handles.axes2)t=1:length(n);stairs(t-1,n)axis(0 length(n) -2 2)xlabel(CMI );grid minor;axes(handles.axes3)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% ha
25、ndles structure with handles and user data (see GUIDATA)% - Executes on button press in hdb3.function hdb3_Callback(hObject, eventdata, handles)user_string=get(handles.edit,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);endxn=xn 0;% %yn=xn;%
26、ynnum=0;% for k=1:length(xn)if xn(k)=1 num=num+1; % 1if num/2 = fix(num/2) % 1-1, yn(k)=1;else yn(k)=-1;endendend% HDB3num=0; % yh=yn; % sign=0; % 0V=zeros(1,length(yn);% V B=zeros(1,length(yn);% Bfor k=1:length(yn)if yn(k)=0 num=num+1; % 0if num=4 % 40 num=0; % yh(k)=1*yh(k-4); % 00000 V(k)=yh(k); % Vif yh(k)=sign % VV 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);% Vend sign=yh(k); % Vendelse num=0; % 10endend% %input=yh; % HDB3decode=input; % sign=0; % for k=1:length(yh)if input(k) = 0if sign=yh(k) % decode(k-3:k)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1