matlab(7-4)汉明码和(7-4)循环码的编程设计
matlab编程实现汉明码 -回复

matlab编程实现汉明码-回复Matlab编程实现汉明码汉明码(Hamming Code)是一种错误检测和纠正算法,广泛应用于数字通信和计算机存储中。
它可以检测和纠正发生在数据传输过程中的单一比特错误。
在本篇文章中,我们将一步一步地介绍如何使用Matlab编程实现汉明码。
第一步:了解汉明码的原理在了解如何编程实现汉明码之前,我们需要先了解汉明码的原理。
汉明码利用了冗余位的概念,在原始数据中插入一些附加位来进行错误检测和纠正。
这些冗余位的位置通常是2的幂次方。
假设我们有一个长度为k的数据码字,我们需要在其中插入r个冗余位。
这样,我们可以得到一个总位数为n的码字,其中n = k + r。
这些冗余位根据特定的规则计算得到,而这些规则取决于汉明码的类型。
第二步:选择汉明码的类型汉明码有许多不同的类型,其中最常见的是(7, 4)汉明码。
这意味着我们将4位数据码字扩展为7位码字。
在本文中,我们将以(7, 4)汉明码为例进行讲解。
第三步:生成汉明码的规则(7, 4)汉明码的生成规则如下:1. 在原始数据的每个第2的幂次方位置上插入一个冗余位。
2. 每个冗余位的值等于它所对应的原始数据位和与它进行异或操作的位的和。
即,第1个冗余位的值等于第3、5、7位的异或和,第2个冗余位的值等于第3、6、7位的异或和,第4个冗余位的值等于第5、6、7位的异或和。
第四步:编写Matlab代码实现(7, 4)汉明码下面是一个Matlab函数生成(7, 4)汉明码的例子:matlabfunction hamming_code = hamming_encode(data)Check the input data lengthif (length(data) ~= 4)error('Input data length should be 4.');endGenerate the parity bitshamming_code(1) = xor(xor(data(3), data(5)), data(7));hamming_code(2) = xor(xor(data(3), data(6)), data(7));hamming_code(3:7) = data;end在上面的代码中,我们首先检查输入的数据长度是否为4位。
matlab编程实现汉明码 -回复

matlab编程实现汉明码-回复Matlab编程实现汉明码汉明码(Hamming code)是一种用于纠正错误的编码方法,可用于检测和纠正单个比特的错误。
在信息传输过程中,数据可能会因为电磁干扰或噪声而产生错误。
通过使用汉明码,我们可以在接收到带有错误的数据时进行错误检测和纠正,从而提高数据传输的可靠性。
本文将介绍如何使用Matlab编程实现汉明码。
我们将以步骤的形式逐渐实现编码和解码过程。
第一步:理解汉明码的原理汉明码是一种线性块码,通过在发送数据之前添加冗余信息(校验位),可以使得接收方检测和纠正单个比特的错误。
汉明码的冗余信息包含了数据的位置信息,从而能够检测并纠正错误。
第二步:生成汉明码的矩阵表示生成汉明码的第一步是确定矩阵H,该矩阵表示汉明码的校验位分布。
矩阵H是一个n×k的矩阵,其中n是码字的总长度,k是数据位的长度。
根据汉明码的性质,矩阵H应满足两个条件:1. 任意两行之间的汉明距离至少为3,以确保可以检测到至少一个错误比特。
2. 列向量之间的汉明距离应大于等于2,以确保可以纠正单个比特的错误。
矩阵H的构建可以采用循环移位操作来实现。
第三步:编码在编码过程中,我们将使用生成矩阵G来将输入数据转换为汉明码。
生成矩阵G是矩阵H的转置,并且是一个k×n的矩阵。
编码的过程中,我们将输入的k位数据向量乘以生成矩阵G得到n位汉明码。
第四步:引入错误为了模拟实际情况下可能发生的错误,我们可以在汉明码中引入一定数量的错误比特。
这可以通过随机地翻转一些比特来实现。
第五步:解码解码汉明码的过程就是检测并纠正错误比特的过程。
为了检测错误,我们将接收到的n位汉明码与矩阵H进行乘法运算得到校验位向量。
如果校验位向量非零,则表示有错误发生。
为了纠正错误,我们可以通过汉明码的特性来确定错误比特的位置,并将其翻转。
这是一个基本的汉明码编码和解码的过程的简要描述。
现在我们将使用Matlab进行实现。
信息论(7,4)汉明码(7,3)循环码

