Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
imAlessas committed Jul 7, 2024
2 parents 90b4797 + 388c25e commit ea49f25
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 200 deletions.
2 changes: 1 addition & 1 deletion src/analysis/cyclic_coding_analysis.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
cyclic_decoding_matrix = (cyclic_decoding_matrix (:, reorder))';

% Associates the syndrome to the bit.
% This vector has been calculated in the hamming_decoding function and
% This vector has been calculated in the get_associations function and
% copy-pasted here.
associations = [0 31 30 13 29 26 12 20 28 2 25 4 11 23 19 8 27 21 1 14 24 9 3 5 10 6 22 15 18 17 7 16];

Expand Down
25 changes: 4 additions & 21 deletions src/analysis/hamming_coding_analysis.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
% Generation of binary sequence

if ~GENERATE_NEW_SEQUENCE
if ~SIMULATION & ANALYSIS
if ~SIMULATION && ANALYSIS
fprintf("Error: Unable to use the current sequence because SIMULATION flag is disable.\n" + ...
"Try enabling it.\nA new sequence will be generated from scratch.\n\n\n")
N = 1e4; % number of bits to be sent
Expand Down Expand Up @@ -51,17 +51,7 @@

% Hamming encoding

% Create the matrix for the Hamming-encoding algorithm
binary_matrix = reshape(binary_sequence, k, N / k)';
show(DEBUG, binary_matrix);

% Hamming-encode the matrix
hamming_encoded_matrix = hamming_encoding(binary_matrix, codeword_length, k, generation_polynomial); % encode data
hamming_encoded_matrix = hamming_encoded_matrix';
show(DEBUG, hamming_encoded_matrix);

% Unwrap the matrix into a single row
hamming_encoded_sequence = hamming_encoded_matrix(:)';
hamming_encoded_sequence = hamming_encoding(binary_sequence, codeword_length, k, generation_polynomial);
show(DEBUG, hamming_encoded_sequence);

% Update the number of bits
Expand Down Expand Up @@ -117,15 +107,8 @@
BER_no_hamming(i) = errors_number_no_hamming / N;


% Reshape the sequence into a matrix, every row is a codeword
detected_sequence_matrix = reshape(detected_signal, codeword_length, N/codeword_length)';

% Perform Hamming decoding
decoded_data_matrix = hamming_decoding(detected_sequence_matrix, codeword_length, k, generation_polynomial); % encode data
decoded_data_matrix = decoded_data_matrix';

% Unwrap the matrix into a sequence
decoded_data_sequence = decoded_data_matrix(:)';
% Hamming decoding
decoded_data_sequence = hamming_decoding(detected_signal, codeword_length, k, generation_polynomial);

% Check number of erros
errors_number_with_hamming = sum (decoded_data_sequence ~= binary_sequence);
Expand Down
19 changes: 6 additions & 13 deletions src/analysis/spectrum_analysis.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
show(DEBUG, k_0);

% Define range of indexes for spectrum
k = k_0 + (-10 : 10);
K = k_0 + (-10 : 10);


% Phase value of the spectral function
phase = (k * OMEGA - omega_0) * tau / 2;
phase = (K * OMEGA - omega_0) * tau / 2;

C_BASK = sinc(phase / pi) * U / 4 * 1j; % fourirer series coefficient, BASK

% BPSK spectrum for periodocal '1' and '0' sequence (...1 0 1 0 1 0 1 0 1 0...)
C_BPSK = C_BASK .* ( exp(1j * k * OMEGA * tau / 2) - exp(- 1j * k * OMEGA * tau / 2));
C_BPSK = C_BASK .* ( exp(1j * K * OMEGA * tau / 2) - exp(- 1j * K * OMEGA * tau / 2));


if RESULT && PLOTS
% creates figure and settings
f = figure(2);
f.Name = 'Analysis of BPSK spectrum';
f.NumberTitle = 'off';
f.Position = [200, 120, 1200, 600];
f.Position = [450, 100, 700, 600];

% plot 1st result
stem( k * OMEGA / (2 * pi), abs(C_BPSK), 'b' ), grid on,
subplot(2, 1, 1), stem( K * OMEGA / (2 * pi), abs(C_BPSK), 'b' ), grid on,
xlabel('Frequency [GHz]'), ylabel('Amplitude, [V]'), title('Amplitude Spectrum of periodic signal')
ylim([-0.05, 0.35]);
end
Expand All @@ -51,14 +51,7 @@
G_BPSK = 1/ tau * abs(S_BASK) .^2;

