Digital Image Corrupted by AWGN

Request help in solving error for the code below:
Assignment is to write MATLAB code for (1) Simulate transmission of the lenna image (attached) using BPSK. For this purpose, the pixel values have to be converted to bits, then modulated and transmitted and finally detected. Do this experiment at a low SNR Eb/No of 0 dB and also at a high SNR of 4 dB. Print the images. Compare and comment.
(2) Employ a linear error detection code (only detection and no correction) and transmit using BPSK again. The code can be your choice, and you can pick any code from the book but should not be too trivial, say a (2,1) code or a repetition code. Consider first 0 dB SNR. In this case, assume that when a packet is received in error, the detector makes a retransmission request. Count the number of re-transmission requests at 0 dB, 2 dB, 4 dB, 6 dB, 8 dB and 10 dB SNR respectively until there is no error. Plot the number of re-transmission requests against the SNR values.
(3) Use an error correction code (of your choice) using syndrome look up table and correct errors directly at the receiver without sending any retransmission request. The code cannot be a too simple code, for example, (3,1) or (4,2) etc. Show images at 0 dB and 4 dB SNR after employing error correction. Give comments comparing results with no error correction and results with error correction.
% Task 1: BPSK Transmission of lenna.pgm
% Load lenna image
lenna = imread('lenna.pgm');
% Convert image to binary
lenna_bin = reshape(dec2bin(lenna(:), 8).' - '0', 1, [])';
% BPSK modulation
lenna_modulated = 2*lenna_bin - 1;
% Eb/No values
EbNo_low = 0; % Low SNR
EbNo_high = 4; % High SNR
% Transmission at low SNR
SNR_low = 10^(EbNo_low/10);
lenna_received_low = awgn(lenna_modulated, SNR_low);
% Transmission at high SNR
SNR_high = 10^(EbNo_high/10);
lenna_received_high = awgn(lenna_modulated, SNR_high);
% Convert received signal back to image
lenna_received_low_img = reshape((sign(lenna_received_low) + 1)/2, size(lenna));
lenna_received_high_img = reshape((sign(lenna_received_high) + 1)/2, size(lenna));
% Display original and received images
figure;
subplot(2,2,1), imshow(lenna), title('Original Image');
subplot(2,2,2), imshow(lenna_received_low_img), title('Received at Low SNR (0 dB)');
subplot(2,2,3), imshow(lenna_received_high_img), title('Received at High SNR (4 dB)');
% Task 2: Linear error detection code with re-transmission
% Define linear error detection code
% Let's use a simple parity check code
parity_check_matrix = [1 1 1 1 1]; % Parity check matrix
num_bits = length(parity_check_matrix);
% Initialize variables
SNR_values = 0:2:10;
num_retransmissions = zeros(size(SNR_values));
% Loop over SNR values
for i = 1:length(SNR_values)
SNR = 10^(SNR_values(i)/10);
lenna_received = awgn(lenna_modulated, SNR);
% Decode received bits using parity check
decoded_bits = mod(sum(reshape(lenna_received, num_bits, [])) + 1, 2);
% Count number of re-transmissions
num_retransmissions(i) = sum(~all(decoded_bits == 0));
end
% Plot number of re-transmissions vs SNR
figure;
plot(SNR_values, num_retransmissions, '-o');
xlabel('Eb/No (dB)');
ylabel('Number of Re-transmissions');
title('Number of Re-transmissions vs SNR');
% Task 3: Error correction code with syndrome lookup table
% For error correction, let's use Hamming(7,4) code
% Define generator matrix and parity check matrix
G = [1 1 0 1; 1 0 1 1; 1 0 0 0; 0 1 1 1; 0 1 0 0; 0 0 1 0; 0 0 0 1];
H = [eye(3), transpose(G(:,5:end))];
% Generate syndrome lookup table
syndrome_table = zeros(1, 8);
for i = 1:size(H,1)
syndrome = mod(H(i,:) * G', 2);
syndrome_decimal = bi2de(syndrome, 'left-msb');
syndrome_table(syndrome_decimal + 1) = i;
end
% Initialize variables
num_errors_corrected = zeros(size(SNR_values));
% Loop over SNR values
for i = 1:length(SNR_values)
SNR = 10^(SNR_values(i)/10);
lenna_received = awgn(lenna_modulated, SNR);
% Decode received bits using syndrome lookup table
received_matrix = reshape(lenna_received, 7, []);
decoded_bits = zeros(4, size(received_matrix, 2));
for j = 1:size(received_matrix, 2)
syndrome = mod(received_matrix(:,j)' * H', 2);
syndrome_decimal = bi2de(syndrome, 'left-msb');
if syndrome_decimal ~= 0 % Error detected
error_position = syndrome_table(syndrome_decimal);
received_matrix(error_position, j) = ~received_matrix(error_position, j); % Correct error
end
decoded_bits(:,j) = received_matrix([3 5 6 7], j);
end
% Count number of errors corrected
num_errors_corrected(i) = sum(sum(decoded_bits ~= lenna_bin(1:4,:)));
end
% Convert corrected bits back to image
lenna_corrected_img = reshape((sign(decoded_bits - 0.5) + 1)/2, size(lenna));
% Display corrected images
figure;
subplot(1,2,1), imshow(lenna_received_low_img), title('Received at Low SNR (0 dB)');
subplot(1,2,2), imshow(lenna_corrected_img), title('Corrected Image');
% Comments
disp("Comments:");
disp("Task 1: BPSK transmission introduces noise, resulting in degraded image quality at lower SNR.");
disp("Task 2: Using a linear error detection code helps reduce the number of re-transmissions at higher SNR levels.");
disp("Task 3: Error correction code significantly improves the image quality compared to no error correction, especially at lower SNR levels.");

4 comentarios

John D'Errico
John D'Errico el 15 de Abr. de 2024
Editada: John D'Errico el 15 de Abr. de 2024
What error? If you have an error in the code, show what it is. ENTIRELY everything in red.
As far as this not being spam, it is clearly a homework assignment, the same thing given hundreds if not thousands of times before. The Lena image has been used for as long as I can remember. That goes back at least 35 years to when I saw it first, and I am sure it goes back further than that. So, why should we believe you have not just copied a question from someone else, and reposted it to gain rep on the site, something spammers do regularly? So the jury is still out on this being spam. If it is not, then convince us otherwise. You might do that by telling us what is the problem. Show the error message you get. Show that you actually care about getting your question resolved.
James Manns
James Manns el 16 de Abr. de 2024
The error message is as follows:
"Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
Error in Computerassignment5b (line 28)
received_signal_low = modulated_bits + noise_low;"
Unfortunately, I am fairly new to MATLAB and this forum so although this may be a common assignment, it is new to me. I have read through some tutorials and asked around for help and realize that this product is not as intuitive to me as it is to others. I assume from the tone of your response that you must deal with a lot of spam but I'm just looking for some assistance. If I am not properly posting my questions, any direction on how to correct woudl be graatly appreciated.
James Manns
James Manns el 16 de Abr. de 2024
There is a different that error that wasn't showing up before:
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the
appropriate size for that dimension.
Error in Computerassignment5 (line 18)
lenna_received_low_img = reshape((sign(lenna_received_low) + 1)/2, size(lenna));
received_signal_low = modulated_bits + noise_low;
That line of code does not occur in what was posted. Furthermore, modulated_bits and noise_low are both not defined in the code that was posted.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Abr. de 2024
lenna = imread('lenna.pgm');
lenna_bin = reshape(dec2bin(lenna(:), 8).' - '0', 1, [])';
lenna_bin reflects the size of lenna in bits
lenna_modulated = 2*lenna_bin - 1;
lenna_modulated is the same size as lenna_bin
lenna_received_low = awgn(lenna_modulated, SNR_low);
lenna_received_low is the same size as lenna_modulated which is the same size as lenna_bin
lenna_received_low_img = reshape((sign(lenna_received_low) + 1)/2, size(lenna));
(sign(lenna_received_low) + 1)/2 is the same size as lenna_received_low which is the same size lenna_modulated which is the same size as lenna_bin
lenna_bin is the size of lenna in bits
You are trying to reshape something the size of lenna in bits to be the same size as lenna in pixels

Más respuestas (0)

Productos

Versión

R2023b

Etiquetas

Preguntada:

el 15 de Abr. de 2024

Respondida:

el 17 de Abr. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by