Hamming Encoder
Create Hamming code from binary vector data
Libraries:
Communications Toolbox /
Error Detection and Correction /
Block
Description
The Hamming Encoder block creates a Hamming code with message length K and codeword length N. For more information, see Hamming Coding.
Examples
This example shows how to model a simple encoder and decoder using appropriate vector lengths for the code and message.
The cm_hamming model includes these blocks:
Bernoulli Binary Generator block with
Samples per frameset to4to match the Hamming encoder message lengthHamming Encoder block with default parameter values
Hamming Decoder block with default parameter values
Error Rate Calculation block with
Output dataset toPortDisplay block connected to the output port of the Error Rate Calculation block
To display the vector length of signals in the model, go to Debug > Diagnostics > Information Overlays > Signals and select Signal Dimensions. The connector lines show the signal attributes. To compile the model, press Ctrl+D. Run the model to display the error rate statistics.

This example shows how to adjust the Eb/N0 setting for the AWGN channel to account for the coding rate when your simulation uses coding.
The slex_hamming_check.slx model performs forward error correction (FEC) coding on a BPSK-modulated signal that gets filtered through an AWGN channel. The model uses BPSK Modulator Baseband, AWGN Channel, and BPSK Demodulator Baseband discrete blocks to simulate a binary symmetric channel. To account for difference between the coded and uncoded Eb/N0, the AWGN channel block computes the coded Eb/N0 as Eb/N0 + 10log10(K/N) dB, where K/N is the code rate and Eb/N0 is the uncoded Eb/N0. The Hamming Encoder block has an input bit period of 1 second and the output bit period decreases, by a factor of the K/N code rate, to 4/7 seconds. Due to the coding rate, the Binary Symmetric Channel has a bit period of 4/7 seconds.
The model initializes variables used to configure block parameters by using the PreLoadFcn callback function. For more information, see Model Callbacks (Simulink).
model = 'slex_hamming_check';
open_system(model);

The AWGN channel block must also configure the Number of bits per symbol and Input signal power, referenced to 1 ohm (watts) parameter settings based on the modulated signal. This example uses BPSK modulation, so the AWGN Channel block has the number of bits per symbol set to 1. The model includes a Power Meter block that measures the signal power at the AWGN input to confirm the setting required for the input signal power of the AWGN Channel block.
To compare simulation with theory, produce error rate results and a plot. The theoretical channel error probability for the coded signal is Q(sqrt(2*Ebc/N0)), where Q() is the standard Q function and Ebc/N0 is the coded Eb/N0 in linear units (not in dB). Compute the theoretical BER upper limit bound of a linear, rate 4/7 block code with a minimum distance of 3, and hard decision decoding for a BPSK-modulated signal in AWGN over a range of Eb/N0 values by using the function. Simulate the bercodingslex_hamming_check model over the same range of Eb/N0 values.
EbNoVec = 0:2:10; theorBER = bercoding(EbNoVec,'block','hard',7,4,3); berVecBSC = zeros(length(EbNoVec),3); for n = 1:length(EbNoVec) EbNo = EbNoVec(n); sim(model); berVecBSC(n,:) = berBSC(end,1); end
Plot the results by using the semilogy function to show the nearly identical results. The model has the Error Rate Calculation block configured to run each Eb/N0 point until 200 errors occur or the block receives 10^6 bits.
semilogy( ... EbNoVec,berVecBSC(:,1),'d', ... EbNoVec,theorBER,'-'); legend('BSC BER','Theoretical BER', ... Location="southwest"); xlabel("Eb/N0 (dB)"); ylabel("Error Probability"); title("Bit Error Probability"); grid on;

Ports
Input
Message to encode, specified as a column vector input signal containing K elements. The Message length K, or M-degree primitive polynomial parameter determines K. This port is unnamed on the block mask.
Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean
Output
Encoded message, returned as a binary column vector containing N elements and having the same data type as the input port, In. The Codeword length, N parameter determines N. This port is unnamed on the block mask.
Parameters
To edit block parameters interactively, use the Property Inspector. From the Simulink® Toolstrip, on the Simulation tab, in the Prepare gallery, select Property Inspector.
Codeword length, specified as a positive integer of the form N = 2M – 1, where M is an integer greater than or equal to 3. For more information, see Hamming Coding. The codeword length is also the output vector length.
Message length of the Hamming code or an M-degree primitive polynomial.
When this parameter is an integer, the value must equal (N – M), where N is the Codeword length, N parameter value and M is the degree of a primitive polynomial.
When this parameter is a vector, the vector must represent a primitive polynomial for GF(2M). For more information, see Hamming Coding.
Block Characteristics
Data Types |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
More About
The Hamming coding scheme uses elements of the finite field GF(2M), where M is an integer greater than or equal to 3. For
information about default primitive polynomials, see the gfprimdf function. You can rely on the default primitive polynomial or
specify the primitive polynomial that the algorithm should use.
To use the default primitive polynomial, enter N and K as the first and second parameters, respectively. K must equal (N – M). The algorithm uses
gfprimdf(M)as the primitive polynomial for GF(2M).To specify the primitive polynomial, enter N as the first parameter and a vector as the second parameter. N = 2M – 1. The vector must represent a primitive polynomial by listing its coefficients in order of ascending exponents. To create primitive polynomials in binary vector form, use the
gfprimfdfunction. You can also specify the polynomial as a character vector, for example,'D^3 + D^2 + 1'. For more information, see Representation of Polynomials in Communications Toolbox.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
Version History
Introduced before R2006a
See Also
Blocks
Functions
Topics
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)