Error Using Integers in MATLAB
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
