Error in AWGN Using Integers or scalar doubles

1 visualización (últimos 30 días)
James Manns
James Manns el 22 de Abr. de 2024
Respondida: Walter Roberson el 22 de Abr. de 2024
Need help correcting the following errors:
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
Error in awgn (line 157)
y = sig + noise;
Error in Computerassignmentfinal (line 20)
received_low = awgn(lenna_bits, SNR_low,'measured');
The code attached.
clc;
clear all;
% Load the lenna image
lenna = imread('lenna.png');
% Convert image to grayscale
lenna_gray = rgb2gray(lenna);
% Convert pixel values to bits
lenna_bits = reshape(de2bi(lenna_gray), [], 1);
% BPSK modulation
Eb_No_low = 0; % Low SNR
Eb_No_high = 4; % High SNR
SNR_low = 10^(Eb_No_low/10);
SNR_high = 10^(Eb_No_high/10);
% Transmit and receive at low SNR
received_low = awgn(lenna_bits, SNR_low,'measured');
% Demodulation
decoded_low = received_low < 0;
% Reshape decoded bits to original image size
decoded_image_low = reshape(decoded_low, size(lenna_gray));
% Plot original and received image at low SNR
figure;
subplot(1,2,1); imshow(lenna_gray); title('Original Image');
subplot(1,2,2); imshow(decoded_image_low); title('Received Image (0 dB SNR)');
% Transmit and receive at high SNR
received_high = awgn(lenna_bits, SNR_high, 'measured');
% Demodulation
decoded_high = received_high < 0;
% Reshape decoded bits to original image size
decoded_image_high = reshape(decoded_high, size(lenna_gray));
% Plot original and received image at high SNR
figure;
subplot(1,2,1); imshow(lenna_gray); title('Original Image');
subplot(1,2,2); imshow(decoded_image_high); title('Received Image (4 dB SNR)');
% Linear error detection code
% Example: Hamming (7,4) code
parityMatrix = [1 1 1 0 1 0 0; 1 1 0 1 0 1 0; 1 0 1 1 0 0 1];
generatorMatrix = [eye(4) parityMatrix'];
% Encode the data
encoded_data = mod(lenna_bits * generatorMatrix, 2);
% Add noise
received_data = awgn(encoded_data, Eb_No_low, 'measured');
% Syndrome lookup table for error detection
syndrome_table = syndtable(parityMatrix);
% Decoding with error detection
decoded_data = zeros(size(encoded_data));
errors = zeros(size(encoded_data, 1), 1);
for i = 1:size(encoded_data, 1)
syndrome = mod(received_data(i, :) * parityMatrix', 2);
if sum(syndrome) ~= 0 % Error detected
errors(i) = 1;
else
decoded_data(i, :) = received_data(i, :);
end
end
% Count number of retransmission requests at different SNRs
SNRs = [0, 2, 4, 6, 8, 10];
retransmissions = zeros(size(SNRs));
for i = 1:length(SNRs)
SNR = 10^(SNRs(i)/10);
received_data = awgn(encoded_data, SNR, 'measured');
errors = zeros(size(encoded_data, 1), 1);
for j = 1:size(encoded_data, 1)
syndrome = mod(received_data(j, :) * parityMatrix', 2);
if sum(syndrome) ~= 0 % Error detected
errors(j) = 1;
retransmissions(i) = retransmissions(i) + 1;
end
end
end
% Plot number of retransmissions against SNR values
figure;
plot(SNRs, retransmissions, '-o');
xlabel('SNR (dB)');
ylabel('Number of Retransmissions');
title('Number of Retransmissions vs SNR');
% Error correction code
% Example: Reed-Solomon code
n = 255;
k = 223;
t = 16;
rs_encoder = comm.RSEncoder(n, k);
rs_decoder = comm.RSDecoder(n, k);
% Encode data
encoded_rs = step(rs_encoder, double(lenna_bits));
% Add noise
received_rs_low = awgn(encoded_rs, Eb_No_low, 'measured');
received_rs_high = awgn(encoded_rs, Eb_No_high, 'measured');
% Decode received data
decoded_rs_low = step(rs_decoder, received_rs_low);
decoded_rs_high = step(rs_decoder, received_rs_high);
% Reshape decoded bits to original image size
decoded_image_rs_low = reshape(decoded_rs_low, size(lenna_gray));
decoded_image_rs_high = reshape(decoded_rs_high, size(lenna_gray));
% Plot original and received images with error correction
figure;
subplot(1,2,1); imshow(decoded_image_low); title('Received Image (0 dB SNR, No Error Correction)');
subplot(1,2,2); imshow(decoded_image_rs_low); title('Received Image (0 dB SNR, Error Correction)');
figure;
subplot(1,2,1); imshow(decoded_image_high); title('Received Image (4 dB SNR, No Error Correction)');
subplot(1,2,2); imshow(decoded_image_rs_high); title('Received Image (4 dB SNR, Error Correction)');
  2 comentarios
Pooja Kumari
Pooja Kumari el 22 de Abr. de 2024
Can you share the exact error you are getting? And the image aslo.
James Manns
James Manns el 22 de Abr. de 2024
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
Error in awgn (line 157)
y = sig + noise;
Error in Computerassignmentfinal (line 20)
received_low = awgn(lenna_bits, SNR_low,'measured');

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Abr. de 2024
lenna = imread('lenna.png');
the result is almost certainly a uint8 matrix.
lenna_gray = rgb2gray(lenna);
rgb2gray() of a uint8 matrix results in a uint8 matrix.
lenna_bits = reshape(de2bi(lenna_gray), [], 1);
de2bi() of a uint8 matrix results in a uint8 matrix. reshape() of a uint8 matrix results in a uint8 matrix.
received_high = awgn(lenna_bits, SNR_high, 'measured');
It is an error to pass a uint8 matrix to awgn()

Más respuestas (0)

Categorías

Más información sobre Error Detection and Correction en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by