Vectors Library

mikroPascal PRO for ARM includes a library for working and using vectors. Routines with 16 as their suffix work with 16-bit data (fractional Q15 format) and routines with 32 as their suffix work with 32-bit data (fractional Q31 format).

  Note :

When using Cortex M4 MCUs, please have in mind that arrays of 16-bit (half-word) type that represent vectors must be allocated on the word-aligned addresses.

Library Routines

Vector_Set16

Prototype

procedure Vector_Set16(indata : ^integer; size : word; value : integer);

Description

Sets size elements of input to value, starting from the first element.

Parameters
  • input: pointer to original vector.
  • size: number of vector elements.
  • value: value written to the elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Set32

Prototype

procedure Vector_Set32(indata : ^longint; size : word; value : longint);

Description

Sets size elements of input to value, starting from the first element.

Parameters
  • input: pointer to original vector.
  • size: number of vector elements.
  • value: value written to the elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Sub16

Prototype

procedure Vector_Sub16(outdata : ^integer; indata1 : ^integer; indata2 : ^integer; N : word);

Description

This function does subtraction of two vectors - subtracts each element of indata2 vector from the corresponding element of indata1 vector. The number of samples to process is given by the parameter N.

outdata[n] = indata1[n] - indata2[n], n Î [0, N-1]

Parameters
  • outdata: result vector - output array of 16-bit fixed-point elements in Q15 format.
  • indata1: first vector - first input array with 16-bit fixed-point elements in Q15 format.
  • indata2: second vector - second input array with 16-bit fixed-point elements in Q15 format.
  • N: Number of samples - must be less or equal to minimum size of two vectors.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Sub32

Prototype

procedure Vector_Sub32(outdata : ^longint; indata1 : ^longint; indata2 : ^longint; N : word);

Description

This function does subtraction of two vectors - subtracts each element of indata2 vector from the corresponding element of indata1 vector. The number of samples to process is given by the parameter N.

outdata[n] = indata1[n] - indata2[n], n Î [0, N-1]

Parameters
  • outdata: result vector - output array of 32-bit fixed-point elements in Q31 format.
  • indata1: first vector - first input array with 32-bit fixed-point elements in Q31 format.
  • indata2: second vector - second input array with 32-bit fixed-point elements in Q31 format.
  • N: Number of samples - must be less or equal to minimum size of two vectors.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Negate16

Prototype

procedure Vector_Negate16(outdata : ^integer; indata: ^integer; N : word);

Description

This function does negation of vector.

outdata[n] = (-1)*indata[n] + 0, n Î [0, N)

Parameters
  • outdata: pointer to result vector.
  • indata: pointer to original vector.
  • N: number of elements in vector(s).
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Negate32

Prototype

procedure Vector_Negate32(outdata : ^longint; indata: ^longint; N : word);

Description

This function does negation of vector.

outdata[n] = (-1)*indata[n] + 0, n Î [0, N)

Parameters
  • outdata: pointer to result vector.
  • indata: pointer to original vector.
  • N: number of elements in vector(s).
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Mul16

Prototype

procedure Vector_Mul16( outdata : ^integer; indata1 : ^integer; indata2 : ^integer; N : word);

Description

This function does multiplication of two vectors. Multiplies each Q15 element of indata1 by the corresponding element of indata2 and stores the results to outdata.
The number of samples to process is given by the parameter N.

outdata[n] = indata1[n] * indata2[n], n Î [0, N-1]

Parameters
  • outdata: pointer to output vector - array of 16-bit fixed-point elements in Q15 format.
  • indata1: pointer to first vector - array with 16-bit fixed-point elements in Q15 format.
  • indata2: pointer to second vector - array with 16-bit fixed-point elements in Q15 format.
  • N: number of samples in vector(s) (must be less or equal to minimum size of two vectors)
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Mul32

Prototype

procedure Vector_Mul32( outdata : ^longint; indata1 : ^longint; indata2 : ^longint; N : word);

Description

This function does multiplication of two vectors. Multiplies each Q31 element of indata1 by the corresponding element of indata2 and stores the results to outdata.
The number of samples to process is given by the parameter N.

