Vectors Library
mikroC 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 :
  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
- 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 | void Vector_Set16(int *indata, unsigned size, int value); | 
|---|---|
| Description | Sets  | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
int vec2[3] = {1,1,1};
Vector_Set16(vec2, 3, 0x4000);
 | 
| Notes | None. | 
Vector_Set32
| Prototype | void Vector_Set32(long *indata, unsigned size, long value); | 
|---|---|
| Description | Sets  | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
long vec2[3] = {1,1,1};
Vector_Set32(vec2, 3, 0x4000);
 | 
| Notes | None. | 
Vector_Sub16
| Prototype | void Vector_Sub16(int *outdata, int *indata1, unsigned *indata2, unsigned N); | 
|---|---|
| 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. | 
| Example | 
unsigned vec1[3] = {1,2,3};
unsigned vec2[3] = {1,1,1};
unsigned vecDest[3];
Vector_Sub16(vecDest, vec1, vec2, 3);
 | 
| Notes | None. | 
Vector_Sub32
| Prototype | void Vector_Sub32(long *outdata, long *indata1, long *indata2, unsigned N); | 
|---|---|
| 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. | 
| Example | 
unsigned vec1[3] = {1,2,3};
unsigned vec2[3] = {1,1,1};
unsigned vecDest[3];
Vector_Sub32(vecDest, vec1, vec2, 3);
 | 
| Notes | None. | 
Vector_Negate16
| Prototype | void Vector_Negate16(int *outdata, int *indata, unsigned N); | 
|---|---|
| Description | This function does negation of vector. outdata[n] = (-1)*indata[n] + 0, n Î [0, N) | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
int vecDest[3];
int vec1[3] = {1,2,3};
Vector_Negate16(vecDest, vec1, 3);
 | 
| Notes | None. | 
Vector_Negate32
| Prototype | void Vector_Negate32(long *outdata, long *indata, unsigned N); | 
|---|---|
| Description | This function does negation of vector. outdata[n] = (-1)*indata[n] + 0, n Î [0, N) | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
long vecDest[3];
long vec1[3] = {1,2,3};
Vector_Negate32(vecDest, vec1, 3);
 | 
| Notes | None. | 
Vector_Mul16
| Prototype | void Vector_Mul16(int *outdata, int *indata1, int *indata2, unsigned N); | 
|---|---|
| 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. | 
| Example | 
int vec1[3] = {1,2,3};
int vec2[3] = {1,1,1};
int vConDest[10];
Vector_Mul16(vConDest vec1, vec2, 3); 
 | 
| Notes | None. | 
Vector_Mul32
| Prototype | void Vector_Mul32(long *outdata, long *indata1, long *indata2, unsigned N); | 
|---|---|
| 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. | 
| Example | 
long vec1[3] = {1,2,3};
long vec2[3] = {1,1,1};
long vConDest[10];
Vector_Mul32(vConDest, vec1, vec2, 3); 
 | 
| Notes | None. | 
Vector_MulC16
| Prototype | void Vector_MulC16(int *outdata, int *indata1, int c, unsigned N); | 
|---|---|
| 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. | 
| Example | 
const q15 c = 5;
int vec[3] = {1,2,3};
int vConDest[10];
Vector_MulC16(vConDest, vec, c, 3); 
 | 
| Notes | None. | 
Vector_MulC32
| Prototype | void Vector_MulC32(long *outdata, long *indata1, long c, unsigned N); | 
|---|---|
| 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. | 
| Example | 
const long c = 5;
long vec[3] = {1,2,3};
long vConDest[10];
Vector_MulC32(vConDest, vec, c, 3); 
 | 
| Notes | None. | 
Vector_Min16
| Prototype | int Vector_Min16(int *indata, unsigned N, unsigned *MinIndex); | 
|---|---|
| Description | This function finds minimal value in vector. minVal = min (indata[n]), n Î [0, N-1] | 
| Parameters | 
 | 
