# dsp.HDLComplexToMagnitudeAngle

Magnitude and phase angle of complex signal—optimized for HDL code generation

## Description

The `dsp.HDLComplexToMagnitudeAngle` System 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:

1. Create the `dsp.HDLComplexToMagnitudeAngle` object and set its properties.

2. Call the object with arguments, as if it were a function.

## Creation

### Syntax

``HCMA = dsp.HDLComplexToMagnitudeAngle``
``HCMA = dsp.HDLComplexToMagnitudeAngle(Name,Value)``

### Description

````HCMA = dsp.HDLComplexToMagnitudeAngle` returns a `dsp.HDLComplexToMagnitudeAngle` System object, `HCMA`, that computes the magnitude and phase angle of a complex input signal.```

example

````HCMA = dsp.HDLComplexToMagnitudeAngle(Name,Value)` sets properties of `HCMA` using one or more name-value pairs. Enclose each property name in single quotes. Example: ```cma = dsp.HDLComplexToMagnitudeAngle('AngleFormat','Radians')``````

## Properties

expand all

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.

Type of output values to return, specified as ```'Magnitude and angle'```, `'Magnitude'`, or `'Angle'`. You can choose for the object to return the magnitude of the input signal, or the phase angle of the input signal, or both.

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 [π,−π].

Scale output by the inverse of the CORDIC gain factor, specified as `true` or `false`.

Note

If your design includes a gain factor later in the datapath, you can set `ScaleOutput` to `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.

Source of the `NumIterations` property for the CORDIC algorithm, specified as:

• `'Auto'` — Sets the number of iterations to one less than the input word length. If the input is `double` or `single`, the number of iterations is 16.

• `'Property'` — Uses the `NumIterations` property.

For details of the CORDIC algorithm, see Algorithm.

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.

#### Dependencies

To enable this property, set `NumIterationsSource` to `'Property'`.

## Usage

### Syntax

``````[mag,angle,validOut] = HCMA(X,validIn)``````
``````[mag,validOut] = HCMA(X,validIn)``````
``````[angle,validOut] = HCMA(X,validIn)``````

### Description

example

``````[mag,angle,validOut] = HCMA(X,validIn)``` converts a scalar or vector of complex values `X` into their component magnitude and phase angles. `validIn` and `validOut` are logical scalars that indicate the validity of the input and output signals, respectively.```
``````[mag,validOut] = HCMA(X,validIn)``` returns only the component magnitudes of `X`. To use this syntax, set OutputValue to `'Magnitude'`. Example: ```HCMA = dsp.HDLComplextoMagnitudeAngle('OutputValue','Magnitude');``````
``````[angle,validOut] = HCMA(X,validIn)``` returns only the component phase angles of `X`.To use this syntax, set OutputValue to `'Angle'`. Example: ```HCMA = dsp.HDLComplextoMagnitudeAngle('OutputValue','Angle');``````

### Input Arguments

expand all

Input signal, specified as a scalar, a column vector representing samples in time, or a row vector representing channels. Using vector input increases data throughput while using more hardware resources. The object implements the conversion logic in parallel for each element of the vector. The input vector can contain up to 64 elements.

`double` and `single` data types are supported for simulation, but not for HDL code generation.

Data Types: `fi` | `int8` | `int16` | `int32` | `uint8` | `uint16` | `uint32` | `single` | `double`
Complex Number Support: Yes

When `validIn` is `true`, the block captures the data from the `dataIn` input argument. The `validIn` argument applies to all samples in a vector input argument.

Data Types: `logical`

### Output Arguments

expand all

Magnitude calculated from the complex input signal, returned as a scalar, a column vector representing samples in time, or a row vector representing channels. The dimensions and data type of this argument match the dimensions of the `dataIn` argument.

#### Dependencies

To enable this argument, set the `OutputValue` property to `'Magnitude and Angle'` or `'Magnitude'`.

Angle calculated from the complex input signal, returned as a scalar, a column vector representing samples in time, or a row vector representing channels. The dimensions and data type of this argument match the dimensions of the `dataIn` argument. The format of this value depends on the AngleFormat property.

#### Dependencies

To enable this argument, set the `OutputValue` property to `'Magnitude and Angle'` or `'Angle'`.

The object sets `validOut` to `true` with each valid data returned on the `magnitude` or `angle` output arguments. The `validOut` argument applies to all samples in a vector output argument.

Data Types: `logical`

## Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named `obj`, use this syntax:

`release(obj)`

expand all

 `step` Run System object algorithm `release` Release resources and allow changes to System object property values and input characteristics `reset` Reset internal states of System object

## Examples

collapse all

Use the `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 `myObject(x)` with `step(myObject,x)`.

```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 `inputWL+3`.

```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') ```

## Algorithms

expand all

### Functions

Introduced in R2014b

Watch now