Error Using Integers in MATLAB

3 vues (au cours des 30 derniers jours)
James Manns
James Manns le 29 Avr 2024
Commenté : James Manns le 29 Avr 2024
How to correct the following error:
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
Error in Computerassignment5b042824 (line 28)
received_signal_low = modulated_bits + noise_low;
clc;
clear all;
% Task 1: Simulate transmission of lenna.pgm image using BPSK
% Read the image
img = imread('lenna.png');
% Convert image to bits
img_bits = reshape(de2bi(img(:), 8, 'left-msb').', [], 1);
% BPSK modulation
modulated_bits = 2*img_bits - 1;
% Eb/No values in dB
EbNo_low = 0;
EbNo_high = 4;
% Additive White Gaussian Noise (AWGN) channel
SNR_low = 10^(EbNo_low/10);
SNR_high = 10^(EbNo_high/10);
% Generate noise
noise_low = sqrt(1/SNR_low)*randn(size(modulated_bits));
noise_high = sqrt(1/SNR_high)*randn(size(modulated_bits));
% Received signals at low and high SNR
received_signal_low = modulated_bits + noise_low;
received_signal_high = modulated_bits + noise_high;
% BPSK demodulation
demodulated_bits_low = sign(received_signal_low);
demodulated_bits_high = sign(received_signal_high);
% Convert bits back to image
received_img_low = reshape((demodulated_bits_low + 1) / 2, 8, []).';
received_img_high = reshape((demodulated_bits_high + 1) / 2, 8, []).';
% Display images
figure;
subplot(2,2,1);
imshow(img);
title('Original Image');
subplot(2,2,2);
imshow(received_img_low, []);
title('Received Image (0 dB SNR)');
subplot(2,2,3);
imshow(received_img_high, []);
title('Received Image (4 dB SNR)');
% Task 2: Employ a linear error detection code (only detection and no correction)
% Count re-transmission requests at different SNR levels
EbNo_values = [0, 2, 4, 6, 8, 10];
retrans_requests = zeros(size(EbNo_values));
for i = 1:length(EbNo_values)
SNR = 10^(EbNo_values(i)/10);
noise = sqrt(1/SNR)*randn(size(modulated_bits));
received_signal = modulated_bits + noise;
demodulated_bits = sign(received_signal);
% Linear error detection code
% Let's use a simple parity check code
error_idx = mod(sum(demodulated_bits == -1), 2) == 1;
if any(error_idx)
retrans_requests(i) = sum(error_idx);
else
break; % No errors, stop transmission
end
end
% Plot number of retransmission requests against SNR values
figure;
plot(EbNo_values(1:i), retrans_requests(1:i), '-o');
xlabel('Eb/No (dB)');
ylabel('Number of Retransmission Requests');
title('Retransmission Requests vs. SNR');
% Task 3: Use an error correction code using syndrome lookup table
% Error correction code
% Let's use a (7, 4) Hamming code
enc = comm.HammingEncoder;
dec = comm.HammingDecoder;
% Corrected images at 0 dB and 4 dB SNR
corrected_img_low = correct_image(received_signal_low, enc, dec);
corrected_img_high = correct_image(received_signal_high, enc, dec);
% Display corrected images
figure;
subplot(1,2,1);
imshow(corrected_img_low, []);
title('Corrected Image (0 dB SNR)');
subplot(1,2,2);
imshow(corrected_img_high, []);
title('Corrected Image (4 dB SNR)');
% Function to correct image using error correction code
function corrected_img = correct_image(received_signal, enc, dec)
% BPSK demodulation
demodulated_bits = sign(received_signal);
% Perform error detection and correction
detected_bits = step(dec, demodulated_bits);
% Convert bits back to image
corrected_img = reshape((detected_bits + 1) / 2, 8, []).';
end
  3 commentaires
James Manns
James Manns le 29 Avr 2024
James Manns
James Manns le 29 Avr 2024
Image attached.

Connectez-vous pour commenter.

Réponse acceptée

Harsh
Harsh le 29 Avr 2024
Hi James,
From what I can gather, you are trying to run the above-mentioned code in MATLAB and are facing an error while using (+) operand.
This error has occured because modulated_bitsand noise_low are of type of uint8 and double respectively, combining them is not possible without matching the data types of the two operands.
Converting noise_low to uint8 using the “uint8()” function or modulated_bitsto double using the “double()” function can resolve this error, please refer to the code below which resolves your issue:
% (in line no. 28)
received_signal_low = double(modulated_bits) + noise_low;
Please ensure to match the data types of the variable being updated throughout the subsequent lines.
I hope this helps, thanks!
  5 commentaires
Harsh
Harsh le 29 Avr 2024
Modifié(e) : Harsh le 29 Avr 2024
Hi Dyuman,
Apologies for any confusion I may have caused. In the following sentence, I neglected to include: {.... are/ arrays /of type uint8 ....}
"This error has occured because “modulated_bits” and “noise_low” are of type of uint8 and double respectively, combining them is not possible without matching the data types of the two operands. "
"Also, how are you saying that modulated_bits is of data type uint8?"
After running the above mentioned script, "modulated_bits" was stored as an array of type uint8 in my MATLAB desktop.
DGM
DGM le 29 Avr 2024
I hardly ever use it, so I did think it was odd that the output of de2bi() would inherit the class of the input. It seems it does.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Error Detection and Correction dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by