Generate HDL and HLS Code from an RGB to YUV Color Space Conversion Algorithm
R2026bThis example shows how to generate HDL and HLS code from a MATLAB® function that converts pixel data from RGB color space to YUV color space using a matrix transformation.
RGB to YUV conversion is a fundamental operation in video processing pipelines. Display and camera systems capture images in RGB. However, video compression standards and broadcast systems operate on luminance (Y) and chrominance (U, V) components because human vision is more sensitive to brightness than color detail. Separating these components enables more efficient compression and bandwidth usage.
Examine the MATLAB Design and Test Bench
Set up the MATLAB function and test bench for this example. In the MATLAB Command Window, enter:
mlhdlc_demo_setup("mlhdlc_rgb2yuv");MATLAB function
Test bench
<model>_runme.mscript, which includes the commands to generate HDL code<model>_runme_hls.mscript, which includes the commands to generate HLS code
The MATLAB function, mlhdlc_rgb2yuv, accepts pixel coordinates and
individual R, G, and B color channel values as inputs. It applies a 3x3 matrix
transformation with an offset vector to produce the Y, U, and V components. The function
uses persistent registers to pipeline the computation, outputting one converted pixel per
call along with delayed coordinate signals.
To view the function, enter:
open mlhdlc_rgb2yuv.m;The test bench file, mlhdlc_rgb2yuv_tb, verifies the behavior of the
MATLAB function. It reads an image, streams each pixel through the design with horizontal
and vertical blanking intervals, and displays the original RGB image alongside the
reconstructed YUV output. To view the test bench,
enter:
open mlhdlc_rgb2yuv_tb
Simulate the Design
To check for run-time errors, simulate the design by running the test bench. In the MATLAB Command Window, enter:
mlhdlc_rgb2yuv_tb;
Generate HDL Code
The mlhdlc_rgb2yuv_runme script specifies the target files,
enables the settings required for this example, and generates HDL code.
The script specifies the MATLAB function and test bench, then creates a fixed-point
configuration object and an HDL configuration object by using the
coder.config function. It associates the test bench with both
configuration
objects.
designName = "mlhdlc_rgb2yuv"; designTB = "mlhdlc_rgb2yuv_tb"; fixptCfg = coder.config("fixpt"); fixptCfg.TestBenchName = designTB; cfg = coder.config("hdl"); cfg.TestBenchName = designTB;
The script then generates code:
codegen("-float2fixed", "fixptCfg", "-config", "cfg", designName, ... "-launchreport");
Run the Script
First, modify the mlhdlc_rgb2yuv_runme script. The script
disables synthesis by default. Set the value for the
SynthesizeGeneratedCode property to
true:
cfg.SynthesizeGeneratedCode = true;
Update the settings for your synthesis tool:
cfg.SynthesisTool = "Xilinx Vivado"; cfg.SynthesisToolChipFamily = "Artix7"; cfg.SynthesisToolDeviceName = "xa7a100t"; cfg.SynthesisToolPackageName = "csg324"; cfg.SynthesisToolSpeedValue = "-1I";
Then, generate code by running the script:
mlhdlc_rgb2yuv_runme
After code generation completes, the report opens. Examine the generated HDL code in the report.
Generate HLS Code
The mlhdlc_rgb2yuv_runme_hls script specifies the target
files, enables the settings required for this example, and generates HLS code.
The script specifies the MATLAB function and test bench, then creates a fixed-point
configuration object and an HLS configuration object by using the
coder.config function. It associates the test bench with both
configuration
objects.
designName = "mlhdlc_rgb2yuv"; designTB = "mlhdlc_rgb2yuv_tb"; fixptCfg = coder.config("fixpt"); fixptCfg.TestBenchName = designTB;
The script creates an HLS configuration object by using the
coder.config function, associates the test bench, and enables
simulation.
cfg = coder.config("hls");
cfg.TestBenchName = designTB;
cfg.GenerateHLSTestBench = true;
cfg.SimulateGeneratedCode = true;The script then generates code:
codegen("-float2fixed", "fixptCfg", "-config", "cfg", designName, ... "-launchreport");
Run the Script
First, modify the mlhdlc_rgb2yuv_runme_hls script. The script
disables synthesis by default. Set the value for the
SynthesizeGeneratedCode property to
true:
cfg.SynthesizeGeneratedCode = true;
Depending on your synthesis tool, set one of these flags to
true:
isCodingForStratusHLS = false; isCodingForVitisHLS = true;
Depending on your synthesis tool, update these synthesis tool settings:
if isCodingForStratusHLS cfg.SynthesisTool = "Cadence Stratus HLS"; elseif isCodingForVitisHLS cfg.SynthesisTool = "Xilinx Vitis HLS"; cfg.SynthesisToolChipFamily = "Artix7"; cfg.SynthesisToolDeviceName = "xa7a100t"; cfg.SynthesisToolPackageName = "csg324"; cfg.SynthesisToolSpeedValue = "-1I"; end
Then, generate code by running the script:
mlhdlc_rgb2yuv_runme_hls
After code generation completes, the report opens. Examine the generated HLS code in the report.