functionvarargout = Hamming(varargin)% HAMMING M-file 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 thelocal% 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_OpeningFunction 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 07-Jan-2008 20:22:48% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @Hamming_OpeningFcn, ...'gui_OutputFcn', @Hamming_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);ifnargin&&ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endifnargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before Hamming is made visible.functionHamming_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 Hamming (see VARARGIN)% Choose default command line output for Hamminghandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes Hamming wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. functionvarargout = Hamming_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 structurevarargout{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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&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)chose=get(handles.popupmenu1,'value');if chose==1;clc;msg=str2num(get(handles.edit1,'string'))%信息产生code=encode(msg,7,4);%汉明编码rcv=decode(code,7,4);%汉明译码set(handles.edit2,'string',num2str(code));set(handles.edit3,'string',num2str(rcv));elsek=str2num(get(handles.edit1,'string'));code1=encode(k,7,3,'cyclic/binary');rcv1=decode(code1,7,3,'cyclic/binary');set(handles.edit2,'string',num2str(code1));set(handles.edit3,'string',num2str(rcv1));endfunction edit7_Callback(hObject, eventdata, handles)% hObject handle to edit7 (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 edit7 as text% str2double(get(hObject,'String')) returns contents of edit7 as a double% --- Executes during object creation, after setting all properties. function edit7_CreateFcn(hObject, eventdata, handles)% hObject handle to edit7 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit8_Callback(hObject, eventdata, handles)% hObject handle to edit8 (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 edit8 as text% str2double(get(hObject,'String')) returns contents of edit8 as a double% --- Executes during object creation, after setting all properties. function edit8_CreateFcn(hObject, eventdata, handles)% hObject handle to edit8 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit9_Callback(hObject, eventdata, handles)% hObject handle to edit9 (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 edit9 as text% str2double(get(hObject,'String')) returns contents of edit9 asa double% --- Executes during object creation, after setting all properties. function edit9_CreateFcn(hObject, eventdata, handles)% hObject handle to edit9 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit10_Callback(hObject, eventdata, handles)% hObject handle to edit10 (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 edit10 as text% str2double(get(hObject,'String')) returns contents of edit10 as a double% --- Executes during object creation, after setting all properties. function edit10_CreateFcn(hObject, eventdata, handles)% hObject handle to edit10 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit15_Callback(hObject, eventdata, handles)% hObject handle to edit15 (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 edit15 as text% str2double(get(hObject,'String')) returns contents of edit15 as a double% --- Executes during object creation, after setting all properties. function edit15_CreateFcn(hObject, eventdata, handles)% hObject handle to edit15 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');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)chose=get(handles.popupmenu1,'value');if chose==1;[H,G,N,K]=hammgen(3); %产生H和G矩阵for i=1:2^Kfor j=K:-1:1if rem(i-1,2^(-j+K+1))>=2^(-j+K)U(i,j)=1;elseU(i,j)=0;endendendC=rem(U*G,2);A=str2num(get(handles.edit7,'string'));[r,l]=size(A);E=[0 0 0 0 0 0 0;0 0 0 0 0 0 1;0 0 0 0 0 1 0;0 0 0 0 1 0 0;0 0 0 1 0 00;0 0 1 0 0 0 0;0 1 0 0 0 0 0;1 0 0 0 0 0 0]; %%%%%求校正子,然后将其转化成十进制数S=rem(A*H',2);Sx=bi2de(S,'left-msb');%%%%下面是(7,4)码检错for i=1:rswitch(Sx)case 0disp=('此接收码字没错')case 1disp=('注意:此接收码字的第3位有错,请纠正')case 2disp=('注意:此接收码字的第2位有错,请纠正')case 4disp=('注意:此接收码字的第1位有错,请纠正')case 3disp=('注意:此接收码字的第5位有错,请纠正')case 5disp=('注意:此接收码字的第7位有错,请纠正')case 6disp=('注意:此接收码字的第4位有错,请纠正')case 7disp=('注意:此接收码字的第6位有错,请纠正')endendset(handles.edit8,'string',disp);%%%下面为在知道哪位出错的情况下,进行纠正for i=1:rswitch(Sx)case 0B(i,:)=A(i,:)+E(1,:);case 1B(i,:)=A(i,:)+E(6,:);case 2B(i,:)=A(i,:)+E(7,:);case 4B(i,:)=A(i,:)+E(8,:);case 3B(i,:)=A(i,:)+E(4,:);case 5B(i,:)=A(i,:)+E(2,:);case 6B(i,:)=A(i,:)+E(5,:);case 7B(i,:)=A(i,:)+E(3,:);endendB=rem(B,2);set(handles.edit9,'string',num2str(B));elsegenpoly=cyclpoly(7,3);[p,g]=cyclgen(7,genpoly);c=str2num(get(handles.edit7,'string'));s=rem(p*c',2);Sx=s(1,1)+s(2,1)*2+s(3,1)*4+s(4,1)*8;switch(Sx)case 0set(handles.edit8,'string','此接收碼无错');set(handles.edit9,'string',num2str(c));case 1if c(1,1)==0c(1,1)=1;elsec(1,1)=0endset(handles.edit8,'string','注意:此接收码字的第一位有错,请纠正');set(handles.edit9,'string',num2str(c));case 2if c(1,2)==0c(1,2)=1;elsec(1,2)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第二位有错,请纠正');case 4if c(1,3)==0c(1,3)=1;elsec(1,3)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第三位有错,请纠正');case 8if c(1,4)==0c(1,4)=1;elsec(1,4)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第四位有错,请纠正');case 13if c(1,5)==0c(1,5)=1;elsec(1,5)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第五位有错,请纠正');case 7if c(1,6)==0c(1,6)=1;elsec(1,6)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第六位有错,请纠正');case 14if c(1,7)==0c(1,7)=1;elsec(1,7)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第七位有错,请纠正');case{3,5,6,9,10,11,12,15}set(handles.edit8,'string','注意:此接收码字有错');set(handles.edit9,'string','注意:无法纠正无法判别错在哪位'); endend% --- Executes on selection change in popupmenu1.function popupmenu1_Callback(hObject, eventdata, handles)% hObject handle to popupmenu1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array% contents{get(hObject,'Value')} returns selected item from popupmenu1% --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles)% hObject handle to popupmenu1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end。
matlab(74)汉明码和(74)循环码的编程设计

二、创新实验设计创新实验一:(7,4)汉明码的编码与译码实现1、实验目的实现(7,4)汉明码的编码与译码,通过这次实验不但加深了对汉明码编码和译码原理了解,而且对线性分组码有所了解。
2、实验原理线性分组码的构造方法比较简单、理论较为成熟,应用比较广泛。
汉明码是一种能够纠正一个错码的效率比较高的线性分组码,下面以(7,4)码为例就汉明码的编码与译码分别进行介绍:(1)编码原理一般来说,若汉明码长为n ,信息位数为k ,则监督位数r=n-k 。
若希望用r 个监督位构造出r 个监督关系式来指示一位错码的n 种可能位置,则要求21r n -≥或211rk r -≥++ (1)设汉明码(n,k )中k=4,为了纠正一位错码,由式(1)可知,要求监督位数r ≥3。
若取r=3,则n=k+r=7。
这样就构成了(7,4)码。
用6543210a a a a a a a 来表示这7个码元,用123s s s 的值表示3个监督关系式中的校正子,则123s s s 的值与错误码元位置的对应关系可以规定如表1所列。
表2.1 校正子和错码位置的关系则由表1可得监督关系式:16542s a a a a =⊕⊕⊕()226531s a a a a =⊕⊕⊕()3 36430s a a a a =⊕⊕⊕()4 在发送端编码时,信息位6543a a a a 的值决定于输入信号,因此它们是随机的。
监督位2a 、1a 、0a 应根据信息位的取值按监督关系来确定,为使所编的码中无错码,则123,,S S S 等于0,即65426531643000(5)0a a a a a a a a a a a a ⊕⊕⊕=⎧⎪⊕⊕⊕=⎨⎪⊕⊕⊕=⎩方程组(5)可等效成如下矩阵形式6543210111010001101010010110010a a a a a a a ⎡⎤⎢⎥⎢⎥⎢⎥⎡⎤⎡⎤⎢⎥⎢⎥⎢⎥=⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎢⎥⎢⎥⎢⎥⎣⎦(6)式(6)可简化为0T T HA =,H 为监督矩阵,则由式(6)可得到监督矩阵11101001101010=[P I ] (7)1011001r H ⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦因为生成矩阵'=[I Q]=[I ]k k G P ,所以由(7)得生成矩阵G 如下:[]k 10001110100110[']00101010001011k G I Q I P ⎡⎤⎢⎥⎢⎥===⎢⎥⎢⎥⎣⎦然后利用信息位和生成矩阵G 相乘产生整个码组,即有[][]65432106543=(8)A a a a a a a a a a a a G=其中A 为整个码组矩阵,6543a a a a 是信息位。
(完整word版)matlab(7,4)汉明码和(7,4)循环码的编程设计

二、创新实验设计创新实验一:(7,4)汉明码的编码与译码实现1、实验目的实现(7,4)汉明码的编码与译码,通过这次实验不但加深了对汉明码编码和译码原理了解,而且对线性分组码有所了解。
2、实验原理线性分组码的构造方法比较简单、理论较为成熟,应用比较广泛。
汉明码是一种能够纠正一个错码的效率比较高的线性分组码,下面以(7,4)码为例就汉明码的编码与译码分别进行介绍:(1)编码原理一般来说,若汉明码长为n ,信息位数为k ,则监督位数r=n-k 。
若希望用r 个监督位构造出r 个监督关系式来指示一位错码的n 种可能位置,则要求21r n -≥或211rk r -≥++ (1)设汉明码(n,k )中k=4,为了纠正一位错码,由式(1)可知,要求监督位数r ≥3。
若取r=3,则n=k+r=7。
这样就构成了(7,4)码。
用6543210a a a a a a a 来表示这7个码元,用123s s s 的值表示3个监督关系式中的校正子,则123s s s 的值与错误码元位置的对应关系可以规定如表1所列。
表2.1 校正子和错码位置的关系则由表1可得监督关系式:16542s a a a a =⊕⊕⊕()226531s a a a a =⊕⊕⊕()3 36430s a a a a =⊕⊕⊕()4 在发送端编码时,信息位6543a a a a 的值决定于输入信号,因此它们是随机的。
监督位2a 、1a 、a 应根据信息位的取值按监督关系来确定,为使所编的码中无错码,则123,,S S S 等于0,即65426531643000(5)0a a a a a a a a a a a a ⊕⊕⊕=⎧⎪⊕⊕⊕=⎨⎪⊕⊕⊕=⎩方程组(5)可等效成如下矩阵形式6543210111010001101010010110010a a a a a a a ⎡⎤⎢⎥⎢⎥⎢⎥⎡⎤⎡⎤⎢⎥⎢⎥⎢⎥=⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎢⎥⎢⎥⎢⎥⎣⎦(6)式(6)可简化为0T T HA =,H 为监督矩阵,则由式(6)可得到监督矩阵11101001101010=[P I ] (7)1011001r H ⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦因为生成矩阵'=[I Q]=[I ]k k G P ,所以由(7)得生成矩阵G 如下:[]k 10001110100110[']00101010001011k G I Q I P ⎡⎤⎢⎥⎢⎥===⎢⎥⎢⎥⎣⎦然后利用信息位和生成矩阵G 相乘产生整个码组,即有[][]65432106543=(8)A a a a a a a a a a a a G=其中A 为整个码组矩阵,6543a a a a 是信息位。
基于matlab的(7,4)循环码的编译仿真设计

