www.pudn.com > simulate.rar > main.m


function varargout = main(varargin) 
% MAIN M-file for main.fig 
%      MAIN, by itself, creates a new MAIN or raises the existing 
%      singleton*. 
% 
%      H = MAIN returns the handle to a new MAIN or the handle to 
%      the existing singleton*. 
% 
%      MAIN('CALLBACK',hObject,eventData,handles,...) calls the local 
%      function named CALLBACK in MAIN.M with the given input arguments. 
% 
%      MAIN('Property','Value',...) creates a new MAIN or raises the 
%      existing singleton*.  Starting from the left, property value pairs are 
%      applied to the GUI before main_OpeningFunction gets called.  An 
%      unrecognized property name or invalid value makes property application 
%      stop.  All inputs are passed to main_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 
 
% Copyright 2002-2003 The MathWorks, Inc. 
 
% Edit the above text to modify the response to help main 
 
% Last Modified by GUIDE v2.5 15-May-2007 20:58:30 
 
% Begin initialization code - DO NOT EDIT 
gui_Singleton = 1; 
gui_State = struct('gui_Name',       mfilename, ... 
    'gui_Singleton',  gui_Singleton, ... 
    'gui_OpeningFcn', @main_OpeningFcn, ... 
    'gui_OutputFcn',  @main_OutputFcn, ... 
    'gui_LayoutFcn',  [] , ... 
    'gui_Callback',   []); 
if nargin && ischar(varargin{1}) 
    gui_State.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 main is made visible. 
function main_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 main (see VARARGIN) 
 
% Choose default command line output for main 
handles.output = hObject; 
 
% Update handles structure 
guidata(hObject, handles); 
 
% UIWAIT makes main wait for user response (see UIRESUME) 
% uiwait(handles.figure1); 
 
 
% --- Outputs from this function are returned to the command line. 
function varargout = main_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} = handles.output; 
 
 
 
