function varargout = hamming(varargin)
% HAMMING MATLAB code for hamming.fig
% HAMMING, by itself, creates a new HAMMING or raises the existing
% singleton*.
%
% H = HAMMING returns the handle to a new HAMMING or the handle to
% the existing singleton*.
%
% HAMMING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HAMMING.M with the given input arguments.
%
% HAMMING('Property','Value',...) creates a new HAMMING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before hamming_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to hamming_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 hamming
% Last Modified by GUIDE v2.5 16-Apr-2019 23:33:35
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @hamming_OpeningFcn, ...
'gui_OutputFcn', @hamming_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 hamming is made visible.
function hamming_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = hamming_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function input_data_Callback(hObject, eventdata, handles)
%输入十进制数
data_str=get(handles.input_data,"String");%输入文本
output='';
for i=1:length(data_str)
if(data_str(i)~='1' && data_str(i)~='0')
output="error";
break;
end
end
if output~="error"
data=data_str;
else
data='0';
end
%data=str2num(data_str);%转为十进制数
%转为二进制数
%data=dec2bin(data);%十进制转二进制
%data_str=num2str(data);%转为文本
%set(handles.input_data,"String",data_str);%输出文本
%数据比特长度k
k=length(data);
set(handles.edit_k,"String",num2str(k));
%校验位个数r
r=1;
while(1)
temp=2^r-1-r;
if temp>=k
break;
else
r=r+1;
end
end
set(handles.edit_r,"String",num2str(r));
%编码长度n
n=k+r;
set(handles.edit_n,"String",num2str(n));
%计算海明码
if output=="error"
else
[output,k,r,n] = hammingCode(data);
end
set(handles.output_data,"String",output);
function input_data_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --------------------------------------------------------------------
function hamming_Callback(hObject, eventdata, handles)
%什么都不做
% --------------------------------------------------------------------
function crc_Callback(hObject, eventdata, handles)
%切换为crc编码菜单
h=gcf;
crc;
close(h);
function edit_k_Callback(hObject, eventdata, handles)
function edit_k_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_r_Callback(hObject, eventdata, handles)
function edit_r_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_n_Callback(hObject, eventdata, handles)
function edit_n_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function output_data_Callback(hObject, eventdata, handles)
function output_data_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function input_hamming_Callback(hObject, eventdata, handles)
%输入二进制数
data_str=get(handles.input_hamming,"String");%输入文本
output='';
for i=1:length(data_str)
if(data_str(i)~='1' && data_str(i)~='0')
output="error";
break;
end
end
if output~="error"
data=data_str;
else
data='0';
end
%编码长度n
n=length(data);
set(handles.edit_n1,"String",num2str(n));
%校验位个数r
r=1;
while(1)
temp=2^r-1;
if temp>=n
break;
else
r=r+1;
end
end
set(handles.edit_r1,"String",num2str(r));
%数据比特长度k
k=n-r;
set(handles.edit_k1,"String",num2str(k));
%海明码解码
if output=="error"
else
[output,check,fp,k,r,n] = hammingDecode(data);
end
set(handles.output_hamming,"String",output);
set(handles.checksum,"String",check);
function input_hamming_CreateFcn(hObject, eventdata, handles)
% hObject handle to input_hamming (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 edit_k1_Callback(hObject, eventdata, handles)
function edit_k1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_k1 (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 edit_r1_Callback(hObject, eventdata, handles)
function edit_r1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit_n1_Callback(hObject, eventdata, handles)
function edit_n1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function output_hamming_Callback(hObject, eventdata, handles)
function output_hamming_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function checksum_Callback(hObject, eventdata, handles)
function checksum_CreateFcn(hObject, eventdata, handles)
% hObject handle to checksum (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
评论0