Equation Chapter 1 Section 1Harbin Institute of Technology信息论与编码实验报告基于MATLAB的(7,4)循环码的编译仿真院系:电子与信息工程学院姓名:周才发学号:13S005051班级:通信二班哈尔滨工业大学基于MATLAB的(7,4)循环码的编译仿真(电子与信息工程学院13S005051 周才发序号:15)1、循环码简介随着社会经济的迅速发展和科学技术的全面进步,计算机事业的飞速发展,以计算机与通信技术为基础的信息系统正处于蓬勃发展的时期。
随着经济文化水平的显著提高,人们对生活质量及工作软件的要求也越来越高。
在计算机通信信息码中循环码是线性分组码的一个重要子集,是目前研究得最成熟的一类码。
它有许多特殊的代数性质,它使计算机通信以一种以数据通信形式出现,实现了在计算机与计算机之间或计算机与终端设备之间进行有效的与正确地信息传递,它使得现代通信的可靠性与有效性实现了质的飞跃。
它是现代计算机技术与通信技术飞速发展的产物,在日常生活通信领域、武器控制系统等领域都被广泛应用。
纠错码(error correcting code),在传输过程中发生错误后能在收端自行发现或纠正的码。
仅用来发现错误的码一般常称为检错码。
为使一种码具有检错或纠错能力,须对原码字增加多余的码元,以扩大码字之间的差别,即把原码字按某种规则变成有一定剩余度(见信源编码)的码字,并使每个码字的码之间有一定的关系。
关系的建立称为编码。
码字到达收端后,可以根据编码规则是否满足以判定有无错误。
当不能满足时,按一定规则确定错误所在位置并予以纠正。
纠错并恢复原码字的过程称为译码。
检错码与其他手段结合使用,可以纠错。
纠错编码又称信道编码,它与信源编码是信息传输的两个方面。
它们之间存在对偶的关系。
应用信道译码直接对一些自然信息进行处理,可以去掉剩余度,以达到压缩数据的目的。
为了使一种码具有检错或纠错能力,必须对原码字增加多余的码元,以扩大码字之间的差别,使一个码字在一定数目内的码元上发生错误时,不致错成另一个码字。
基于某matlab地(7,4)汉明码编译仿真