function edit1_Callback(hObject, eventdata, handles) 
% hObject    handle to edit1 (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 edit1 as text 
%        str2double(get(hObject,'String')) returns contents of edit1 as a double 
 
 
% --- Executes during object creation, after setting all properties. 
function edit1_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to edit1 (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 
 
 
 
function edit2_Callback(hObject, eventdata, handles) 
% hObject    handle to edit2 (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 edit2 as text 
%        str2double(get(hObject,'String')) returns contents of edit2 as a double 
 
 
% --- Executes during object creation, after setting all properties. 
function edit2_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to edit2 (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 pushbutton1. 
function pushbutton1_Callback(hObject, eventdata, handles) 
% hObject    handle to pushbutton1 (see GCBO) 
% eventdata  reserved - to be defined in a future version of MATLAB 
% handles    structure with handles and user data (see GUIDATA) 
m=deblank(get(handles.edit1,'string')); 
str=strrep(m,' ',''); 
msg=zeros(1,length(str)); 
%m=str2mat(str2num(str)); 
for i=1:length(str) 
    msg(1,i)=str2num(str(1,i)); 
end 
n=deblank(get(handles.edit2,'string')); 
str1=strrep(n,' ',''); 
generator=zeros(1,length(str1)); 
for i=1:length(str1) 
    generator(1,i)=str2num(str1(1,i)); 
end 
c=zeros(1,length(str1)); 
re=zeros(1,length(str1)-1); 
c(1,1)=1; 
for i=1:(length(str1)-1) 
    c(1,i+1)=0; 
    re(1,i)=0; 
end 
%c = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]; % x^k 
 
multip = conv(c,msg);	% 对序列乘以x^16,表示左移16个0 
[divid, remainder]=deconv(multip,generator);    % 对多项式的除,就等于做反卷积 
remainder=mod(remainder(end-length(str1)+2:end),2);	% 对除法的余数做模二运算 
code=[msg,remainder];	% 只是在原序列后面加上CRC码 
[a b]=size(remainder); 
str5=''; 
for i=1:b 
 
    str4=[num2str(remainder(1,i)) ' ']; 
    str5=[str5 str4]; 
    %str5=strcat(str5,str4) 
end 
set(handles.edit3,'string',str5) 
set(handles.text4,'userdata',code) 
%set(handles.text5,'userdata',num2str(code(end-length(str1)+1:end))) 
set(handles.text5,'userdata',generator) 
set(handles.text6,'userdata',str1) 
set(handles.text3,'userdata',re) 
%msgbox(['CRC[',num2str(code(end-16:end)),']校验正确!']); 
% --- Executes on button press in pushbutton2. 
function pushbutton2_Callback(hObject, eventdata, handles) 
% hObject    handle to pushbutton2 (see GCBO) 
% eventdata  reserved - to be defined in a future version of MATLAB 
% handles    structure with handles and user data (see GUIDATA) 
code1=get(handles.text4,'userdata'); 
generator=get(handles.text5,'userdata'); 
str1=get(handles.text6,'userdata'); 
re=get(handles.text3,'userdata'); 
 
m=deblank(get(handles.edit6,'string')); 
str=strrep(m,' ',''); 
if length(str)~=length(code1) 
    msgbox(['接收数据的位数不正确,请正确输入接收数据']) 
    return 
else 
 
    code=zeros(1,length(str)); 
    %m=str2mat(str2num(str)); 
    for i=1:length(str) 
        code(1,i)=str2num(str(1,i)); 
    end 
 
    [divid, remainder]=deconv(code,generator);	% 对多项式的除,就等于做反卷积 
    remainder=mod(remainder(end-length(str1)+2:end),2);	% 对除法的余数做模二运算 
    set(handles.text1,'userdata',remainder) 
    set(handles.text2,'userdata',code) 
    if isequal(remainder,re); 
        set(handles.edit4,'string','1') 
        %set(handles.text5,'userdata',num2str(code(end-length(str1)+1:end))) 
        set(handles.edit5,'string',num2str(remainder)) 
        %msgbox(['CRC[',num2str(code(end-16:end)),']校验正确!']); 
    else 
        %msgbox(['CRC校验错误:[',num2str(remainder),']']); 
        set(handles.edit4,'string','0') 
        %set(handles.text5,'userdata',num2str(code(end-length(str1)+1:end))) 
        set(handles.edit5,'string',num2str(remainder)) 
         
    end 
end 
% --- Executes on button press in pushbutton3. 
function pushbutton3_Callback(hObject, eventdata, handles) 
% hObject    handle to pushbutton3 (see GCBO) 
% eventdata  reserved - to be defined in a future version of MATLAB 
% handles    structure with handles and user data (see GUIDATA) 
code1=get(handles.text4,'userdata'); 
code=get(handles.text2,'userdata'); 
rem=get(handles.text1,'userdata'); 
axes(handles.axes1) 
cla; 
i=1:length(code); 
plot(i,code1,'r',i,code,'k.') 
 
 
function edit3_Callback(hObject, eventdata, handles) 
% hObject    handle to edit3 (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 edit3 as text 
%        str2double(get(hObject,'String')) returns contents of edit3 as a double 
 
 
% --- Executes during object creation, after setting all properties. 
function edit3_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to edit3 (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 
 
 
 
function edit4_Callback(hObject, eventdata, handles) 
% hObject    handle to edit4 (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 edit4 as text 
%        str2double(get(hObject,'String')) returns contents of edit4 as a double 
 
 
% --- Executes during object creation, after setting all properties. 
function edit4_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to edit4 (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 
 
 
 
function edit5_Callback(hObject, eventdata, handles) 
% hObject    handle to edit5 (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 edit5 as text 
%        str2double(get(hObject,'String')) returns contents of edit5 as a double 
 
 
% --- Executes during object creation, after setting all properties. 
function edit5_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to edit5 (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 
 
 
 
 
 
function edit6_Callback(hObject, eventdata, handles) 
% hObject    handle to edit6 (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 edit6 as text 
%        str2double(get(hObject,'String')) returns contents of edit6 as a double 
 
 
% --- Executes during object creation, after setting all properties. 
function edit6_CreateFcn(hObject, eventdata, handles) 
% hObject    handle to edit6 (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