Error Using Reshape in MATLAB
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How do I correct the following error in MATLAB? MATLAB 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(double(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(double(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 for linear error detection code
received_data = awgn(double(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 for Reed-Solomon code
received_rs_low = awgn(double(encoded_rs), Eb_No_low, 'measured');
received_rs_high = awgn(double(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)');
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that
dimension.
Error in Computerassignmentfinal (line 26)
decoded_image_low = reshape(decoded_low, size(lenna_gray));
0 Kommentare
Siehe auch
Kategorien
Mehr zu Color finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!