东北大学分校电子信息系综合课程设计基于matlab的(7,4)汉明码编译仿真专业名称通信工程班级学号4090719学生何云瑞指导教师雅珍设计时间2011.12.19~2012.1.4课程设计任务书专业:通信工程学号:409719 学生(签名):设计题目:(7,4)汉明码的编码、译码仿真一、设计实验条件电子信息创新实验室二、设计任务及要求1.了解、掌握汉明码编码、译码原理;2.用仿真软件对(7.4)汉明码进行编码、译码仿真;3.画出编码后、译码后的信号波形图。
三、设计报告的容1.设计题目与设计任务(设计任务书)2.前言(绪论)(设计的目的、意义等)3.设计主体(各部分设计容、分析、结论等)4.结束语(设计的收获、体会等)5.参考资料四、设计时间与安排1、设计时间:2周2、设计时间安排:熟悉实验设备、收集资料:天设计图纸、实验、计算、程序编写调试:天编写课程设计报告:天答辩:天目录摘要 (I)1.引言 (1)2.设计原理 (2)2.1汉明码编码原理 (2)2.2汉明码纠错原理 (4)3.程序与SIMULINK仿真 (6)3.1程序 (6)3.1.1程序函数介绍 (6)3.1.2 主程序 (6)3.1.3调试结果 (7)3.2 SIMULINK仿真 (8)3.2.1 SIMULINK电路图 (8)3.2.2 模块参数设置 (9)3.2.3仿真波形 (11)3.2.4仿真结果分析 (15)4.总结 (16)参考文献 (17)摘要汉明码(Hamming Code)是一种能够自动检测并纠正一位错码的线性纠错码,即SEC (Single Error Correcting)码,用于信道编码与译码中,提高通信系统抗干扰的能力。
本文主要利用MATLAB中SIMULINK通信系统仿真模型库进行汉明码建模仿真,并调用通信系统功能函数对外界输入的信息进行汉明码的编、译码,绘制时域波形及误码率与信噪比关系曲线图。
在此基础上,对汉明码的性能进行分析,得出汉明码能降低噪声干扰的结论。
信息论(7,4)汉明码(7,3)循环码

