Magnitude and phase angle of complex signal—optimized for HDL code generation
object™ computes the magnitude and phase angle of a complex signal. It provides
hardware-friendly control signals. The System
object uses a pipelined coordinate rotation digital computer (CORDIC) algorithm to
achieve an HDL-optimized implementation.
To compute the magnitude and phase angle of a complex signal:
dsp.HDLComplexToMagnitudeAngle object and set its properties.
Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects? (MATLAB).
HCMA = dsp.HDLComplexToMagnitudeAngle
HCMA, that computes the magnitude and phase angle of a
complex input signal.
sets properties of
HCMA = dsp.HDLComplexToMagnitudeAngle(
HCMA using one or more name-value pairs. Enclose
each property name in single quotes.
cma = dsp.HDLComplexToMagnitudeAngle('AngleFormat','Radians')
Unless otherwise indicated, properties are nontunable, which means you cannot change their
values after calling the object. Objects lock when you call them, and the
release function unlocks them.
If a property is tunable, you can change its value at any time.
For more information on changing property values, see System Design in MATLAB Using System Objects (MATLAB).
OutputValue— Type of values to return
'Magnitude and angle'(default) |
Type of output values to return, specified as
can choose for the object to return the magnitude of the input signal, or the phase
angle of the input signal, or both.
AngleFormat— Format of phase angle output value
Format of the phase angle output value from the object, specified as:
'Normalized' — Fixed-point format that normalizes the angle
in the range [–1,1].
'Radians' — Fixed-point values in the range [π,−π].
ScaleOutput— Scale output by inverse of CORDIC gain factor
Scale output by the inverse of the CORDIC gain factor, specified as
If your design includes a gain factor later in the datapath, you can set
false, and include the CORDIC
gain factor in the later gain. For calculation of this gain factor, see Algorithm. The object replaces
the first CORDIC iteration by mapping the input value onto the angle range [0,π/4].
Therefore, the initial rotation does not contribute a gain term.
NumIterationsSource— Source of
Source of the
NumIterations property for the CORDIC algorithm,
'Auto' — Sets the number of iterations to one less than the
input word length. If the input is
single, the number of iterations is 16.
'Property' — Uses the
For details of the CORDIC algorithm, see Algorithm.
NumIterations— Number of CORDIC iterations
Number of CORDIC iterations that the object executes, specified as an integer. The number of iterations must be less than or equal to one less than the input word length.
For details of the CORDIC algorithm, see Algorithm.
To enable this property, set
X— Input signal
Input signal, specified as a complex scalar value.
single are allowed for simulation but not for HDL code
Complex Number Support: Yes
validIn— Validity of input signal
Validity of the input signal, specified as a logical scalar.
mag— Magnitude component of input signal
Magnitude calculated from the complex input signal, returned as a scalar value with the same data type as the input signal.
angle— Phase angle component of input signal
Angle calculated from the complex input signal, returned as a scalar value with the same data type as the input signal. The format of this value depends on the AngleFormat property.
validOut— Validity of output components
Validity of the output components, returned as a logical scalar.
To use an object function, specify the
object as the first input argument. For
example, to release system resources of a System
dsp.HDLComplextoMagnitudeAngle object to compute the magnitude and phase angle of a complex signal. The object uses a CORDIC algorithm for an efficient hardware implementation.
Choose word lengths and create random complex input signal. Then, convert the input signal to fixed-point.
a = -4; b = 4; inputWL = 16; inputFL = 12; numSamples = 10; reData = ((b-a).*rand(numSamples,1)+a); imData = ((b-a).*rand(numSamples,1)+a); dataIn = (fi(reData+imData*1i,1,inputWL,inputFL)); figure plot(dataIn) title('Random Complex Input Data') xlabel('Real') ylabel('Imaginary')
Write a function that creates and calls the System object™. You can generate HDL from this function.
Note: This object syntax runs only in R2016b or later. If you are using an earlier release, replace each call of an object with the equivalent
step syntax. For example, replace
function [mag,angle,validOut] = Complex2MagAngle(yIn,validIn) %Complex2MagAngle % Converts one sample of complex data to magnitude and angle data. % yIn is a fixed-point complex number. % validIn is a logical scalar value. % You can generate HDL code from this function. persistent cma; if isempty(cma) cma = dsp.HDLComplexToMagnitudeAngle('AngleFormat','Radians'); end [mag,angle,validOut] = cma(yIn,validIn); end
The number of CORDIC iterations determines the latency that the object takes to compute the answer for each input sample. The latency is
NumIterations+4. In this example,
NumIterationsSource is set to the default,
'Auto', so the object uses
inputWL-1 iterations. The latency is
latency = inputWL+3; mag = zeros(1,numSamples+latency); ang = zeros(1,numSamples+latency); validOut = false(1,numSamples+latency);
Call the function to convert each sample. After you apply all input samples, continue calling the function with invalid input to flush remaining output samples.
for ii = 1:1:numSamples [mag(ii),ang(ii),validOut] = Complex2MagAngle(dataIn(ii),true); end for ii = (numSamples+1):1:(numSamples+latency) [mag(ii),ang(ii),validOut(ii)] = Complex2MagAngle(fi(0+0*1i,1,inputWL,inputFL),false); end % Remove invalid output values mag = mag(validOut == 1); ang = ang(validOut == 1); figure polar(ang,mag,'--r') % Red is output from HDL-optimized System object title('Output from dsp.HDLComplexToMagnitudeAngle') magD = abs(dataIn); angD = angle(dataIn); figure polar(angD,magD,'--b') % Blue is output from abs and angle functions title('Output from abs and angle Functions')
The CORDIC algorithm is a hardware-friendly method for performing trigonometric functions. It is an iterative algorithm that approximates the solution by converging toward the ideal point. The object uses CORDIC vectoring mode to iteratively rotate the input onto the real axis.
The Givens method for rotating a complex number x+iy by an angle θ is as follows. The direction of rotation, d, is +1 for counterclockwise and −1 for clockwise.
For a hardware implementation, factor out the cosθ to leave a tanθ term.
To rotate the vector onto the real axis, choose a series of rotations of θn so that . Remove the cosθ term so each iterative rotation uses only shift and add operations.
Combine the missing cosθ terms from each iteration into a constant, and apply it with a single multiplier to the result of the final rotation. The output magnitude is the scaled final value of x. The output angle, z, is the sum of the rotation angles.
The convergence region for the standard CORDIC rotation is ≈±99.7°. To work around this limitation, before doing any rotation, the object maps the input into the [0,π/4] range using the following algorithm.
if abs(x) > abs(y) input_mapped = [abs(x), abs(y)]; else input_mapped = [abs(y), abs(x)]; end
Quadrant mapping saves hardware resources and reduces latency by reducing the number of CORDIC pipeline stages by one. The CORDIC gain factor, Kn, therefore does not include the n=0, or cos(π/4) term.
After the CORDIC iterations are complete, the object corrects the angle back to its original location. First it adjusts the angle to the correct side of π/4.
if abs(x) > abs(y) angle_unmapped = CORDIC_out; else angle_unmapped = (pi/2) - CORDIC_out; end
if (x < 0) if (y < 0) output_angle = - pi + angle_unmapped; else output_angle = pi - angle_unmapped; else if (y<0) output_angle = -angle_unmapped;
The object generates a pipelined HDL architecture to maximize throughput. Each CORDIC iteration is done in one pipeline stage. The gain multiplier, if enabled, is implemented with Canonical Signed Digit (CSD) logic.
|Input Word Length||Output Magnitude Word Length|
|Input Word Length||Output Angle Word Length|
The CORDIC logic at each pipeline stage implements one iteration. For each pipeline stage, the shift and angle rotation are constants.
When you set
object does not generate HDL code for the angle accumulation and quadrant correction
This format normalizes the fixed-point radian angle values around the unit circle. This is a more efficient use of bits than a range of [0,2π] radians. Normalized angle format also enables wraparound at 0/2π without additional detect and correct logic.
For example, representing the angle with 3 bits results in the following normalized values.
Using the mapping described in Modified CORDIC Algorithm, the object normalizes the angles across [0,π/4] and maps them to the correct octant at the end of the calculation.
The latency is NumIterations + 4 cycles from input to output. Each call to the object models one clock cycle.
When you set NumIterationsSource to
'Auto', the number of iterations is one less than the input word length
and the latency is three more than the input word length. If the data type of the input is
single, the number of iterations is 16 and
the latency is 20.
Performance was measured for the default configuration, with output scaling disabled and
fixdt(1,16,12) input. When the generated HDL code is synthesized into
Virtex®-6 (XC6VLX240T-1FFG1156) FPGA, the design achieves 260 MHz clock frequency. It
uses the following resources.
Xilinx LogiCORE® DSP48
|Block RAM (16K)||0|
Performance of the synthesized HDL code varies depending on your target and synthesis options.