CMSIS-DSP  
CMSIS DSP Software Library
 
Loading...
Searching...
No Matches
Vector operation example

To compute:

\[ \overrightarrow{d} = \overrightarrow{a} + \overrightarrow{b} * \overrightarrow{c} \]

we need to:

  1. Include the right header files
  2. allocate the vectors
  3. initialize the vectors
  4. make the computation.

Include the headers

The headers are not yet part of the CMSIS-DSP packs since they are experimental. You can get them from the CMSIS-DSP github

#include <dsppp/memory_pool>
#include <dsppp/matrix>
using namespace arm_cmsis_dsp;

If fixed point datatypes are required, #include <dsppp/fixed_point> should be used before <dsppp/matrix>

Fixed point requires the use of CMSIS-DSP.

Creation of the vectors

To create a vector a you would write:

Vector<float32_t,NB> is creating a vector of dimension NB (known at build time) and datatype float32_t. This creation is requiring some memory allocation and by default it is done with a malloc.

It is possible to change the memory allocator for the vectors (and it is advised) to avoid using malloc and instead have deterministic allocation without fragmentation.

See section Memory allocation.

Vectors of different dimensions are considered as being different types.

If you don't know the dimension at build time, you can use a different type of vector with:

For the trade-off between vector with build time dimension or runtime dimension please see the section Static / dynamic .

Initialization of the vectors

You can index the vectors as normal C arrays.

for(int i = 0;i< NB;i++)
{
a[i] = b[i] = c[i] = i;
}

Computation

The computation can be written normally as :

Vector<float32_t,NB> d = a + b * c;

Note that the computation can be parametrized with template arguments so the same computation could be used with any datatype or length. In that case you would have to define a template (and not just a normal function) and inside you would use something like:

Vector<T,NB> d = a + b * c;

where T is a type variable coming from the template.

The operators +, * are computed in one pass with one loop : we have loop fusion and instead of having a loop per operator we have a loop for the whole computation.

To understand fusion and how to extend it with new operators, see section Fusion .

For an overview of vector operators, see section Vector . For an overview of matrix operators, see section Matrix .

Displaying the result

The vectors can be displayed on stdout for debug purpose.

std::cout << "Result = " << d ;