functionvarargout = Hamming(varargin)% HAMMING M-file 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 thelocal% 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_OpeningFunction 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 07-Jan-2008 20:22:48% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @Hamming_OpeningFcn, ...'gui_OutputFcn', @Hamming_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);ifnargin&&ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endifnargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before Hamming is made visible.functionHamming_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 Hamming (see VARARGIN)% Choose default command line output for Hamminghandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes Hamming wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. functionvarargout = Hamming_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 structurevarargout{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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction 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.ifispc&&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)chose=get(handles.popupmenu1,'value');if chose==1;clc;msg=str2num(get(handles.edit1,'string'))%信息产生code=encode(msg,7,4);%汉明编码rcv=decode(code,7,4);%汉明译码set(handles.edit2,'string',num2str(code));set(handles.edit3,'string',num2str(rcv));elsek=str2num(get(handles.edit1,'string'));code1=encode(k,7,3,'cyclic/binary');rcv1=decode(code1,7,3,'cyclic/binary');set(handles.edit2,'string',num2str(code1));set(handles.edit3,'string',num2str(rcv1));endfunction edit7_Callback(hObject, eventdata, handles)% hObject handle to edit7 (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 edit7 as text% str2double(get(hObject,'String')) returns contents of edit7 as a double% --- Executes during object creation, after setting all properties. function edit7_CreateFcn(hObject, eventdata, handles)% hObject handle to edit7 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit8_Callback(hObject, eventdata, handles)% hObject handle to edit8 (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 edit8 as text% str2double(get(hObject,'String')) returns contents of edit8 as a double% --- Executes during object creation, after setting all properties. function edit8_CreateFcn(hObject, eventdata, handles)% hObject handle to edit8 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit9_Callback(hObject, eventdata, handles)% hObject handle to edit9 (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 edit9 as text% str2double(get(hObject,'String')) returns contents of edit9 asa double% --- Executes during object creation, after setting all properties. function edit9_CreateFcn(hObject, eventdata, handles)% hObject handle to edit9 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit10_Callback(hObject, eventdata, handles)% hObject handle to edit10 (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 edit10 as text% str2double(get(hObject,'String')) returns contents of edit10 as a double% --- Executes during object creation, after setting all properties. function edit10_CreateFcn(hObject, eventdata, handles)% hObject handle to edit10 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit15_Callback(hObject, eventdata, handles)% hObject handle to edit15 (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 edit15 as text% str2double(get(hObject,'String')) returns contents of edit15 as a double% --- Executes during object creation, after setting all properties. function edit15_CreateFcn(hObject, eventdata, handles)% hObject handle to edit15 (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.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');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)chose=get(handles.popupmenu1,'value');if chose==1;[H,G,N,K]=hammgen(3); %产生H和G矩阵for i=1:2^Kfor j=K:-1:1if rem(i-1,2^(-j+K+1))>=2^(-j+K)U(i,j)=1;elseU(i,j)=0;endendendC=rem(U*G,2);A=str2num(get(handles.edit7,'string'));[r,l]=size(A);E=[0 0 0 0 0 0 0;0 0 0 0 0 0 1;0 0 0 0 0 1 0;0 0 0 0 1 0 0;0 0 0 1 0 00;0 0 1 0 0 0 0;0 1 0 0 0 0 0;1 0 0 0 0 0 0]; %%%%%求校正子,然后将其转化成十进制数S=rem(A*H',2);Sx=bi2de(S,'left-msb');%%%%下面是(7,4)码检错for i=1:rswitch(Sx)case 0disp=('此接收码字没错')case 1disp=('注意:此接收码字的第3位有错,请纠正')case 2disp=('注意:此接收码字的第2位有错,请纠正')case 4disp=('注意:此接收码字的第1位有错,请纠正')case 3disp=('注意:此接收码字的第5位有错,请纠正')case 5disp=('注意:此接收码字的第7位有错,请纠正')case 6disp=('注意:此接收码字的第4位有错,请纠正')case 7disp=('注意:此接收码字的第6位有错,请纠正')endendset(handles.edit8,'string',disp);%%%下面为在知道哪位出错的情况下,进行纠正for i=1:rswitch(Sx)case 0B(i,:)=A(i,:)+E(1,:);case 1B(i,:)=A(i,:)+E(6,:);case 2B(i,:)=A(i,:)+E(7,:);case 4B(i,:)=A(i,:)+E(8,:);case 3B(i,:)=A(i,:)+E(4,:);case 5B(i,:)=A(i,:)+E(2,:);case 6B(i,:)=A(i,:)+E(5,:);case 7B(i,:)=A(i,:)+E(3,:);endendB=rem(B,2);set(handles.edit9,'string',num2str(B));elsegenpoly=cyclpoly(7,3);[p,g]=cyclgen(7,genpoly);c=str2num(get(handles.edit7,'string'));s=rem(p*c',2);Sx=s(1,1)+s(2,1)*2+s(3,1)*4+s(4,1)*8;switch(Sx)case 0set(handles.edit8,'string','此接收碼无错');set(handles.edit9,'string',num2str(c));case 1if c(1,1)==0c(1,1)=1;elsec(1,1)=0endset(handles.edit8,'string','注意:此接收码字的第一位有错,请纠正');set(handles.edit9,'string',num2str(c));case 2if c(1,2)==0c(1,2)=1;elsec(1,2)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第二位有错,请纠正');case 4if c(1,3)==0c(1,3)=1;elsec(1,3)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第三位有错,请纠正');case 8if c(1,4)==0c(1,4)=1;elsec(1,4)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第四位有错,请纠正');case 13if c(1,5)==0c(1,5)=1;elsec(1,5)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第五位有错,请纠正');case 7if c(1,6)==0c(1,6)=1;elsec(1,6)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第六位有错,请纠正');case 14if c(1,7)==0c(1,7)=1;elsec(1,7)=0endset(handles.edit9,'string',num2str(c));set(handles.edit8,'string','注意:此接收码字的第七位有错,请纠正');case{3,5,6,9,10,11,12,15}set(handles.edit8,'string','注意:此接收码字有错');set(handles.edit9,'string','注意:无法纠正无法判别错在哪位'); endend% --- Executes on selection change in popupmenu1.function popupmenu1_Callback(hObject, eventdata, handles)% hObject handle to popupmenu1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array% contents{get(hObject,'Value')} returns selected item from popupmenu1% --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles)% hObject handle to popupmenu1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER.ifispc&&isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
二、创新实验设计创新实验一:(7,4)汉明码的编码与译码实现1、实验目的实现(7,4)汉明码的编码与译码,通过这次实验不但加深了对汉明码编码和译码原理了解,而且对线性分组码有所了解。
2、实验原理线性分组码的构造方法比较简单、理论较为成熟,应用比较广泛。
汉明码是一种能够纠正一个错码的效率比较高的线性分组码,下面以(7,4)码为例就汉明码的编码与译码分别进行介绍:(1)编码原理一般来说,若汉明码长为n ,信息位数为k ,则监督位数r=n-k 。
若希望用r 个监督位构造出r 个监督关系式来指示一位错码的n 种可能位置,则要求21r n -≥或211rk r -≥++ (1)设汉明码(n,k )中k=4,为了纠正一位错码,由式(1)可知,要求监督位数r ≥3。
若取r=3,则n=k+r=7。
这样就构成了(7,4)码。
用6543210a a a a a a a 来表示这7个码元,用123s s s 的值表示3个监督关系式中的校正子,则123s s s 的值与错误码元位置的对应关系可以规定如表1所列。
表2.1 校正子和错码位置的关系则由表1可得监督关系式:16542s a a a a =⊕⊕⊕()226531s a a a a =⊕⊕⊕()3 36430s a a a a =⊕⊕⊕()4 在发送端编码时,信息位6543a a a a 的值决定于输入信号,因此它们是随机的。
监督位2a 、1a 、a 应根据信息位的取值按监督关系来确定,为使所编的码中无错码,则123,,S S S 等于0,即65426531643000(5)0a a a a a a a a a a a a ⊕⊕⊕=⎧⎪⊕⊕⊕=⎨⎪⊕⊕⊕=⎩方程组(5)可等效成如下矩阵形式6543210111010001101010010110010a a a a a a a ⎡⎤⎢⎥⎢⎥⎢⎥⎡⎤⎡⎤⎢⎥⎢⎥⎢⎥=⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎢⎥⎢⎥⎢⎥⎣⎦(6)式(6)可简化为0T T HA =,H 为监督矩阵,则由式(6)可得到监督矩阵11101001101010=[P I ] (7)1011001r H ⎡⎤⎢⎥=⎢⎥⎢⎥⎣⎦因为生成矩阵'=[I Q]=[I ]k k G P ,所以由(7)得生成矩阵G 如下:[]k 10001110100110[']00101010001011k G I Q I P ⎡⎤⎢⎥⎢⎥===⎢⎥⎢⎥⎣⎦然后利用信息位和生成矩阵G 相乘产生整个码组,即有[][]65432106543=(8)A a a a a a a a a a a a G=其中A 为整个码组矩阵,6543a a a a 是信息位。
根据上述原理可以得到(7,4)汉明码的整个码组。
(2)译码与检错、纠错原理当数字信号编码成汉明码后,由于信道噪声的存在,使得经过信道后的汉明码会发生差错,使得接收端接收到错码,因此需要多错码进行纠正,以提高通信系统的抗干扰能力及可靠性。
下面分析纠错译码原理。
设B 为接收码组,它是一行7列的矩阵,即1234567=[]B b b b b b b b ,B 中可能含有错码,错误图样7654321=[]E A B e e e e e e e =-,在E 的表达式中,有0(0,1,2,7)1i i i i i b a e i b a =⎧==⎨≠⎩当时当时若0i e =,表示该码元没错;若1i e =,表示该码元为错码。
由E A B =-得(9)A B E=+(9)表示接收码组和错误图样之和等于正确码组U ,通过(9)式就可以实现纠错。
可以用标准阵来表示所有可能的2n 个n 元码组的接收矢量,(n,k)码的标准阵形式如下:123452223242523233343532345234522222n kn kn kn kn kj j j j j A A A A A E A E A E A E A E E A E A E A E A E E A E A E A E A E E A E A E A E A E -----++++++++++++++++ 应该注意到,码组1A (全0码字)起两个作用:既是其中一个正确码组,也是错误图样1E ,代表1A 所在行没有错误。
标准阵中的1232,,n k A E E E -是陪集首,陪集首的选择是有规定的,第j 行的陪集首是在前j-1行中没有出现的最小码组,即错误图样E ,如果不选错误图样作为陪集首,译码将会产生错误。
对于(7,4)汉明码,其最小码重是3,设码的纠错能力为t ,根据公式min 12d t -⎡⎤=⎢⎥⎣⎦得该码的纠错能力为1,即能纠错一位错码。
由于根据完备码的定义有02(12)tn kj n j -=⎛⎫= ⎪⎝⎭∑将(7,4)码代入(12)可以得知,(7,4)汉明码为完备码组,只能纠错和检错一位错码。
对于正确码组A 而言,有(10)T AH =当接收到错码变成码组B 时,有()(11)T T T T T S BH A E H AH EH EH ==+=+=其中S 为校正子所构成的校正矩阵,由于S 和E 如(11)所示的一一对应的关系,对于(7,4)码,错误图样与伴随式的对应关系如下表2.2 伴随式查询表实现纠错。
3、实验内容由于编码涉及到矩阵的运算,而matlab在处理矩阵运算方面有独特优势,所以这次选择用matlab工具来实现(7,4)汉明码的编码和译码。
(7,4)码的编码比较简单,已知监督矩阵H,可以通过函数求生成矩阵G,然后信息位和生成矩阵G相乘就可得到所有的码组,(7,4)汉明码的编码程序如下:%%%(7,4)汉明码编码——2012.11.10clear all;clc;H=[1 1 1 0 1 0 0 ;1 1 0 1 0 1 0;1 0 1 1 0 0 1];%(7,4)码的监督矩阵G=gen2par(H)%(7,4)码的生成矩阵m=[0 0 0 0;0 0 0 1;0 0 1 0;0 0 1 1;0 1 0 0;0 1 0 1;0 1 1 0;0 1 1 1;1 0 0 0;1 0 0 1;1 0 1 0;1 0 1 1;1 1 0 0;1 1 0 1;1 1 1 0;1 1 1 1];%%所有的信息位U=[rem(m*G,2)];disp('(7,4)汉明码的编码结果:');disp(U);由原理分析可知,要实现纠错和检错译码,关键在于伴随式查询表,因此如何实现伴随式查询表是编程的关键,(7,4)汉明码的译码程序如下:%%%下面是(7,4)码译码A=input('请输入接收码组:');[r,l]=size(A);E=[0 0 0 0 0 0 0;0 0 0 0 0 0 1;0 0 0 0 0 1 0;0 0 0 0 1 0 0;0 0 0 1 0 0 0;0 0 1 0 0 0 0;0 1 0 0 0 0 0;1 0 0 0 0 0 0];%%%%%求校正子,然后将其转化成十进制数for i=1:rSx=S(i,1)*4+S(i,2)*2+S(i,3);end%%%%下面是(7,4)码检错for i=1:rswitch(Sx)case 0disp('此接收码字没错')case 1disp('注意:此接收码字的第一位有错,请纠正')case 2disp('注意:此接收码字的第二位有错,请纠正')case 4disp('注意:此接收码字的第三位有错,请纠正')case 3disp('注意:此接收码字的第四位有错,请纠正')case 5disp('注意:此接收码字的第五位有错,请纠正')case 6disp('注意:此接收码字的第六位有错,请纠正')case 7disp('注意:此接收码字的第七位有错,请纠正')endend%%%下面为在知道哪位出错的情况下,进行纠正for i=1:rswitch(Sx)case 0B(i,:)=A(i,:)+E(1,:);case 1B(i,:)=A(i,:)+E(2,:);case 2B(i,:)=A(i,:)+E(3,:);case 4B(i,:)=A(i,:)+E(4,:);case 3B(i,:)=A(i,:)+E(5,:);case 5B(i,:)=A(i,:)+E(6,:);case 6B(i,:)=A(i,:)+E(7,:);case 7B(i,:)=A(i,:)+E(8,:);endendB=rem(B,2);disp('纠错后的码字');disp(B);由于可能在纠错过程中会产生错误,在此,我们设计了检错纠错是否产生错误的程序,程序如下:%%%%下面为检查纠错是否产生错误k=1;for k=1:16if (U(k,:)==B)flag=1;breakendendif flag==1disp('纠错无误');elsedisp('注意:纠错有误,请检查')End由于译码的目的就是找出信息位c,所以,需要从纠错后的码组中提取出信息位,提取信息位的程序如下:%%%下面为提取码的信息位for j=1:4c(:,j)=B(:,j);enddisp('信息位c如下:')disp(c)4、实验结果分析运行编码程序后,得到如下结果通过理论计算,可以得出实验结果和理论编码结果吻合,可以发现,(7,4)码的最小码重是3。
当接收码组为[1 1 0 1 1 0 0]时,译码结果如下运行程序后,可以看到:当接收码组为[1 1 0 1 1 0 0]时,会出现“注意:此接收码字的第六位有错,请纠正”的提示,在程序中,为防止纠错错误,我设置了纠错后的码和编码结果的比较,如果纠错后的码和编码结果当中的某个码组相同,说明纠错无误,否则纠错有误。
当接收码组为[1 1 0 1 1 0 0]时,经过纠错后出现纠错无误的结果,说明纠错后的码是正确码组。
然后提取纠错后的码组的信息位c ,从运行结果可以看出,c=1001。
当接收码组为[0 1 0 1 1 0 1]时,运行结果如下:此接收码组本来就是一个正确码组,所以通过运行程序后,会出现“此接收码字没错”的提示,纠错后的码字和接收的码组是相同的,检查到纠错无误,然后提取信息位c ,信息位c=0101,整个译码完成。
5、改进建议(7,4)汉明码的编码与译码相对比较简单,可以通过matlab 编程实现对信道编码性能的分析,可以编程实现比较编码后的误码率和没编码的误码率,也可以编程实现在有噪声的情况下来进行译码。
创新实验二:(7,4)循环码的编码与译码1.实验编码原理: 根据循环码的代数性质建立系统编码的过程,可以把消息矢量用如下多项式表示:12211...)(m x m x m x m x m k k k k ++++=----要编码成系统循环码形式,把消息比特移入码字寄存器的最右边k 位,而把监督比特加在最左边的n-k 个中,则要用k n x -乘以m(x)得到kn x - m(x)=k n x - m(x)= q(x) g(x)+ p(x),其中p(x)可以表示为p(x)= ,则p(x)+ kn x- m(x)= + 另U(x)= p(x)+ k n x - m(x),则U=(0p ,1p ,2p ,···,1--k n p ,0m,1m ,···,1-k m )。