How to convert JPG to bits for simulink transmission?
Show older comments
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
on 22 Apr 2025
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:
- de2bi: https://www.mathworks.com/help/comm/ref/de2bi.html
- bi2de: https://www.mathworks.com/help/comm/ref/bi2de.html
- mod: https://www.mathworks.com/help/matlab/ref/double.mod.html
- Using Persistent Variables in MATLAB Function Blocks: https://www.mathworks.com/help/hdlcoder/ug/using-persistent-variables-inside-matlab-function-blocks-for-hdl-code-generation.html
Hope this resolves your issue.
Abhiram
on 22 Apr 2025
0 votes
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!