Vectors Library
mikroPascal PRO for PIC32 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).
Library Routines
- Vector_Set16
- Vector_Set32
- Vector_Sub16
- Vector_Sub32
- Vector_Negate16
- Vector_Negate32
- Vector_Mul16
- Vector_Mul32
- Vector_MulC16
- Vector_MulC32
- Vector_Min16
- Vector_Min32
- Vector_Max16
- Vector_Max32
- Vector_Dotp16
- Vector_Dotp32
- Vector_Correlate16
- Vector_Correlate32
- Vector_Convolve16
- Vector_Convolve32
- Vector_Add16
- Vector_Add32
- Vector_AddC16
- Vector_AddC32
- Vector_Abs16
- Vector_Abs32
- Vector_Sum_Squares16
- Vector_Sum_Squares32
Vector_Set16
| Prototype |
procedure Vector_Set16(indata : ^integer; size : word; value : integer); |
|---|---|
| Description |
Sets |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Set32
| Prototype |
procedure Vector_Set32(indata : ^longint; size : word; value : longint); |
|---|---|
| Description |
Sets |
| Parameters |
|
| 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 outdata[n] = indata1[n] - indata2[n], n Î [0, N-1] |
| Parameters |
|
| 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 outdata[n] = indata1[n] - indata2[n], n Î [0, N-1] |
| Parameters |
|
| 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 |
|
| 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 |
|
| 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. outdata[n] = indata1[n] * indata2[n], n Î [0, N-1] |
| Parameters |
|
| 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. outdata[n] = indata1[n] * indata2[n], n Î [0, N-1] |
| Parameters |
|
| 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 |
|
| 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 |
|
| 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] |
| Parameters |
|
| Returns |
Minimum value ( |
| 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] |
| Parameters |
|
| Returns |
Minimum value ( |
| 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] |
| Parameters |
|
| Returns |
Maximum value ( |
| 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] |
| Parameters |
|
| Returns |
Maximum value ( |
| 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 |
|
| 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 |
|
| 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).
|
| Parameters |
|
| 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).
|
| Parameters |
|
| 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.
|
| Parameters |
|
| 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.
|
| Parameters |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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.
What do you think about this topic ? Send us feedback!



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