From 8918a448344c1111d0852c26b8423d8c25c06b95 Mon Sep 17 00:00:00 2001 From: imAlessas Date: Sun, 7 Jul 2024 16:39:14 +0200 Subject: [PATCH 1/5] Update padding_bits function and testing The function is generic and can hadle different length of codeword. --- src/func/add_padding_bits.m | 38 +++++++++++++++---------------- src/func/remove_padding_bits.m | 26 ++++++++++++--------- src/func/test/test_padding_bits.m | 26 ++++++++++++++------- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/func/add_padding_bits.m b/src/func/add_padding_bits.m index 690fc11..b3910b2 100644 --- a/src/func/add_padding_bits.m +++ b/src/func/add_padding_bits.m @@ -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 diff --git a/src/func/remove_padding_bits.m b/src/func/remove_padding_bits.m index 6b49280..a6505e0 100644 --- a/src/func/remove_padding_bits.m +++ b/src/func/remove_padding_bits.m @@ -1,31 +1,35 @@ -function compact_sequence = remove_padding_bits(padded_sequence) -% REMOVE_PADDING_BITS removes padding bits from the end of a padded sequence. +function compact_sequence = remove_padding_bits(padded_sequence, r) +% REMOVE_PADDING_BITS Removes padding bits from the end of a padded sequence. % % INPUT: -% padded_sequence: A vector representing the sequence with padding bits. +% padded_sequence: A vector representing the sequence with padding bits. +% r: An integer representing the number of bits reserved for storing the padding information. % % OUTPUT: -% compact_sequence: A vector representing the sequence with padding bits removed. +% compact_sequence: A vector representing the sequence with padding bits removed. % Call the get_padding_bits function to determine the number of padding bits - padding = get_padding_bits(padded_sequence); + padding = get_padding_bits(padded_sequence, r); % Remove the padding bits from the end of the sequence compact_sequence = padded_sequence(1 : end - padding); end -function padding_bits = get_padding_bits(padded_sequence) -% GET_PADDING_BITS extracts the binary representation of the padding bits from the end of a sequence. + + +function padding_bits = get_padding_bits(padded_sequence, r) +% GET_PADDING_BITS Extracts the binary representation of the padding bits from the end of a sequence. % % INPUT: -% padded_sequence: A vector representing the sequence with padding bits. +% padded_sequence: A vector representing the sequence with padding bits. +% r: An integer representing the number of bits reserved for storing the padding information. % % OUTPUT: -% padding_bits: The decimal value representing the extracted padding bits. +% padding_bits: The decimal value representing the extracted padding bits. - % Extract the last 5 bits from the padded sequence - bits = padded_sequence(end-4 : end); + % Extract the last r bits from the padded sequence + bits = padded_sequence(end - r + 1 : end); % Convert the binary representation of the bits to a string str = ''; diff --git a/src/func/test/test_padding_bits.m b/src/func/test/test_padding_bits.m index 9a5ce12..c5a4771 100644 --- a/src/func/test/test_padding_bits.m +++ b/src/func/test/test_padding_bits.m @@ -1,31 +1,41 @@ -clc, clear, format compact +clc; clear; format compact +% Import functions from the specified package or directory import func.* % Number of tests to perform number_of_tests = 100; % Initialize result array to store test outcomes -result = 1 : number_of_tests; +result = zeros(1, number_of_tests); % Record the start time for the test duration calculation tic; % Loop through each test -for i = 1 : length(result) +for i = 1 : number_of_tests fprintf('Test #%i', i); + % Generate a random codeword length between 15 and 115 (inclusive) + codeword_length = 15 + randi(1e2, 1); + + % Calculate the number of bits needed to store the padding information + r = ceil(log2(codeword_length + 1)); + + % Calculate the length of the compact sequence without padding bits + k = codeword_length - r; + % Generate a random length for the symbol sequence N = randi(1e8, 1); - % Generate a random binary symbol sequence - symbol_sequence = randi(2, 1, N) - 1; + % Generate a random binary symbol sequence of length N + symbol_sequence = randi([0, 1], 1, N); % Add padding bits to the symbol sequence - padded_sequence = add_padding_bits(symbol_sequence); + padded_sequence = add_padding_bits(symbol_sequence, k, r); % Remove padding bits from the padded sequence - compact_sequence = remove_padding_bits(padded_sequence); + compact_sequence = remove_padding_bits(padded_sequence, r); % Compare the original symbol sequence with the compacted sequence result(i) = sum(symbol_sequence ~= compact_sequence); @@ -55,4 +65,4 @@ end % Display the total duration of the test -disp("Test duration: " + test_duration); +disp("Test duration: " + test_duration + " seconds"); From 4b3cdded5aa6bf81e215fd15f0bf16f5a58aefc2 Mon Sep 17 00:00:00 2001 From: imAlessas Date: Sun, 7 Jul 2024 17:30:29 +0200 Subject: [PATCH 2/5] Update Cyclic Hamming Encoding Compacted the matrix operation inside the function and improved time consuming associations calculation --- src/func/hamming_decoding.m | 76 +++++++++++++++++------- src/func/hamming_encoding.m | 11 ++-- src/func/test/test_cyclic_hamming_code.m | 26 ++++---- 3 files changed, 72 insertions(+), 41 deletions(-) diff --git a/src/func/hamming_decoding.m b/src/func/hamming_decoding.m index 9f5613e..9c6d425 100644 --- a/src/func/hamming_decoding.m +++ b/src/func/hamming_decoding.m @@ -1,49 +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 +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_matrix: Matrix containing encoded Hamming codewords. + % 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: Matrix containing decoded and corrected information blocks. - - % Determine the number of control symbols + % decoded_data: Vector containing decoded and corrected information blocks. + + % 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)'; - % Define associations between syndrome values and error positions - positions = (1 : codeword_length)'; - syndrome_decimal_value_vector = []; - - for i = 1 : codeword_length - syndrome_decimal_value_vector = [syndrome_decimal_value_vector; bin2dec(num2str(syndrome_matrix(i, :)))]; - end - - associations = [positions, syndrome_decimal_value_vector]; - associations = sortrows(associations, 2); + % 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 error vector table + % 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 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 + + % Return the cached associations + associations = cached_associations; end diff --git a/src/func/hamming_encoding.m b/src/func/hamming_encoding.m index c9775bf..ad6c94a 100644 --- a/src/func/hamming_encoding.m +++ b/src/func/hamming_encoding.m @@ -1,4 +1,4 @@ -function encoded_data_matrix = hamming_encoding(binary_data_matrix, codeword_length, k, generation_polynomial) +function encoded_data = hamming_encoding(binary_data, codeword_length, k, generation_polynomial) % HAMMING_ENCODING performs Hamming encoding on a binary data matrix. % % INPUT: @@ -10,9 +10,10 @@ % 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); @@ -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 diff --git a/src/func/test/test_cyclic_hamming_code.m b/src/func/test/test_cyclic_hamming_code.m index f1c5421..f57c63c 100644 --- a/src/func/test/test_cyclic_hamming_code.m +++ b/src/func/test/test_cyclic_hamming_code.m @@ -26,28 +26,24 @@ fprintf('Test #%i', i); % Generate a random number N for the length of the symbol sequence - N = k * randi(1e5, 1); + N = k * randi(1e6, 1); % Generate a random binary symbol sequence - symbol_sequence = randi(2, 1, N) - 1; + symbol_sequence = randi([0, 1], 1, N); - % Reshape the symbol sequence into a matrix - symbol_sequence_matrix = reshape(symbol_sequence, k, N/k)'; + % Perform Hamming encoding on the symbol sequence + encoded_sequence = hamming_encoding(symbol_sequence, codeword_length, k, generation_polynomial); - % Perform Hamming encoding on the symbol sequence matrix - encoded_sequence_matrix = hamming_encoding(symbol_sequence_matrix, codeword_length, k, generation_polynomial); - - % Introduce random errors in the encoded sequence matrix - for j = 1 : length(encoded_sequence_matrix(:,1)) + % Introduce random errors in the encoded sequence + num_codewords = length(encoded_sequence) / codeword_length; + for j = 1 : num_codewords error_position = randi(codeword_length, 1); - encoded_sequence_matrix(j, error_position) = ~encoded_sequence_matrix(j, error_position); + encoded_sequence((j-1) * codeword_length + error_position) = ... + ~encoded_sequence((j-1) * codeword_length + error_position); end - % Perform Hamming decoding on the encoded sequence matrix - decoded_sequence_matrix = hamming_decoding(encoded_sequence_matrix, codeword_length, k, generation_polynomial); - - % Reshape the decoded sequence matrix into a vector - decoded_sequence = reshape(decoded_sequence_matrix', 1, []); + % Perform Hamming decoding on the encoded sequence + decoded_sequence = hamming_decoding(encoded_sequence, codeword_length, k, generation_polynomial); % Compare the original symbol sequence with the decoded sequence result(i) = sum(symbol_sequence ~= decoded_sequence); From 8be80b6851fda25eeb9eba4aeaead0022880443c Mon Sep 17 00:00:00 2001 From: imAlessas Date: Sun, 7 Jul 2024 17:35:16 +0200 Subject: [PATCH 3/5] Update Interleaving made the function more generic, can now handle various length of codeword and now just length 31 --- src/func/deinterleaving.m | 5 +---- src/func/interleaving.m | 5 +---- src/func/test/test_interleaving.m | 9 ++++++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/func/deinterleaving.m b/src/func/deinterleaving.m index 18931e8..8ceca6b 100644 --- a/src/func/deinterleaving.m +++ b/src/func/deinterleaving.m @@ -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: @@ -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; diff --git a/src/func/interleaving.m b/src/func/interleaving.m index 2e4334c..dcf77ac 100644 --- a/src/func/interleaving.m +++ b/src/func/interleaving.m @@ -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: @@ -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; diff --git a/src/func/test/test_interleaving.m b/src/func/test/test_interleaving.m index 0de9814..55ec576 100644 --- a/src/func/test/test_interleaving.m +++ b/src/func/test/test_interleaving.m @@ -15,17 +15,20 @@ for i = 1 : length(result) fprintf('Test #%i', i); + % Generate a random number for the length of the column + column_length = randi(1e3, 1); + % Generate a random number N for the length of the symbol sequence - N = 31 * randi(1e6, 1); + N = column_length * randi(1e5, 1); % Generate a random binary symbol sequence symbol_sequence = randi(2, 1, N) - 1; % Apply interleaving to the symbol sequence - mixed_sequence = interleaving(symbol_sequence); + mixed_sequence = interleaving(symbol_sequence, column_length); % Apply deinterleaving to the interleaved sequence - unmixed_sequence = deinterleaving(mixed_sequence); + unmixed_sequence = deinterleaving(mixed_sequence, column_length); % Count the number of errors between the original and deinterleaved sequences result(i) = sum(symbol_sequence ~= unmixed_sequence); From e76feea83555516606863bf477f3bb97c2bccaf1 Mon Sep 17 00:00:00 2001 From: imAlessas Date: Sun, 7 Jul 2024 18:35:32 +0200 Subject: [PATCH 4/5] Minor changes on analysis/ - improved Hamming encoding function usage - refactored k into K in spectrum analyssi due to execution conflicts --- src/analysis/cyclic_coding_analysis.m | 2 +- src/analysis/hamming_coding_analysis.m | 25 ++++--------------------- src/analysis/spectrum_analysis.m | 8 ++++---- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/analysis/cyclic_coding_analysis.m b/src/analysis/cyclic_coding_analysis.m index ed703a1..adaec53 100644 --- a/src/analysis/cyclic_coding_analysis.m +++ b/src/analysis/cyclic_coding_analysis.m @@ -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]; diff --git a/src/analysis/hamming_coding_analysis.m b/src/analysis/hamming_coding_analysis.m index 09de409..1484d10 100644 --- a/src/analysis/hamming_coding_analysis.m +++ b/src/analysis/hamming_coding_analysis.m @@ -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 @@ -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 @@ -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); diff --git a/src/analysis/spectrum_analysis.m b/src/analysis/spectrum_analysis.m index 524eefb..9bcd158 100644 --- a/src/analysis/spectrum_analysis.m +++ b/src/analysis/spectrum_analysis.m @@ -13,16 +13,16 @@ 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 @@ -33,7 +33,7 @@ f.Position = [450, 100, 700, 600]; % plot 1st result - subplot(2, 1, 1), 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 From 388c25e9510fb13b6225e7645965ac447640eed1 Mon Sep 17 00:00:00 2001 From: imAlessas Date: Sun, 7 Jul 2024 18:35:50 +0200 Subject: [PATCH 5/5] Update main.m --- src/main.m | 66 ++++++++++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/src/main.m b/src/main.m index 40d17bc..e7b0724 100644 --- a/src/main.m +++ b/src/main.m @@ -13,8 +13,8 @@ % RESULT: Display values as per guidelines % DEBUG: Additional display for variables or comparisons % PLOTS: Toggle for generating plots -SIMULATION = 0; % Set to 1 to compute the simulation -ANALYSIS = 1; % Set to 1 to enable the analysis +SIMULATION = 1; % Set to 1 to compute the simulation +ANALYSIS = 0; % Set to 1 to enable the analysis RESULT = 1; % Set to 1 to display result values DEBUG = 0; % Set to 1 to enable additional debugging display PLOTS = 1; % Set to 1 to generate plots, 0 to disable plots @@ -66,6 +66,7 @@ alphabet_matrix = [alphabet; probability_vector]; show(DEBUG, alphabet_matrix); + disp(" - Creating sequence"); initial_symbol_sequence = symbol_sequence_generator(alphabet_matrix, transmitted_symbol_number); show(DEBUG, initial_symbol_sequence); end @@ -84,10 +85,12 @@ % Perform Shannon-Fano Source Encoding if SIMULATION + disp(" - Performing Shannon-Fano encoding"); shannon_fano_encoded_sequence = shannon_fano_encoding(initial_symbol_sequence); show(DEBUG, shannon_fano_encoded_sequence); - padded_sequence = add_padding_bits(shannon_fano_encoded_sequence); + disp(" - Adding padding bits"); + padded_sequence = add_padding_bits(shannon_fano_encoded_sequence, k, r); show(DEBUG, padded_sequence); end @@ -137,33 +140,27 @@ % Perform Cyclic-Hamming channel coding if SIMULATION - % Wrap into a matrix - padded_matrix = reshape(padded_sequence, k, length(padded_sequence) / k)'; - - hamming_encoded_matrix = hamming_encoding(padded_matrix, codeword_length, k, generation_polynomial); - hamming_encoded_matrix = hamming_encoded_matrix'; - - % unwrap matrix - hamming_encoded_sequence = hamming_encoded_matrix(:)'; - % show(DEBUG, hamming_encoded_sequence); + disp(" - Performing Hamming-Encoding"); + hamming_encoded_sequence = hamming_encoding(padded_sequence, codeword_length, k, generation_polynomial); + show(DEBUG, hamming_encoded_sequence); % = = = = = = = = = = = = = = = = = = = = % Perform interleaving - - interleaved_sequence = interleaving(hamming_encoded_sequence); - % show(DEBUG, interleaved_sequence) + disp(" - Performing interleaving"); + interleaved_sequence = interleaving(hamming_encoded_sequence, codeword_length); + show(DEBUG, interleaved_sequence) % = = = = = = = = = = = = = = = = = = = = % Perform scrambling - + disp(" - Performing scrambling"); scrambled_sequence = scrambling(interleaved_sequence, scrambler_key); - % show(DEBUG, scrambled_sequence); + show(DEBUG, scrambled_sequence); @@ -171,6 +168,8 @@ % = = = = = = = = = = = = = = = = = = = = % Perform modulation + disp(" - Performing BPSK modulation"); + % Define the time-step delta_t = tau / samples_per_symbol; @@ -212,6 +211,9 @@ % Perform Gaussion White Noise addition if SIMULATION + + disp(" - Adding GWN"); + % Reversed SNR formula EbN0 = 10^(SNR / 10); @@ -231,6 +233,8 @@ % = = = = = = = = = = = = = = = = = = = = % Perform detection + disp(" - Performing BPSK demodulation"); + % Slice recieved signal into segments in each column sliced_disturbed_signal = reshape(disturbed_signal, samples_per_symbol, N); @@ -243,8 +247,8 @@ % = = = = = = = = = = = = = = = = = = = = % Perform descrambling + disp(" - Performing descrambling"); descrambled_sequence = descrambling(detected_signal, scrambler_key); - show(DEBUG, sum(descrambled_sequence ~= interleaved_sequence), "Descrambling" ) @@ -252,13 +256,13 @@ % = = = = = = = = = = = = = = = = = = = = % Perform deinterleaving - deinterleaved_sequence = deinterleaving(descrambled_sequence); - + disp(" - Performing deinterleaving"); + deinterleaved_sequence = deinterleaving(descrambled_sequence, codeword_length); show(DEBUG, sum(deinterleaved_sequence ~= hamming_encoded_sequence), "Deinterleaving" ) errors_occurred = sum(deinterleaved_sequence ~= hamming_encoded_sequence); - show(RESULT, errors_occurred, "Errors occurred during the transmission:"); + fprintf(" Errors occurred: %d\n", errors_occurred); @@ -266,30 +270,23 @@ % = = = = = = = = = = = = = = = = = = = = % Perform Hamming-decoding and error correction - % Wraps the sequence into a matrix - deinterleaved_matrix = reshape(deinterleaved_sequence, codeword_length, length(deinterleaved_sequence) / codeword_length)'; - - hamming_decoded_matrix = hamming_decoding(deinterleaved_matrix, codeword_length, k, generation_polynomial); - hamming_decoded_matrix = hamming_decoded_matrix'; - - % Unwraps the matrix into a sequence - hamming_decoded_sequence = hamming_decoded_matrix(:)'; - + disp(" - Performing Hamming-decoding"); + hamming_decoded_sequence = hamming_decoding(deinterleaved_sequence, codeword_length, k, generation_polynomial); show(DEBUG, sum(hamming_decoded_sequence ~= padded_sequence), "Channel decoding" ) % = = = = = = = = = = = = = = = = = = = = % Remove padding bits - unpadded_sequence = remove_padding_bits(hamming_decoded_sequence); - + disp(" - Removing padding bits"); + unpadded_sequence = remove_padding_bits(hamming_decoded_sequence, r); show(DEBUG, sum(unpadded_sequence ~= shannon_fano_encoded_sequence), "Unpadding" ) % = = = = = = = = = = = = = = = = = = = = % Perform Shannon-Fano decoding + disp(" - Performing Shannon-Fano decoding"); shannon_fano_decoded_sequence = shannon_fano_decoding(unpadded_sequence); - show(DEBUG, sum(shannon_fano_decoded_sequence ~= initial_symbol_sequence), "Source decoding" ) % = = = = = = = = = = = = = = = = = = = = @@ -302,7 +299,8 @@ % Shows the actual errors between the initial and the final sequence errors_occurred = sum(shannon_fano_decoded_sequence ~= initial_symbol_sequence); - show(RESULT, errors_occurred, "Uncorrected errors:"); + fprintf("\nReal number of errors: %d\n", errors_occurred); end +fprintf("\n\nEnd of experiemnt.\n\n");