outdata[n] = indata1[n] * indata2[n], n Î [0, N-1]

Parameters
  • outdata: pointer to output vector - array of 32-bit fixed-point elements in Q31 format.
  • indata1: pointer to first vector - array with 32-bit fixed-point elements in Q31 format.
  • indata2: pointer to second vector - array with 32-bit fixed-point elements in Q31 format.
  • N: number of samples in vector(s) (must be less or equal to minimum size of two vectors)
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_MulC16

Prototype

procedure Vector_MulC16( outdata : ^integer; indata : ^integer; c : integer; N : word);

Description

Multiplies each Q15 element of indata by the Q15 constant c and stores the results to outdata. The number of samples to process is given by the parameter N.

outdata[n] = indata1[n] * c, n Î [0, N-1]

Parameters
  • outdata: pointer to output vector - array of 16-bit fixed-point elements in Q15 format.
  • indata1: pointer to input vector - array with 16-bit fixed-point elements in Q15 format.
  • c: 15-bit fixed-point constant.
  • N: number of samples in vector(s) (must be less or equal to minimum size of the input vector).
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_MulC32

Prototype

procedure Vector_MulC32( outdata : ^longint; indata : ^longint; c : longint; N : word);

Description

Multiplies each Q31 element of indata by the Q31 constant c and stores the results to outdata. The number of samples to process is given by the parameter N.

outdata[n] = indata1[n] * c[n], n Î [0, N-1]

Parameters
  • outdata: pointer to output vector - array of 32-bit fixed-point elements in Q31 format.
  • indata1: pointer to input vector - array with 32-bit fixed-point elements in Q31 format.
  • c: 32-bit fixed-point constant.
  • N: number of samples in vector(s) (must be less or equal to minimum size of the input vector).
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Min16

Prototype

function Vector_Min16(indata : ^integer; N : word; MinIndex : ^word) : integer;

Description

This function finds minimal value in vector.

minVal = min (indata[n]), n Î [0, N-1]

If indata[i] = indata[j] = minVal, and i < j, then MinIndex = j.

Parameters
  • indata: pointer to original vector.
  • N: number of elements in vector.
  • MinIndex: pointer to index of minimum value.
Returns

Minimum value (minVal).

Requires

Nothing.

Notes

None.

Vector_Min32

Prototype

function Vector_Min32(indata : ^longint; N : word; MinIndex : ^word) : longint;

Description

This function finds minimal value in vector.

minVal = min (indata[n]), n Î [0, N-1]

If indata[i] = indata[j] = minVal, and i < j, then MinIndex = j.

Parameters
  • indata: pointer to original vector.
  • N: number of elements in vector.
  • MinIndex: pointer to index of minimum value.
Returns

Minimum value (minVal).

Requires

Nothing.

Notes

None.

Vector_Max16

Prototype

function Vector_Max16(indata : ^integer; N : word; MaxIndex : ^word) : integer;

Description

This function find maximal value in vector.

maxVal = max (indata[n]), n Î [0, N-1]

If indata[i] = indata[j] = maxVal, and i < j, then MaxIndex = j.

Parameters
  • indata: pointer to original vector.
  • N: number of elements in vector(s).
  • MaxIndex: pointer to index of maximum value.
Returns

Maximum value (maxVal).

Requires

Nothing.

Notes

None.

Vector_Max32

Prototype

function Vector_Max32(indata : ^longint; N : word; MinIndex : ^word) : longint;

Description

This function find maximal value in vector.

maxVal = max (indata[n]), n Î [0, N-1]

If indata[i] = indata[j] = maxVal, and i < j, then MaxIndex = j.

Parameters
  • indata: pointer to original vector.
  • N: number of elements in vector(s).
  • MaxIndex: pointer to index of maximum value.
Returns

Maximum value (maxVal).

Requires

Nothing.

Notes

None.

Vector_Dotp16

Prototype

function Vector_Dotp16(indata1 : ^integer; indata2 : ^integer; N : word; scale :word) : integer;

