lenna = imread('lenna.png');
lenna_gray = rgb2gray(lenna);
lenna_bits = reshape(de2bi(lenna_gray, 8, 'left-msb'), [], 1);
modulated_signal = pskmod(lenna_bits, 2);
SNR_low = Eb_No_low + 10*log10(1);
SNR_high = Eb_No_high + 10*log10(1);
received_low = awgn(modulated_signal, SNR_low, 'measured');
demodulated_low = pskdemod(received_low, 2);
received_high = awgn(modulated_signal, SNR_high, 'measured');
demodulated_high = pskdemod(received_high, 2);
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);
figure('Position', [100, 100, 1200, 400]);
imshow(decoded_image_low);
title('Received Image (0 dB SNR)');
imshow(decoded_image_high);
title('Received Image (4 dB SNR)');
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);
BER at low SNR (0 dB): 0.078496
fprintf('BER at high SNR (4 dB): %f\n', ber_high);
BER at high SNR (4 dB): 0.012567