Main Content

Complex Data Type Support for High-Level Synthesis Code Generation

Declaring Complex Signals

The following MATLAB® code declares several local complex variables. x and y are declared by complex constant assignment; z is created using the using the complex() function.

function [x,y,z] = fcn

% create 8 bit complex constants
x = uint8(1 + 2i);
y = uint8(3 + 4j);
z = uint8(complex(5, 6));

The following code example shows High-Level Synthesis (HLS) code generated from the previous MATLAB code.

class fcnClass 
{ 
 public: 
  void fcn(sc_fixed<16,3> &x_re, sc_fixed<16,3> &x_im, sc_fixed<16,4> &y_re, 
           sc_fixed<16,4> &y_im, sc_fixed<16,4> &z_re, sc_fixed<16,4> &z_im) 
  { 
    x_re = sc_fixed<16,3>(1.0); 
    x_im = sc_fixed<16,3>(2.0); 
    y_re = sc_fixed<16,4>(3.0); 
    y_im = sc_fixed<16,4>(4.0); 
    z_re = sc_fixed<16,4>(5.0); 
    z_im = sc_fixed<16,4>(6.0); 
  } 
};

As shown in the example, complex inputs, outputs and local variables declared in MATLAB code expand into real and imaginary signals. The naming conventions for these derived signals are:

  • Real components have the same name as the original complex signal, suffixed with the default string '_re' (for example, x_re). To specify a different suffix, set the Complex real part postfix option (or the corresponding ComplexRealPostfix CLI property).

  • Imaginary components have the same name as the original complex signal, suffixed with the string '_im' (for example, x_im). To specify a different suffix, set the Complex imaginary part postfix option (or the corresponding ComplexImagPostfix CLI property).

A complex variable declared in MATLAB code remains complex during the entire length of the program.

Conversion Between Complex and Real Signals

The MATLAB code provides access to the fields of a complex signal via the real() and imag() functions, as shown in the following code.

function [Re_part, Im_part]= fcn(c)
% Output real and imaginary parts of complex input signal
 
Re_part = real(c);
Im_part = imag(c);

HDL Coder™ supports these constructs, accessing the corresponding real and imaginary signal components in generated HLS code. In the following HLS code, the MATLAB complex signal variable c is flattened into the signals c_re and c_im. Each of these signals is assigned to the output variables Re_part and Im_part, respectively.

class fcnClass 
{ 
 public: 
  void fcn(sc_fixed<16,3> c_re, sc_fixed<16,3> c_im, sc_fixed<16,3> &Re_part, 
           sc_fixed<16,3> &Im_part) 
  { 
    /*  Output real and imaginary parts of complex input signal */ 
    Re_part = c_re; 
    Im_part = c_im; 
  } 
}; 

Support for Vectors of Complex Numbers

You can generate HLS code for vectors of complex numbers. Like scalar complex numbers, vectors of complex numbers are flattened down to vectors of real and imaginary parts in generated HLS code.

For example in the following script t is a complex vector variable of base type ufix4 and size [1,2].

function y = fcn(u1, u2)
 
t = [u1 u2];
y = t+1;

In the generated HLS code the variable t is broken down into real and imaginary parts with the same two-element array. The real and imaginary parts of the complex number have the same vector of type ufix4, as shown in the following code.

sc_uint<4> t_re[2]; 
sc_uint<4> t_im[2]; 

In the above MATLAB script, u1 and u2 are scalar complex numbers and y is a vector of complex numbers. This generates the following function definition in the HLS code.

void fcn(sc_uint<4> u1_re, sc_uint<4> u1_im, sc_uint<4> u2_re, sc_uint<4> 
           u2_im, sc_uint<5> (&y_re)[2], sc_uint<5> (&y_im)[2]) 
 

Complex vector-based operations (+,-,* etc.,) are similarly broken down to vectors of real and imaginary parts. Operations are performed independently on the elements of such vectors, following MATLAB semantics for vectors of complex numbers.

Restrictions When Defining Complex Variables

For code generation, you must set the complexity of variables at the time of assignment. Assign a complex constant to the variable or use the complex function. For example:

x = 5 + 6i; % x is a complex number by assignment.
y = complex(5,6); % y is the complex number 5 + 6i.
After assignment, you cannot change the complexity of a variable. Code generation for the following function fails because x(k) = 3 + 4i changes the complexity of x.
function x = test1()
    x = zeros(3,3); % x is real
    for k = 1:numel(x)
        x(k) = 3 + 4i;
    end
end
To resolve this issue, assign a complex constant to x.
function x = test1()
    x = zeros(3,3)+ 0i; %x is complex
    for k = 1:numel(x)
        x(k) = 3 + 4i;
    end
end

See Also

|

Related Topics