Error Using Integers in MATLAB
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
Harsh
2024년 4월 29일
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_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.
Converting “noise_low” to uint8 using the “uint8()” function or “modulated_bits” to 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
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
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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Error Detection and Correction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!