-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
190 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,43 @@ | ||
function padded_sequence = add_padding_bits(compact_sequence) | ||
% ADD_PADDING_BITS adds padding bits to the end of a compact sequence to meet interleaving requirements. | ||
function padded_sequence = add_padding_bits(compact_sequence, k, r) | ||
% ADD_PADDING_BITS Adds padding bits to the end of a compact sequence to meet interleaving requirements. | ||
% | ||
% INPUT: | ||
% compact_sequence: A vector representing the compact sequence. | ||
% compact_sequence: A vector representing the compact sequence. | ||
% k: An integer representing the divisor used in determining the number of padding bits. | ||
% r: An integer representing the number of bits reserved for storing the padding information. | ||
% | ||
% OUTPUT: | ||
% padded_sequence: A vector representing the sequence with added padding bits. | ||
% padded_sequence: A vector representing the sequence with added padding bits. | ||
|
||
% Determine the number of bits needed for padding | ||
bits_to_pad = count_padding_bits(compact_sequence); | ||
bits_to_pad = count_padding_bits(compact_sequence, k, r); | ||
|
||
% Convert the number of bits to pad to binary representation | ||
binary_padding_value = str2num(dec2bin(bits_to_pad, 5)')'; | ||
binary_padding_value = str2num(dec2bin(bits_to_pad, r)')'; | ||
|
||
% Add padding bits to the end of the sequence | ||
padded_sequence = [compact_sequence, zeros(1, bits_to_pad - 5), binary_padding_value]; | ||
padded_sequence = [compact_sequence, zeros(1, bits_to_pad - r), binary_padding_value]; | ||
end | ||
|
||
|
||
|
||
|
||
function number_of_padding_bits = count_padding_bits(compact_sequence) | ||
% COUNT_PADDING_BITS calculates the number of padding bits needed for interleaving. | ||
function number_of_padding_bits = count_padding_bits(compact_sequence, k, r) | ||
% COUNT_PADDING_BITS Calculates the number of padding bits needed for interleaving. | ||
% | ||
% INPUT: | ||
% compact_sequence: A vector representing the compact sequence. | ||
% compact_sequence: A vector representing the compact sequence. | ||
% k: An integer representing the divisor used in determining the number of padding bits. | ||
% r: An integer representing the number of bits reserved for storing the padding information. | ||
% | ||
% OUTPUT: | ||
% number_of_padding_bits: The number of padding bits needed. | ||
|
||
% Define the number of bits reserved for storing the padding information | ||
storage_bits = 5; | ||
|
||
% Define the divisor used in determining the number of padding bits | ||
divisor = 26; | ||
% number_of_padding_bits: The number of padding bits needed. | ||
|
||
% Calculate the number of padding bits required | ||
number_of_padding_bits = divisor - rem(length(compact_sequence), divisor); | ||
number_of_padding_bits = k - rem(length(compact_sequence), k); | ||
|
||
% Adjust the number of padding bits if it is less than the storage_bits | ||
if number_of_padding_bits < storage_bits | ||
number_of_padding_bits = number_of_padding_bits + divisor; | ||
if number_of_padding_bits < r | ||
number_of_padding_bits = number_of_padding_bits + k; | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,81 @@ | ||
function decoded_data_matrix = hamming_decoding(encoded_data_matrix, codeword_length, k, generation_polynomial) | ||
% HAMMING_DECODING performs decoding of Hamming group (m, k) codewords specified in | ||
% matrix form for multiple codewords. | ||
% | ||
% INPUT: | ||
% encoded_data_matrix: Matrix containing encoded Hamming codewords. | ||
% codeword_length: Length of the codeword (total symbols per codeword). | ||
% k: Number of actual information symbols. | ||
% generation_polynomial: Generator polynomial for Hamming encoding. | ||
% | ||
% OUTPUT: | ||
% decoded_data: Matrix containing decoded and corrected information blocks. | ||
function decoded_data = hamming_decoding(encoded_data, codeword_length, k, generation_polynomial) | ||
% HAMMING_DECODING performs decoding of Hamming (m, k) codewords specified in | ||
% matrix form for multiple codewords. | ||
% | ||
% INPUT: | ||
% encoded_data: Vector containing encoded Hamming codewords. | ||
% codeword_length: Length of the codeword (total symbols per codeword). | ||
% k: Number of actual information symbols. | ||
% generation_polynomial: Generator polynomial for Hamming encoding. | ||
% | ||
% OUTPUT: | ||
% decoded_data: Vector containing decoded and corrected information blocks. | ||
|
||
% Determine the number of control symbols | ||
% Reshape the encoded data into a matrix where each row is a codeword | ||
encoded_data_matrix = reshape(encoded_data, codeword_length, length(encoded_data) / codeword_length)'; | ||
|
||
% Calculate the number of control symbols (parity bits) | ||
r = codeword_length - k; | ||
|
||
% Specify syndrome calculation matrix | ||
% Generate the syndrome calculation matrix | ||
[~, cyclic_encoding_matrix] = cyclgen(codeword_length, generation_polynomial); | ||
syndrome_matrix = cyclic_encoding_matrix(:, (1:r)); | ||
syndrome_matrix = [syndrome_matrix; eye(r)]; | ||
|
||
% Calculate syndrome for each codeword | ||
% Calculate the syndrome for each codeword | ||
syndrome_value = rem(encoded_data_matrix * syndrome_matrix, 2); | ||
syndrome_value = syndrome_value * 2.^(r - 1 : -1 : 0)'; | ||
|
||
% Calculate error indexes based on syndrome values | ||
error_indexes = get_error_indexes(syndrome_matrix, syndrome_value, codeword_length); | ||
|
||
% Define error vector table | ||
% Get the associations vector (syndrome values to error positions mapping) | ||
associations = get_associations(syndrome_matrix, codeword_length); | ||
|
||
% Map the syndrome value to error position | ||
correction_index = [0, associations(:, 1)']; | ||
error_indexes = correction_index(syndrome_value + 1); | ||
|
||
% Define the error vector table | ||
error_vector = [zeros(1, codeword_length); | ||
eye(codeword_length)]; | ||
|
||
% Perform correction by adding the error vector to the received codeword | ||
% Correct the errors in the received codewords | ||
codeword = rem(encoded_data_matrix + error_vector(error_indexes + 1, :), 2); | ||
|
||
% Read the columns of data symbols to form decoded data | ||
decoded_data_matrix = codeword(:, 1:k); | ||
% Extract the information symbols from the corrected codewords | ||
decoded_data_matrix = codeword(:, 1:k)'; | ||
|
||
% Reshape the decoded data matrix back to a vector | ||
decoded_data = decoded_data_matrix(:)'; | ||
end | ||
|
||
|
||
|
||
|
||
function error_indexes = get_error_indexes(syndrome_matrix, syndrome_value, codeword_length) | ||
% CALCULATE_ERROR_INDEXES calculates the error indexes based on syndrome values. | ||
% | ||
% INPUT: | ||
% syndrome_matrix: Matrix containing syndrome patterns. | ||
% syndrome_value: Syndrome values for each codeword. | ||
% codeword_length: Length of the codeword. | ||
% | ||
% OUTPUT: | ||
% error_indexes: Vector containing error indexes. | ||
|
||
% Initialize vector to store decimal values of binary syndrome patterns | ||
syndrome_decimal_value_vector = []; | ||
|
||
% Convert binary syndrome values to decimal for association | ||
for i = 1 : codeword_length | ||
syndrome_decimal_value_vector = [syndrome_decimal_value_vector; bin2dec(num2str(syndrome_matrix(i, :)))]; | ||
function associations = get_associations(syndrome_matrix, codeword_length) | ||
% GET_ASSOCIATIONS generates the association vector mapping syndrome values to error positions. | ||
% | ||
% INPUT: | ||
% syndrome_matrix: Matrix used for syndrome calculation. | ||
% codeword_length: Length of the codeword (total symbols per codeword). | ||
% | ||
% OUTPUT: | ||
% associations: Matrix mapping positions to syndrome values, sorted by syndrome value. | ||
|
||
% Persistent variable to store the associations across function calls | ||
persistent cached_associations; | ||
|
||
% Check if associations are already calculated | ||
if isempty(cached_associations) | ||
% Initialize positions and syndrome decimal value vector | ||
positions = (1 : codeword_length)'; | ||
syndrome_decimal_value_vector = []; | ||
|
||
% Calculate syndrome decimal values for each position | ||
for i = 1 : codeword_length | ||
syndrome_decimal_value_vector = [syndrome_decimal_value_vector; bin2dec(num2str(syndrome_matrix(i, :)))]; | ||
end | ||
|
||
% Create the associations matrix and sort by syndrome value | ||
cached_associations = [positions, syndrome_decimal_value_vector]; | ||
cached_associations = sortrows(cached_associations, 2); | ||
end | ||
|
||
% Combine positions and corresponding decimal values for sorting | ||
associations = [transpose(1:codeword_length), syndrome_decimal_value_vector]; | ||
|
||
% Sort associations based on decimal values for efficient error detection | ||
associations = sortrows(associations, 2); | ||
|
||
% Create correction index for mapping syndrome values to error positions | ||
correction_index = [0, associations(:, 1)']; | ||
|
||
% Map syndrome values to error positions using correction index | ||
error_indexes = correction_index(syndrome_value + 1); | ||
|
||
% Return the cached associations | ||
associations = cached_associations; | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.