diff --git a/src/analysis/cyclic_coding_analysis.m b/src/analysis/cyclic_coding_analysis.m index 0d53769..b6bf285 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 8db8db7..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 @@ -30,10 +30,10 @@ 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 @@ -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 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/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/hamming_decoding.m b/src/func/hamming_decoding.m index 8827e79..9c6d425 100644 --- a/src/func/hamming_decoding.m +++ b/src/func/hamming_decoding.m @@ -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 diff --git a/src/func/hamming_encoding.m b/src/func/hamming_encoding.m index dd9c82f..ad6c94a 100644 --- a/src/func/hamming_encoding.m +++ b/src/func/hamming_encoding.m @@ -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); @@ -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/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/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_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); 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); 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"); diff --git a/src/main.m b/src/main.m index 5bf6cbd..e7b0724 100644 --- a/src/main.m +++ b/src/main.m @@ -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");