Vectors Library
mikroBasic 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 |
sub procedure Vector_Set16(dim indata as ^integer, dim size as word, dim value as integer) |
|---|---|
| Description |
Sets |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Set32
| Prototype |
sub procedure Vector_Set32(dim indata as ^longint, dim size as word, dim value as longint) |
|---|---|
| Description |
Sets |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Sub16
| Prototype |
sub procedure Vector_Sub16(dim outdata as ^integer, dim indata1 as ^integer, dim indata2 as ^integer, dim N as 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 |
sub procedure Vector_Sub32(dim outdata as ^longint, dim indata1 as ^longint, dim indata2 as ^longint, dim N as 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 |
sub procedure Vector_Negate16(dim outdata as ^integer, dim indata as ^integer, dim N as 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 |
sub procedure Vector_Negate32(dim outdata as ^longint, dim indata as ^longint, dim N as 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 |
sub procedure Vector_Mul16(dim outdata as ^integer, dim indata1 as ^integer, dim indata2 as ^integer, dim N as 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 |
sub procedure Vector_Mul32(dim outdata as ^longint, dim indata1 as ^longint, dim indata2 as ^longint, dim N as 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 |
sub procedure Vector_MulC16(dim outdata as ^integer, dim indata as ^integer, dim c as integer, dim N as 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 |
sub procedure Vector_MulC32(dim outdata as ^longint, dim indata as ^longint, dim c as longint, dim N as 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 |
sub function Vector_Min16(dim indata as ^integer, dim N as word, dim byref MinIndex as word) as 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 |
sub function Vector_Min32(dim indata as ^longint, dim N as word, dim byref MinIndex as word) as 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 |
sub function Vector_Max16(dim indata as ^integer, dim N as word, dim byref MaxIndex as word) as 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 |
sub function Vector_Max32(dim indata as ^longint, dim N as word, dim byref MinIndex as word) as 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 |
sub function Vector_Dotp16(dim indata1 as ^integer, dim indata2 as ^integer, dim N as word, dim scale as word) as 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 |
sub function Vector_Dotp32(dim indata1 as ^longint, dim indata2 as ^longint, dim N as word, dim scale as word) as 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 |
sub procedure Vector_Correlate16(dim outdata as ^integer, dim indata1 as ^integer, dim N1 as word, dim indata2 as ^integer, dim N2 as word) |
|---|---|
| Description |
Function calculates Vector correlation (using convolution).
|
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Correlate32
| Prototype |
sub procedure Vector_Correlate32(dim outdata as ^longint, dim indata1 as ^longint, dim N1 as word, dim indata2 as ^longint, dim N2 as word) |
|---|---|
| Description |
Function calculates Vector correlation (using convolution).
|
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Convolve16
| Prototype |
sub procedure Vector_Convolve16(dim outdata as ^integer, dim indata1 as ^integer, dim N1 as word, dim indata2 as ^integer, dim N2 as word) |
|---|---|
| Description |
Function calculates Vector using convolution.
|
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Convolve32
| Prototype |
sub procedure Vector_Convolve32(dim outdata as ^longint, dim indata1 as ^longint, dim N1 as word, dim indata2 as ^longint, dim N2 as word) |
|---|---|
| Description |
Function calculates Vector using convolution.
|
| Parameters |
|
| Returns |
Nothing. |
| Requires |
Nothing. |
| Notes |
None. |
Vector_Add16
| Prototype |
sub procedure Vector_Add16(dim outdata as ^integer, dim indata1 as ^integer, dim indata2 as ^integer, dim N as 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 |
sub procedure Vector_Add32(dim outdata as ^longint, dim indata1 as ^longint, dim indata2 as ^longint, dim N as 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 |
sub procedure Vector_AddC16(dim outdata as ^integer, dim indata1 as ^integer, dim c as integer, dim N as 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 |
sub procedure Vector_AddC16(dim outdata as ^longint, dim indata1 as ^longint, dim c as longint, dim N as 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 |
sub procedure Vector_Abs16(dim outdata as ^integer, dim indata as ^integer, dim N as 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 |
sub procedure Vector_Abs32(dim outdata as ^longint, dim indata as ^longint, dim N as 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 |
sub function Vector_Sum_Squares16(dim indata as ^integer, dim N as word, dim scale as word) as 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 |
<sub function Vector_Sum_Squares32(dim indata as ^longint, dim N as word, dim scale as word) as 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
dim Q15_1 as integer[4]
Q15_2 as integer[4]
Q31_1 as longint[4]
Q31_2 as longint[4]
Q15_Out as integer[7]
Q31_Out as longint[7]
i as integer
l as longint
scale, index, N as word
sub procedure Init()
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 sub
main:
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)
asm nop end asm
end.
What do you think about this topic ? Send us feedback!



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