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 件のコメント

Dyuman Joshi
Dyuman Joshi 2024 年 4 月 29 日
Note that de2bi is not recommended. Use int2bit instead.
Please attach the image so we can run your code and reproduce the error.
James Manns
James Manns 2024 年 4 月 29 日
James Manns
James Manns 2024 年 4 月 29 日
Image attached.

サインインしてコメントする。

 採用された回答

Harsh
Harsh 2024 年 4 月 29 日

0 投票

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 件のコメント

Dyuman Joshi
Dyuman Joshi 2024 年 4 月 29 日
編集済み: Dyuman Joshi 2024 年 4 月 29 日
"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. "
That is incorrect, you can combine them given the double value is a scalar -
a = uint8([5 6 7])
a = 1x3
5 6 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
b = 2.5
b = 2.5000
a+b
ans = 1x3
8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I suggest you modify the wording of your statement accordingly.
Also, how are you saying that modulated_bits is of data type uint8?
DGM
DGM 2024 年 4 月 29 日
編集済み: DGM 2024 年 4 月 29 日
This is something that only seems to happen when doing operations between two non-scalar arrays.
a = 5*ones(10,'uint8');
b = 2.5*ones(10,'double');
a+b
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
I'm pretty sure there's more to it than that, but the given code is throwing an error due to the mixed-class operation.
Dyuman Joshi
Dyuman Joshi 2024 年 4 月 29 日
Yes, that is somewhat evident from the error message. I will modify my comment accordingly.
Harsh
Harsh 2024 年 4 月 29 日
編集済み: Harsh 2024 年 4 月 29 日
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 2024 年 4 月 29 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2023b

タグ

質問済み:

2024 年 4 月 29 日

コメント済み:

2024 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by