admin 管理员组文章数量: 887021
2024年1月24日发(作者:scalar黑色)
.-
实验内容:
利用Matlab软件的GUI界面编程,做一个简单的界面,通过此界面调用相关的程序去实现由抽样判决后的AMI码型、CMI码型和HDB3码型数字序列恢复出原始的PCM脉冲编码信号。
实验目的:
1,熟悉Matlab的GUI的编程操作,学会运用GUI来建立一个界面
2.掌握AMI,CMI,HDB3码型的特点和意义,学会将PCM脉冲编码信号转化为AMI,CMI,HDB3的编码以及AMI,CMI,HDB3解码的方式。
实验方法:
实验中采用的方法是先建立大概的界面,其中包括输入框、按钮、提示内容及坐标系等。然后再对各种组件设置好属性,并对各个组件编写回调函数,最后进行调试验证饥渴。
实验代码:
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。
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 r 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 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脉冲位置记录变量 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 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); % 整流 实验现象: 总体界面: .- AMI: CMI: HDB3: .- 总结: 1. 本次实验中因为对画方波不太熟悉,在使用stairs函数时,方波图总是在最后少了一位,后来经查询后才知道stairs的正确用法。 2. 由于对GUI界面设置的不熟悉,很多细节都没做到位,界面的美观有待提高。 3. 在编代码是经常没有写注释,导致后来有些变量混淆了。 附录:全部代码 function varargout = 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 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 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 GUIDE's 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 v2.5 21-Dec-2014 19:55: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(varargin{1}) gui__Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = 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 figure % 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); % UIWAIT 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 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 varargout{1} = ; 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 (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, handles) % 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'), 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: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 axes(1) t=1:length(f); stairs(t-1,f) axis([0 length(f) -2 2]) xlabel('ASCIIÂë'); grid minor; .- axes(2) t=1:length(e); stairs(t-1,e) axis([0 length(e) -2 2]) xlabel('AMI ±àÂë'); grid minor; axes(3) 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_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]; %%%%%%%%%%%% ±àÂë %%%%%%%%%%%%% 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 r 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(1) t=1:length(f); stairs(t-1,f) axis([0 length(f) -2 2]) xlabel('ASCIIÂë'); grid minor; axes(2) t=1:length(n); stairs(t-1,n) axis([0 length(n) -2 2]) xlabel('CMI ±àÂë'); grid minor; axes(3) .- 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, 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=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 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(1) t=1:length(xn); stairs(t-1,xn) axis([0 length(xn) -2 2]) xlabel('ASCIIÂë'); grid minor; axes(2) t=1:length(yh); stairs(t-1,yh) .- axis([0 length(yh) -2 2]) xlabel('HDB3 ±àÂë'); grid minor; axes(3) 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) % --- Executes 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 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) % --- 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
版权声明:本文标题:根据matlab的码型转换 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1706062533h500279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论