Main Content

This example shows how to implement a hardware-efficient QR decomposition using the Complex Burst QR Decomposition block.

The Complex Burst QR Decomposition block performs the first step of solving the least-squares matrix equation AX = B which transforms A in-place to R and B in-place to C = Q'B, then solves the transformed system RX = C, where QR is the orthogonal-triangular decomposition of A.

To compute the stand-alone QR decomposition, this example sets B to be the identity matrix so that the output of the Complex Burst QR Decomposition block is the upper-triangular R and C = Q'.

Specify the number of rows in matrices A and B, the number of columns in matrix A, and the number of columns in matrix B. This example sets B to be the identity matrix the same size as the number of rows of A.

m = 10; % Number of rows in matrices A and B n = 3; % Number of columns in matrix A p = m; % Number of columns in matrix B

Use the helper function `complexUniformRandomArray`

to generate a random matrix A such that the real and imaginary parts of the elements of A are between -1 and +1, and A is full rank. Matrix B is the identity matrix.

```
rng('default')
A = fixed.example.complexUniformRandomArray(-1,1,m,n);
B = eye(m);
```

Use the helper function `qrFixedpointTypes`

to select fixed-point data types for matrices A and B that guarantee no overflow will occur in the transformation of A in-place to R and B in-place to C = Q'B.

The real and imaginary parts of the elements of A are between -1 and 1, so the maximum possible absolute value of any element is sqrt(2).

max_abs_A = sqrt(2); % Upper bound on max(abs(A(:)) max_abs_B = 1; % Upper bound on max(abs(B(:)) precisionBits = 24; % Number of bits of precision T = fixed.qrFixedpointTypes(m,max_abs_A,max_abs_B,precisionBits); A = cast(A,'like',T.A); B = complex(cast(B,'like',T.B));

```
model = 'ComplexBurstQRModel';
open_system(model);
```

The Data Handler subsystem in this model takes complex matrices A and B as inputs. The `ready`

port triggers the Data Handler. After sending a true `validIn`

signal, there may be some delay before `ready`

is set to false. When the Data Handler detects the leading edge of the `ready`

signal, the block sets `validIn`

to true and sends the next row of A and B. This protocol allows data to be sent whenever a leading edge of the `ready`

signal is detected, ensuring that all data is processed.

Use the helper function `setModelWorkspace`

to add the variables defined above to the model workspace. These variables correspond to the block parameters for the Complex Burst QR Decomposition block.

numSamples = 1; % Number of sample matrices fixed.example.setModelWorkspace(model,'A',A,'B',B,'m',m,'n',n,'p',p,... 'numSamples',numSamples);

out = sim(model);

The Complex Burst QR Decomposition block outputs data one row at a time. When a result row is output, the block sets `validOut`

to true. The rows of matrices R and C are output in reverse order to accommodate back-substitution, so you must reconstruct the data to interpret the results. To reconstruct the matrices R and C from the output data, use the helper function `qrModelOutputToArray`

.

[C,R] = fixed.example.qrModelOutputToArray(out.C,out.R,m,n,p,numSamples);

The block computes C = Q'B. In this example, B is the identity matrix, so Q = C' is the economy-size orthogonal factor of the QR decomposition.

Q = C';

Q is orothogonal, so Q'Q is the identity matrix within roundoff.

I = Q'*Q

I = 1.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 - 0.0000i -0.0000 + 0.0000i 1.0000 + 0.0000i -0.0000 - 0.0000i -0.0000 + 0.0000i -0.0000 + 0.0000i 1.0000 + 0.0000i DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 60 FractionLength: 48

R is an upper-triangular matrix.

R

R = 3.1655 + 0.0000i 0.4870 + 1.1980i 0.1466 - 0.9092i 0.0000 + 0.0000i 2.2184 + 0.0000i -0.2159 - 0.0972i 0.0000 + 0.0000i 0.0000 + 0.0000i 2.2903 + 0.0000i DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 29 FractionLength: 24

isequal(R,triu(R))

ans = logical 1

To evaluate the accuracy of the Complex Burst QR Decomposition block, compute the relative error.

relative_error = norm(double(Q*R - A))/norm(double(A))

relative_error = 1.7960e-06

Suppress mlint warnings.

```
%#ok<*NOPTS>
```