| Returns | Minimum value ( | 
| Requires | Nothing. | 
| Example | 
unsigned vec1[3] = {1,2,3};
unsigned index;
unsigned rslt;
rslt = Vector_Min16(vec1, 3, &index);
 | 
| Notes | None. | 
Vector_Min32
| Prototype | long Vector_Min32(long *indata, unsigned N, unsigned *MinIndex); | 
|---|---|
| Description | This function finds minimal value in vector. minVal = min (indata[n]), n Î [0, N-1] | 
| Parameters | 
 | 
| Returns | Minimum value ( | 
| Requires | Nothing. | 
| Example | 
long vec1[3] = {1,2,3};
unsigned index;
long rslt;
rslt = Vector_Min32(vec1, 3, &index);
 | 
| Notes | None. | 
Vector_Max16
| Prototype | int Vector_Max16(int *indata, unsigned N, unsigned *MaxIndex); | 
|---|---|
| Description | This function find maximal value in vector. maxVal = max (indata[n]), n Î [0, N-1] | 
| Parameters | 
 | 
| Returns | Maximum value ( | 
| Requires | Nothing. | 
| Example | 
int vec1[3] = {1,2,3};
unsigned index;
int rslt;
rslt = Vector_Max16(vec1, 3, &index);
 | 
| Notes | None. | 
Vector_Max32
| Prototype | long Vector_Max32(long *indata, unsigned N, unsigned *MaxIndex); | 
|---|---|
| Description | This function find maximal value in vector. maxVal = max (indata[n]), n Î [0, N-1] | 
| Parameters | 
 | 
| Returns | Maximum value ( | 
| Requires | Nothing. | 
| Example | 
long vec1[3] = {1,2,3};
unsigned index;
long rslt;
rslt = Vector_Max32(vec1, 3, &index);
 | 
| Notes | None. | 
Vector_Dotp16
| Prototype | int Vector_Dotp16(int *indata1, int *indata2, unsigned N, unsigned scale); | 
|---|---|
| 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. | 
| Example | 
int vec2[3] = {1,1,1};
int rslt;
q15 = Vector_Dotp16(vec2,vec2,3);
 | 
| Notes | None. | 
Vector_Dotp32
| Prototype | long Vector_Dotp32(long *indata1, long *indata2, unsigned N, unsigned scale); | 
|---|---|
| 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. | 
| Example | 
long vec2[3] = {1,1,1};
long rslt;
rslt = Vector_Dotp32(vec2,vec2,3);
 | 
| Notes | None. | 
Vector_Correlate16
| Prototype | void Vector_Correlate16(int *outdata, int *indata1, int N1, int *indata2, int N2); | 
|---|---|
| Description | Function calculates Vector correlation (using convolution). 
 | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
int vConDest[10];
int vec2[3] = {1,1,1};
Vector_Correlate16(vConDest, vec2, 3, vec2, 3); 
 | 
| Notes | None. | 
Vector_Correlate32
| Prototype | void Vector_Correlate32(long *outdata, long *indata1, long N1, long *indata2, int N2); | 
|---|---|
| Description | Function calculates Vector correlation (using convolution). 
 | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
long vConDest[10];
long vec2[3] = {1,1,1};
Vector_Correlate16(vConDest, vec2, 3, vec2, 3); 
 | 
| Notes | None. | 
Vector_Convolve16
| Prototype | void Vector_Convolve16(int *outdata, int *indata1, int N1, int indata2, int N2); | 
|---|---|
| Description | Function calculates Vector using convolution. 
 | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
int vec2[3] = {1,1,1};
int vConDest2[10];
Vector_Convolve16(vConDest2, vec2, 3, vec2, 3); 
 | 
| Notes | None. | 
Vector_Convolve32
| Prototype | void Vector_Convolve32(long *outdata, long *indata1, int N1, long *indata2, int N2); | 
|---|---|
| Description | Function calculates Vector using convolution. 
 | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
long vec2[3] = {1,1,1};
long vConDest2[10];
Vector_Convolve32(vConDest2, vec2, 3, vec2, 3); 
 | 
| Notes | None. | 
Vector_Add16
| Prototype | void Vector_Add16(int *outdata, int *indata1, int *indata2, unsigned N); | 
|---|---|
| Description | Function calculates vector addition. outdata[n] = indata1[n] + indata2[n], n Î [0, N-1] | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
int vec1[3] = {1,2,3};
int vec2[3] = {1,1,1};
int vecDest[3];
Vector_Add16(vecDest, vec1, vec2, 3);
 | 
| Notes | None. | 
Vector_Add32
| Prototype | void Vector_Add32(long *outdata, long *indata1, long *indata2, unsigned N); | 
|---|---|
| Description | Function calculates vector addition. outdata[n] = indata1[n] + indata2[n], n Î [0, N-1] | 
| Parameters | 
 | 
| Returns | Nothing. | 
| Requires | Nothing. | 
| Example | 
long vec1[3] = {1,2,3};
long vec2[3] = {1,1,1};
long vecDest[3];
Vector_Add32(vecDest, vec1, vec2, 3);
 | 
| Notes | None. | 
Vector_AddC16
| Prototype | void Vector_AddC16(int *outdata, int *indata1, int c, unsigned N); | 
|---|---|
| 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. | 
| Example | 
int vec1[3] = {1,2,3};
int c = 5;
int vecDest[3];
Vector_AddC16(vecDest, vec1, c, 3);
 | 
| Notes | None. | 
Vector_AddC32
| Prototype | void Vector_AddC32(long *outdata, long *indata1, long c, unsigned N); | 
|---|---|
| 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. | 
| Example | 
long vec1[3] = {1,2,3};
long c = 5;
long vecDest[3];
Vector_AddC32(vecDest, vec1, c, 3);
 | 
| Notes | None. | 
Vector_Abs16
| Prototype | void Vector_Abs16(int *outdata, int *indata1, unsigned N); | 
|---|---|
| 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. | 
| Example | 
int vec1[3] = {-1,2,-3};
int vecDest[3];
Vector_Abs16(vecDest, vec1, 3);
 | 
| Notes | None. | 
Vector_Abs32
| Prototype | void Vector_Abs32(long *outdata, long *indata, unsigned N); | 
|---|---|
| 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. | 
| Example | 
long vec1[3] = {-1,2,-3};
long vecDest[3];
Vector_Abs32(vecDest, vec1, 3);
 | 
| Notes | None. | 
Vector_Sum_Squares16
| Prototype | int Vector_Sum_Squares16(int *indata, unsigned N, unsigned scale); | 
|---|---|
| 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. | 
| Example | 
int vec1[3] = {-1,2,-3};
int res;
res = Vector_Sum_Squares16(vec1, 3, 1);
 | 
| Notes | None. | 
Vector_Sum_Squares32
| Prototype | long Vector_Sum_Squares32(long *indata, unsigned N, unsigned scale); | 
|---|---|
| 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. | 
| Example | 
long vec1[3] = {-1,2,-3};
long res;
res = Vector_Sum_Squares32(vec1, 3, 1);
 | 
| Notes | None. | 
Library Example
int Q15_1[4] = {0xC000, 0x2000, 0xF000, 0x0800};
//                -0.5;   0.25; -0.125; 0.0625;
int Q15_2[4] = {0x999A, 0x3333, 0xE666, 0x0CCD};
//             ~(-0.8);   ~0.4; ~(-0.2);  ~0.1;
long Q31_1[4] = {0xC0000000, 0x20000000, 0xF0000000, 0x08000000};
//                     -0.5;       0.25;     -0.125;     0.0625;
long Q31_2[4] = {0x999A0000, 0x33330000, 0xE6660000, 0x0CCD0000};
//                  ~(-0.8);       ~0.4;    ~(-0.2);       ~0.1;
int  Q15_Out[7], i, N;
long Q31_Out[7], l;
unsigned scale, index;
void main() {
  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, 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, 0xA0A0);
  // Q15_Out = {0xA0A0, 0xA0A0, 0xA0A0, 0xA0A0}
  
  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}
}
What do you think about this topic ? Send us feedback!



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