Description

Computes the dot product of the Q15 vectors indata1 and indata2. The number of samples to process is given by the parameter N. The scale parameter specifies the amount of right shift applied to the final result.

Parameters
  • indata1: pointer to first vector - array with 16-bit fixed point elements in Q15 format.
  • indata2: pointer to second vector - array with 16-bit fixed point elements in Q15 format.
  • N: number of samples in vector(s).
  • scale: Scaling factor: divide the result by 2scale.
Returns

Scaled result of the calculation in fractional Q15 format :

Requires

Nothing.

Notes

None.

Vector_Dotp32

Prototype

function Vector_Dotp32(indata1 : ^longint; indata2 : ^longint; N : word; scale :word) : longint;

Description

Computes the dot product of the Q31 vectors indata1 and indata2. The number of samples to process is given by the parameter N. The scale parameter specifies the amount of right shift applied to the final result.

Parameters
  • indata1: pointer to first vector - array with 32-bit fixed point elements in Q31 format.
  • indata2: pointer to second vector - array with 32-bit fixed point elements in Q31 format.
  • N: number of samples in vector(s).
  • scale: Scaling factor: divide the result by 2scale.
Returns

Scaled result of the calculation in fractional Q31 format :

Requires

Nothing.

Notes

None.

Vector_Correlate16

Prototype

procedure Vector_Correlate16(outdata : ^integer; indata1 : ^integer; N1 : word; indata2 : ^integer; N2 : word);

Description

Function calculates Vector correlation (using convolution).


where:
x[n] defined for n Î [0, N)
y[n] defined for n Î [0, M), M £ N
r[n] defined for n Î [0, N+M-1)

Parameters
  • outdata: pointer to result vector - array with 16-bit fixed point elements in Q15 format.
  • indata1: pointer to first vector - array with 16-bit fixed point elements in Q15 format.
  • N1: number of the first vector elements.
  • indata2: pointer to second vector - array with 16-bit fixed point elements in Q15 format.
  • N2: number of the second vector elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Correlate32

Prototype

procedure Vector_Correlate32(outdata : ^longint; indata1 : ^longint; N1 : word; indata2 : ^longint; N2 : word);

Description

Function calculates Vector correlation (using convolution).


where:
x[n] defined for n Î [0, N)
y[n] defined for n Î [0, M), M £ N
r[n] defined for n Î [0, N+M-1)

Parameters
  • outdata: pointer to result vector - array with 32-bit fixed point elements in Q31 format.
  • indata1: pointer to first vector - array with 32-bit fixed point elements in Q31 format.
  • N1: number of the first vector elements.
  • indata2: pointer to second vector - array with 32-bit fixed point elements in Q31 format.
  • N2: number of the second vector elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Convolve16

Prototype

procedure Vector_Convolve16(outdata : ^integer; indata1 : ^integer; N1 : word; indata2 : ^integer; N2 : word);

Description

Function calculates Vector using convolution.

, n Î [0, M)
, n Î [M, N)
, n Î [N, N+M-1)

Parameters
  • outdata: pointer to result vector - array with 16-bit fixed point elements in Q15 format.
  • indata1: pointer to first vector - array with 16-bit fixed point elements in Q15 format.
  • N1: number of the first vector elements.
  • indata2: pointer to second vector - array with 16-bit fixed point elements in Q15 format.
  • N2: number of the second vector elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Convolve32

Prototype

procedure Vector_Convolve32(outdata : ^longint; indata1 : ^longint; N1 : word; indata2 : ^longint; N2 : word);

Description

Function calculates Vector using convolution.

, n Î [0, M)
, n Î [M, N)
, n Î [N, N+M-1)

Parameters
  • outdata: pointer to result vector - array with 32-bit fixed point elements in Q31 format.
  • indata1: pointer to first vector - array with 32-bit fixed point elements in Q15 format.
  • N1: number of the first vector elements.
  • indata2: pointer to second vector - array with 32-bit fixed point elements in Q31 format.
  • N2: number of the second vector elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Add16

Prototype

