Hamming Code Encode Error
14 views (last 30 days)
Show older comments
James Manns
on 30 Apr 2024
Commented: Walter Roberson
on 30 Apr 2024
How to correct the following error?
Unable to resolve the name 'comm.HammingEncoder'.
Error in Finalcomputerassignment (line 75)
enc = comm.HammingEncoder;
clc;
clear all;
% Load the 'lenna' image
lenna = imread('lenna.png');
% Convert the image to grayscale
lenna_gray = rgb2gray(lenna);
% Convert pixel values to bits
lenna_bits = reshape(de2bi(lenna_gray, 8, 'left-msb'), [], 1);
% Define Eb/No values for low and high SNR
Eb_No_low = 0;
Eb_No_high = 4;
% Modulate using BPSK
modulated_signal = pskmod(lenna_bits, 2);
% Calculate SNR values for low and high SNR, taking into account the noise power in BPSK
% For BPSK, the bit energy is the same as the symbol energy since there is one bit per symbol
SNR_low = Eb_No_low + 10*log10(1);
SNR_high = Eb_No_high + 10*log10(1);
% Transmit and receive at low SNR
received_low = awgn(modulated_signal, SNR_low, 'measured');
% Demodulate received signal at low SNR
demodulated_low = pskdemod(received_low, 2);
% Transmit and receive at high SNR
received_high = awgn(modulated_signal, SNR_high, 'measured');
% Demodulate received signal at high SNR
demodulated_high = pskdemod(received_high, 2);
% Reshape demodulated bits to original image size
szin = size(lenna_gray,1:2);
decoded_image_low = reshape(demodulated_low, [], 8);
decoded_image_low = uint8(bi2de(decoded_image_low, 'left-msb'));
decoded_image_low = reshape(decoded_image_low, szin);
decoded_image_high = reshape(demodulated_high, [], 8);
decoded_image_high = uint8(bi2de(decoded_image_high, 'left-msb'));
decoded_image_high = reshape(decoded_image_high, szin);
% Employ a linear error detection code (only detection and no correction)
% Calculate and display Bit Error Rate (BER)
ber_low = sum(abs(double(lenna_bits) - double(demodulated_low))) / length(lenna_bits);
ber_high = sum(abs(double(lenna_bits) - double(demodulated_high))) / length(lenna_bits);
fprintf('BER at low SNR (0 dB): %f\n', ber_low);
fprintf('BER at high SNR (4 dB): %f\n', ber_high);
% Plot original, received image at 0 dB SNR, and received image at 4 dB SNR
figure('Position', [100, 100, 1200, 400]);
% Original Image
subplot(1, 3, 1);
imshow(lenna_gray);
title('Original Image');
% Received Image at 0 dB SNR
subplot(1, 3, 2);
imshow(decoded_image_low);
title('Received Image (0 dB SNR)');
% Received Image at 4 dB SNR
subplot(1, 3, 3);
imshow(decoded_image_high);
title('Received Image (4 dB 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
0 Comments
Accepted Answer
Walter Roberson
on 30 Apr 2024
comm.HammingEncoder is old MATLAB (some point before R2019a).
2 Comments
More Answers (0)
See Also
Categories
Find more on FSK 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!