How to convert JPG to bits for simulink transmission?

4 views (last 30 days)
I am trying to design a system where I can upload a JPG file, and then run it through a matlab function block which converts the jpg file to bits so that i can use it for an OFDM IEEE 802.11a simulink model. The reference model I am using is the following link attached:
I am trying to replace the bernoulli generator with a Matlab Function block which converts the jpg file into bits so i can use that as for transmission, and after the simulation is over, I intend to collect those bits that were transmitted and reconstruct the image again. The OFDM system sends in 144 bits at a time, so that needs to be noted when designing the matlab code for jpg to bits. Any and all help will be greatly appreciated!

Answers (2)

Nithin
Nithin on 22 Apr 2025
Hi @Nisab,
To substitute the Bernoulli Binary Generator with a custom "MATLAB Function" block that provides a bitstream from an image, you’ll first need to convert the image into bits as a preprocessing step. Then, you can access these bits within the "MATLAB Function" block in Simulink. Refer to the steps below to understand the workflow:
  • Pre-process the image in MALTAB to store the image as a bitstream
% Read the image file as bytes
fid = fopen('input.jpg', 'rb');
imgBytes = fread(fid, inf, 'uint8');
fclose(fid);
% Convert bytes to bits
imgBits = reshape(de2bi(imgBytes, 8, 'left-msb')', [], 1); % Column vector
% Pad bits to be a multiple of 144
padLength = mod(144 - mod(length(imgBits), 144), 144);
imgBitsPadded = [imgBits; zeros(padLength, 1)];
% Save for use in Simulink (optional)
save('imgBitsPadded.mat', 'imgBitsPadded');
  • Use a "MATLAB Function" block inside Simulink to output 144 bits per call by reading from the preprocessed bitstream.
function bitsOut = getImageBits()
persistent imgBits idx
if isempty(imgBits)
temp = load('imgBitsPadded.mat');
imgBits = temp.imgBitsPadded;
idx = 1;
end
% Output 144 bits per call
if idx + 143 <= length(imgBits)
bitsOut = imgBits(idx:idx+143);
idx = idx + 144;
else
bitsOut = zeros(144,1); % Or wrap around
end
  • Finally if you are looking to reconstruct the image, after the simulation, collect the received bits (rxBits) in the workspace and reconstruct the image as follows:
% Remove padding if added
rxBits = rxBits(1:end-padLength);
% Convert bits back to bytes
rxBytes = bi2de(reshape(rxBits, 8, [])', 'left-msb');
% Write bytes to file
fid = fopen('output.jpg', 'wb');
fwrite(fid, rxBytes, 'uint8');
fclose(fid);
  • Refer to the image below which shows the results after simulation
Kindly refer to the following MathWorks documentations to know more about the functions used:
Hope this resolves your issue.
  2 Comments
Nisab
Nisab on 22 Apr 2025
Thank you so much! I was wondering if you had any parameters you used while running the simulation, for example the sample time for the image to bits matlab block, or the stop time for the simulation as well? While I was using my own image, i had gotten these results attached. There is clearly something wrong with the spectrum when I tried to run it, and the output jpg file doesnt open as well (unsupported file format from Windows 11)
Any help in troubleshooting this is greatly appreciated, thank you so much for your help so far!
Nisab
Nisab on 25 Apr 2025
Adding onto my last confusion, the rxBits that i receive using my picture is 144x1x27779 double , which is way more than the txBits, 24288x1 double. Are there any parameters you used to limit the sample time so that only the txBits amount is sent instead of the 144x1x27779 double?
Your help is greatly appreciated!

Sign in to comment.


Abhiram
Abhiram on 22 Apr 2025
Hi @Nisab,
To convert a JPG file to bits, you can refer to the MATLAB Answers page linked below:
Additionally, you can refer to the MATLAB Documentation for “Image Transmission and Reception Using 802.11 Waveform and SDR” for further guidance on how to encode and pack an image file into WLAN packets for transmission and subsequently decode the packets to retrieve the image.
Hope this helps!

Categories

Find more on Communications Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!