Hi James,
The MATLAB code you have provided aims to simulate the transmission and reception of an image in a noisy environment, emphasizing the impact of signal-to-noise ratio (SNR) on image quality. However, there are a few conceptual and practical issues that might lead to unexpected outcomes. Here is a concise breakdown of these issues along with proposed modifications to enhance the simulation:
- Lack of Modulation Scheme: Transmitting raw bits doesn't mirror real-world digital communications, where modulation is essential. For example, implement BPSK modulation using "pskmod" for transmission and "pskdemod" for reception to improve robustness and realism.
- Improper SNR Calculation for Modulated Signals: The current SNR calculation overlooks the modulation scheme, leading to inaccurate noise modeling. Adjust the SNR calculation to reflect the modulation scheme's characteristics accurately. Also, remember that "awgn" requires the SNR input in dB.
- Inefficient Bitstream Handling: Converting image pixels directly to a bitstream and back without considering the modulation scheme's requirements. Use "reshape" and "de2bi" for efficient bitstream conversion, ensuring it aligns with the modulation and demodulation process.
- Incorrect Use of dec2bin and bi2dec: Potential misuse could compromise data integrity. Correctly apply "de2bi" and "bi2de" with the 'left-msb' argument for consistent bit ordering, maintaining data integrity.
Here is how you can refine your code considering the above points:
% 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); % Ensure 8 bits per pixel and 'left-msb' ordering
% 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); % BPSK modulation
% 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); % Since BPSK has 1 bit per symbol, log10(1) = 0, but shown for clarity
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); % BPSK demodulation
% 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); % BPSK demodulation
% 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);
% 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)');
You can evaluate the performance using the bit error rate (BER) metric as follows:
% 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);
Please refer to the following documentation pages for further details and better understanding of the use cases of respective functions:
- Modulation: https://www.mathworks.com/help/comm/modulation.html
- "awgn": https://www.mathworks.com/help/comm/ref/awgn.html
- "reshape": https://www.mathworks.com/help/matlab/ref/reshape.html
- "bi2de": https://www.mathworks.com/help/comm/ref/bi2de.html
- "de2bi": https://www.mathworks.com/help/comm/ref/de2bi.html
I hope this helps!