Main Content

Create a Line Buffer Interface for SystemC Code Generation

In this example, you will learn how to generate SystemC™ code using a line buffer interface from MATLAB® code. The MATLAB code implements a simple Sobel filter, which is used for edge detection in images.

The Sobel filter edge detection algorithm operates on grey scale image and produces an image that highlights the high spatial frequency regions corresponding to edges in the input image.

The example highlights these aspects of generating SystemC code from MATLAB Code:

  • Line buffer interface - Use the coder.hdl.interface pragma in your MATLAB code to implement the line buffer interface. The line buffer interface supports only two-dimensional matrices for both working sets and the interface.

  • Input matrix - The entry point function hls_sobel in the MATLAB code accepts two-dimensional matrices as input and returns the pixel value.

  • Generating 2D working sets - Use the hdl.WorkingSet class in your MATLAB testbench to generate two-dimensional working sets from the input image.

Sobel Filter MATLAB Design

The hls_sobel function operates on a given working set by applying the Sobel filter kernel. The function returns the output pixel that corresponds to the position of the working set in the input image.

dbtype('hls_sobel')
1     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2     %% Sobel edge detection using Line buffer interface in HLS.
3     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4     % This example shows how to generate a line buffer interface from the MATLAB(R)
5     % design using MATLAB to HLS workflow. This MATLAB design implements the 
6     % Sobel edge detection algorithm by the following steps.
7     % 
8     % 1. Compute the horizontal and vertical gradients, gh and gv by convolving
9     %    the working set with the Sobel kernel and its transpose.
10    %      Sobel kernel = [-1 -2 -1
11    %                       0  0  0
12    %                       1  2  1];
13    % 2. Compute the gradient y as follows:
14    %      y = floor(abs(gh) + abs(gv)) / 4;
15    % 3. If the gradient is greater than the threshold, the pixel is considered
16    %    an edge pixel. In this example, the threshold is chosen as 255.
17    
18    %#codegen
19    
20    %   Copyright 2023 The MathWorks, Inc.
21    function y = hls_sobel(ws)
22    
23    ORIGIN = [2 2];
24    IMAGE_SIZE = [480 752];
25    CONSTANT_FILL_VALUE = 0;
26    
27    % Specify the line buffer properties using coder.hdl.interface pragma.
28    coder.hdl.interface(ws, 'Line Buffer', IMAGE_SIZE, 'Origin', ORIGIN, 'FillValue', CONSTANT_FILL_VALUE);
29    
30    ws1 = reshape(ws, [1 3*3]);
31    
32    % Sobel kernel
33    Gx = [-1 0 1 -2 0 2 -1 0 1];
34    Gy = [-1 -2 -1 0 0 0 1 2 1];
35    
36    % Compute horizontal and vertical gradients
37    gh = sum(ws1 .* Gx);
38    gv = sum(ws1 .* Gy);
39    
40    % Compute the gradient.
41    y = floor(abs(gh) + abs(gv)) / 4;
42    
43    % Determine whether the pixel is on the edge or not.
44    y = min(y,255);
45    
46    end

Sobel Filter MATLAB Test Bench

In MATLAB testbench, use the hdl.WorkingSet class to generate working sets from the input image and pass them to the hls_sobel function. The dimensions of each working set matrix are determined by the WINDOW_SIZE parameter defined in the testbench.

dbtype('hls_sobel_tb')
1     %
2     
3     %   Copyright 2023 The MathWorks, Inc.
4     
5     clear;
6     
7     % Read the image for which the edge detection needs to be performed.
8     image3d = double(imread('mlhdlc_img_yuv.png'));
9     image = image3d(:,:,1);
10    
11    [IMAGE_HEIGHT, IMAGE_WIDTH] = size(image);
12    WINDOW_SIZE = [3 3];  % Size of the working set/Window size
13    ORIGIN = [2 2];    % Working set origin
14    % Constant value to fill the pixels falling outside the image in the working set.
15    CONSTANT_FILL_VALUE = 0;
16    
17    y = zeros(IMAGE_HEIGHT,IMAGE_WIDTH);
18    
19    % Create the working set object. This helps in creating the 2-D working sets
20    % of specified size and origin from the input image.
21    ws = hdl.WorkingSet(image, WINDOW_SIZE, 'Origin', ORIGIN, 'FillValue', CONSTANT_FILL_VALUE);
22    
23    for i = 1:IMAGE_HEIGHT
24        for j = 1:IMAGE_WIDTH
25            % Get the working set at the pixel (i,j) in the image.
26            workingSet = ws.getWorkingSet(i,j);
27            y(i,j) = hls_sobel(workingSet);
28        end
29    end
30    
31    % Plot the original image and the extracted edges.
32    figure('Name', [mfilename, '_plot'])
33    
34    subplot(1,2,1);
35    imshow(uint8(image));
36    title('Original Image');
37    
38    subplot(1,2,2);
39    imshow(uint8(y));
40    title('Edges');

Simulate the MATLAB Algorithm

To avoid run-time errors, simulate the design with the test bench.

hls_sobel_tb

Generate SystemC Code by Using HDL Workflow Advisor

To generate SystemC code from the MATLAB design:

1. At the MATLAB command line, set up the high-level synthesis (HLS) tool path for SystemC code generation by using the function hdlsetuphlstoolpath.

2. Create an HDL Coder project by adding the hls_sobel.m design file and the hls_sobel_tb.m test bench file to the HDL workflow advisor.

3. Launch the HDL Workflow Advisor. Select Code Generation Workflow as MATLAB to SystemC.

4. In the Select Code Generation Target step, from the Synthesis tool list, select Cadence Stratus.

5. Right-click the SystemC Code Generation task and select the Run to selected task to run all the steps from the beginning through the SystemC code generation.

For more information, see Get Started with MATLAB to High-Level Synthesis Workflow Using HDL Coder App.

Examine the Generated Code

You can examine the generated SystemC code by clicking the hyperlinks in the SystemC Code Generation step log window.

The line buffer properties specified in the coder.hdl.interface pragma are populated in the ml.tcl metadata file. These are read by the Stratus HLS tool to create appropriate line buffers.

See Also

Related Topics