procedure Vector_Add16(outdata : ^integer; indata1 : ^integer; indata2 : ^integer; N : word);

Description

Function calculates vector addition.

outdata[n] = indata1[n] + indata2[n], n Î [0, N-1]

Parameters
  • outdata: pointer to result vector
  • indata1: pointer to first vector
  • indata2: pointer to second vector
  • N: number of vector(s) elements
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Add32

Prototype

procedure Vector_Add32(outdata : ^longint; indata1 : ^longint; indata2 : ^longint; N : word);

Description

Function calculates vector addition.

outdata[n] = indata1[n] + indata2[n], n Î [0, N-1]

Parameters
  • outdata: pointer to result vector
  • indata1: pointer to first vector
  • indata2: pointer to second vector
  • N: number of vector(s) elements
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_AddC16

Prototype

procedure Vector_AddC16(outdata : ^integer; indata1 : ^integer; c : integer; N : word);

Description

Function adds the Q15 constant c to all elements of indata. The number of samples to process is given by the parameter N.

outdata[n] = indata[n] + C, n Î [0, N-1]

Parameters
  • outdata: pointer to result vector - array of 16-bit fixed-point elements in Q15 format.
  • indata: pointer to input vector - array of 16-bit fixed-point elements in Q15 format.
  • c: constant added to all elements of the vector.
  • N: number of vector(s) elements
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_AddC32

Prototype

procedure Vector_AddC16(outdata : ^longint; indata1 : ^longint; c : longint; N : word);

Description

Function adds the Q31 constant c to all elements of indata. The number of samples to process is given by the parameter N.

outdata[n] = indata[n] + C, n Î [0, N-1]

Parameters
  • outdata: pointer to result vector - array of 32-bit fixed-point elements in Q31 format.
  • indata: pointer to input vector - array of 32-bit fixed-point elements in Q31 format.
  • c: constant added to all elements of the vector.
  • N: number of vector(s) elements
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Abs16

Prototype

procedure Vector_Abs16(outdata : ^integer; indata : ^integer; N : word);

Description

Computes the absolute value of each element of indata and stores it to outdata. The number of samples to process is given by the parameter N.

outdata[n] = abs(indata[n]), n Î [0, N-1]

Parameters
  • outdata: pointer to result vector - array of 16-bit fixed-point elements in Q15 format.
  • indata: pointer to input vector - array of 16-bit fixed-point elements in Q15 format.
  • N: number of vector(s) elements.
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Abs32

Prototype

procedure Vector_Abs32(outdata : ^longint; indata : ^longint; N : word);

Description

Computes the absolute value of each element of indata and stores it to outdata. The number of samples to process is given by the parameter N.

outdata[n] = abs(indata[n]), n Î [0, N-1]

Parameters
  • outdata: pointer to result vector - array of 32-bit fixed-point elements in Q31 format.
  • indata: pointer to input vector - array of 32-bit fixed-point elements in Q31 format.
  • N: number of vector(s) elements
Returns

Nothing.

Requires

Nothing.

Notes

None.

Vector_Sum_Squares16

Prototype

function Vector_Sum_Squares16(indata : ^integer; N : word; scale : word) : integer;

Description

Computes the sum of squared values of all elements of indata. The number of samples to process is given by the parameter N. The scale parameter specifies the amount of right shift applied to the final result.

Parameters
  • indata: pointer to input vector - array of 16-bit fixed-point elements in Q15 format.
  • N: number of vector(s) elements.
  • scale: Scaling factor: divide the result by 2scale.
Returns

Scaled result of the calculation in fractional Q15 format.

Requires

Nothing.

Notes

None.

Vector_Sum_Squares32

Prototype

<function Vector_Sum_Squares32(indata : ^longint; N : word; scale : word) : longint;

Description

Computes the sum of squared values of all elements of indata. The number of samples to process is given by the parameter N. The scale parameter specifies the amount of right shift applied to the final result.

Parameters
  • indata: pointer to input vector - array of 32-bit fixed-point elements in Q31 format.
  • N: number of vector(s) elements.
  • scale: Scaling factor: divide the result by 2scale.
