Error Using horzcat in MATLAB

1 Ansicht (letzte 30 Tage)
James Manns
James Manns am 27 Apr. 2024
Kommentiert: James Manns am 27 Apr. 2024
How to correct the following error in MATLAB? The received image is just a block of static.
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, 1), []);
% 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, 1), []);
% 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 horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Computerassignmentfinal (line 50)
generatorMatrix = [eye(4) parityMatrix'];

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Apr. 2024
parityMatrix = [1 1 1 0 1 0 0; 1 1 0 1 0 1 0; 1 0 1 1 0 0 1];
parityMatrix is 3 x 7
generatorMatrix = [eye(4) parityMatrix'];
eye(4) is 4 x 4
parityMatrix' is 7 x 3
You are trying to [] a 4 x 4 and a 7 x 3. There is no way that can work.
If parityMatrix was 4 x 7 and you were to [eye(4) parityMatrix] then that would work.
  1 Kommentar
James Manns
James Manns am 27 Apr. 2024
I used the following and it showed the same output:
parityMatrix = [1 1 1 1
1 1 0 0
1 0 1 0
0 1 1 1
1 0 0 1
0 1 0 0
0 0 1 1];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by