if RESULT && PLOTS
% creates figure and settings
f = figure(3);
f.Name = 'Analysis of BPSK spectrum';
f.NumberTitle = 'off';
f.Position = [200, 120, 1200, 600];

% plot 2nd result
plot( omega / (2 * pi), G_BPSK, 'b' ), grid on,
subplot(2, 1, 2), plot( omega / (2 * pi), G_BPSK, 'b' ), grid on,
xlabel('Frequency [GHz]'), ylabel('PSD'), title('PSD of random signal')
ylim([-0.1e-8, 1.6e-8]);
end
38 changes: 18 additions & 20 deletions src/func/add_padding_bits.m
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
5 changes: 1 addition & 4 deletions src/func/deinterleaving.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function unmixed_sequence = deinterleaving(mixed_sequence)
function unmixed_sequence = deinterleaving(mixed_sequence, column_length)
% DEINTERLEAVING rearranges an interleaved sequence into its original form.
%
% INPUT:
Expand All @@ -7,9 +7,6 @@
% OUTPUT:
% unmixed_sequence: A vector representing the original sequence.

% Define the length of each column (also the number of rows)
column_length = 31;

% Calculate the number of columns (also the number of rows) based on the input sequence length
row_length = length(mixed_sequence) / column_length;

Expand Down
116 changes: 62 additions & 54 deletions src/func/hamming_decoding.m
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
31 changes: 17 additions & 14 deletions src/func/hamming_encoding.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
function encoded_data_matrix = hamming_encoding(binary_data_matrix, codeword_length, k, generation_polynomial)
% HAMMING_ENCODING performs Hamming encoding on a binary data matrix.
%
% INPUT:
% binary_data_matrix: Binary matrix containing k information symbols.
% codeword_length: Length of the codeword (total symbols per codeword).
% k: Number of actual information symbols.
% generation_polynomial: Generator polynomial for Hamming cyclic encoding.
%
% OUTPUT:
% encoded_data_matrix: Matrix containing the encoded data symbols.
function encoded_data = hamming_encoding(binary_data, codeword_length, k, generation_polynomial)
% HAMMING_ENCODING performs Hamming encoding on a binary data matrix.
%
% INPUT:
% binary_data_matrix: Binary matrix containing k information symbols.
% codeword_length: Length of the codeword (total symbols per codeword).
% k: Number of actual information symbols.
% generation_polynomial: Generator polynomial for Hamming cyclic encoding.
%
% OUTPUT:
% encoded_data_matrix: Matrix containing the encoded data symbols.

% Calculate the number of redundant symbols (parity symbols)
binary_data_matrix = reshape(binary_data, k, length(binary_data) / k)';

r = codeword_length - k;

% Generate the cyclic encoding matrix based on the generator polynomial
[~, cyclic_encoding_matrix] = cyclgen(codeword_length, generation_polynomial);

Expand All @@ -22,5 +23,7 @@

% Calculate control symbol values using matrix multiplication
% Use rem() function to find modulo 2 sum as a remainder of division by 2
encoded_data_matrix = rem(binary_data_matrix * cyclic_encoding_matrix, 2);
encoded_data_matrix = rem(binary_data_matrix * cyclic_encoding_matrix, 2)';

encoded_data = encoded_data_matrix(:)';
end
5 changes: 1 addition & 4 deletions src/func/interleaving.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function mixed_sequence = interleaving(unmixed_sequence)
function mixed_sequence = interleaving(unmixed_sequence, column_length)
% INTERLEAVING reorganizes a sequence by interleaving its elements into a matrix.
%
% INPUT:
Expand All @@ -7,9 +7,6 @@
% OUTPUT:
% mixed_sequence: A vector representing the interleaved sequence.

% Define the length of each column (also the number of rows)
column_length = 31;

% Calculate the number of length of each row (also the number of columns) based on the input sequence length
row_length = length(unmixed_sequence) / column_length;

Expand Down
Loading

0 comments on commit ea49f25

Please sign in to comment.