Returns

Scaled result of the calculation in fractional Q31 format.

Requires

Nothing.

Notes

None.

Library Example

program Vectors_Test;

var Q15_1   : array[4] of integer;
    Q15_2   : array[4] of integer;
    Q31_1   : array[4] of longint;
    Q31_2   : array[4] of longint;
    Q15_Out : array[7] of integer;
    Q31_Out : array[7] of longint;
    i       : integer;
    l       : longint;
    scale, index, N : word;

procedure Init();
  begin
  Q15_1[0] := integer(0xC000);        // -0.5
  Q15_1[1] := 0x2000;                 // 0.25
  Q15_1[2] := integer(0xF000);        // -0.125
  Q15_1[3] := 0x0800;                 // 0.0625
  Q15_2[0] := integer(0x999A);        // ~(-0.8)
  Q15_2[1] := 0x3333;                 // ~0.4
  Q15_2[2] := integer(0xE666);        // ~(-0.2)
  Q15_2[3] := 0x0CCD;                 // ~0.1
  
  Q31_1[0] := longint(0xC0000000);    // -0.5
  Q31_1[1] := 0x20000000;             // 0.25
  Q31_1[2] := longint(0xF0000000);    // -0.125
  Q31_1[3] := 0x08000000;             // 0.0625
  Q31_2[0] := longint(0x999A0000);    // ~(-0.8)
  Q31_2[1] := 0x33330000;             // ~0.4
  Q31_2[2] := longint(0xE6660000);    // ~(-0.2)
  Q31_2[3] := 0x0CCD0000;             // ~0.1

  end;
begin
  Init();
  N := 4;
  Vector_Abs16(@Q15_Out, @Q15_1, N);
  // Q15_Out = {0x4000, 0x2000, 0x1000, 0x0800}

  Vector_Add16(@Q15_Out, @Q15_1, @Q15_2, N);
  // Q15_Out = (0x599A, 0x5334, 0xD666, 0x14CD)
  // Q15_Out[0] = 0x5334 -> overflow

  Vector_AddC32(@Q31_Out, @Q31_1, longint(0xC0000000), N);
  // Q31_Out = (0x80000000, 0xE0000000, 0xB0000000, 0xC8000000)

  scale := 2;
  l := Vector_Dotp32(@Q31_1, @Q31_2, N, scale);
  // l = 1/(2^scale) * sum_k(Q31_1[k]*Q31_2[k]) = 0x10FFF400 <- 0.1328125

  Vector_Mul16(@Q15_Out, @Q15_1, @Q15_2, N);
  // Q15_Out = (0x3333, 0x0CCC, 0x0333, 0x00CC)

  Vector_Sub16(@Q15_Out, @Q15_1, @Q15_2, N);
  // Q15_Out = (0x2666, 0xECCD, 0x099A, 0xFB33)

  scale := 3;
  i := Vector_Sum_Squares16(@Q15_1, N, scale);
  // i = 1/(2^scale)*sum_K(Q15_1[k]^2) = 0x0550 <- 0.0415039

  Vector_Set16(@Q15_Out, N, 0x0A0A);
  // Q15_Out = (0x0A0A, 0x0A0A, 0x0A0A, 0x0A0A)

  i := Vector_Min16(@Q15_1, N, @index);
  // i = 0xC000; index = 0;

  l := Vector_Max32(@Q31_1, N, @index);
  // l = 0x20000000; index = 1;

  Vector_Negate16(@Q15_Out, @Q15_1, N);
  // Q15_Out = (0x4000, 0xE000, 0x1000, 0xF800

  Vector_Correlate32(@Q31_Out, @Q31_1, 3, @Q31_2, 2);
  // Q31_Out = (0x0CCCC000, 0xE0002000, Ox3FFFC000, 0xE6668000)


  Vector_Convolve16(@Q15_Out, @Q15_1, 4, @Q15_2, 3);
  // Q15_Out = (0x333, 0xCCCD, 0x2666, 0xECCC, 0x0666, 0xFE66)
end.