14.1 Useful functions

MikroElektronika

At the beginning of this chapter the functions used in this chapter will be described together with some other useful functions. Table 14-1 presents a list of the functions including a description of their parameters, results (functions only), and eventual restrictions on the parameter values

FIR_Radix
IIR_Radix
FFT
IFFT
BitReverseComplex
Vector_Set
VectorPower
Vector_Subtract
VectorScale
Vector_Negate
Vector_Multiply
Vector_Min
Vector_Max
Vector_Dot
Vector_Correlate
Vector_Convolve
Vector_Add
Matrix_Transponse
Matrix_Subtract
Matrix_Scale
Matrix_Multiply
Matrix_Add

FIR_Radix

Prototype unsigned FIR_Radix(unsigned FilterOrder, const unsigned *ptrCoeffs, unsigned BuffLength,unsigned *ptrInput, unsigned Index);
Description This function applies FIR filter to ptrInput. Input samples must be in Y data space.
FilterOrder is order of the filter + 1.
ptrCoeffs is address of filter coeffitients in program memory.
BuffLength represents number of samples ptrInput points to.
ptrInput is address of input samples.
Index index of current sample.
Returns sum(k=0..N-1)(coef[k]*input[N-k]) – Current sample of processed signal(B[n])
N – buffer length
k – Current index

IIR_Radix

Prototype unsigned IIR_Radix (const int BScale, const int AScale, const signed *ptrB, const signed*ptrA, unsigned FilterOrder, unsigned *ptrInput, unsigned Input_Len, unsigned *ptrOutput,unsigned Index)
Description This function applies IIR filter to ptrInput. Input and output samples must be in Y data space.
AScale A Scale factor
BScale B Scale factor
ptrB Address of B coefficients (In program memory)
ptrA Address of A coefficients (In program memory)
FilterOrder is order of the filter + 1.
ptrInput is address of input samples. Input_Len represents number of samples ptrInput points to.
ptrOutput is address of output samples. Output length is equal to Input length.
Index index of current sample.
Returns y[n]=sum(k=0..N)(Acoef[k]*x[n-k]) – sum(k=1..M)(Bcoef[k]*y[n-k])

FFT

Prototype void Fft(unsigned log2N, const unsigned *TwiddleFactorsAddress, unsigned *Samples);
Description Function applies FFT transformation to input samples, input samples must be in Y data space.
N – buffer length (must be the power of 2).
TwiddleFactorsAddress is address of costant array which contains complex twiddle factors.The array is expected to be in program memory.
Samples array of input samples.
Upon completion complex array of FFT samples is placed in the Samples parameter.
Returns F(k) = 1/N*sum_n (f(n)*WN(kn)), WN(kn) = exp[-(j*2*pi*k*n)/N]
Fn – array of complex input samples
n in {0, 1,… , N-1}, and k in {0, 1,… , N-1}, with N = 2^m, m element of Z.
WN – TwiddleFactorsThe amplitude of current FFT sample is calculated as:
F[k]=sqrt(Re[k]^2+ Im[k]^2)
Note

Complex array of FFT samples is placed in Samples parameter. Input Samples are arranged in manner Re,Im,Re,Im… (where Im is always zero). Output samples are arranged in the same manner but Im parts are different from zero. Output samples are symmetrical (First half of output samples (index from 0 to N/2) is identical to the second half of output samples(index from N/2 to N).

Input data is a complex vector such that the magnitude of the real and imaginary parts of each of its elements is less than 0.5. If greater or equal to this value the results could produce saturation. Note that the output values are scaled by a factor of 1/N, with N the length of the FFT. input is expected in natural ordering, while output is produced in bit reverse ordering.

BitReverseComplex

Prototype void BitReverseComplex(unsigned log2N, unsigned *ReIm)
Description This function does Complex (in-place) Bit Reverse re-organization.
N – buffer length (must be the power of 2).
ReIm – Output Sample(from FFT).
Note Input samples must be in Y data space.

Vector_Set

Prototype void Vector_Set(unsigned *input, unsigned size, unsigned value);
Description Sets size elements of input to value, starting from the first element.
Size must be > 0.
Length of input is limited by available ram

VectorPower

Prototype unsigned VectorPower(unsigned N, unsigned *Vector);
Description Function returns result of power value (powVal) in radix point 1.15
Operation powVal = sum (srcV[n] * srcV[n]) with n in {0, 1,… , numElems-1}
Input N = number of the elements in vector(s) (numElems)
Vector = ptr to source vector (srcV)
Note AccuA used, not restored
CORCON saved, used, restored

Vector_Subtract

Prototype void Vector_Subtract(unsigned *dest, unsigned *v1, unsigned *v2, unsigned numElems);
Description This procedure does substraction of two vectors. numElems must be less or equal to minimum size of two vectors.
v1 – First Vector
v2 – Second Vector
dest – Result Vector
Operation dstV[n] = srcV1[n] – srcV2[n]
with n in {0, 1,… , numElems-1}
Note AccuA used, not restored.
CORCON saved, used, restored.

VectorScale

Prototype void VectorScale(unsigned N, int ScaleValue, unsigned *SrcVector, unsigned *DestVector);
Description This procedure does vector scaling with scale value.
N – Buffer length
SrcVector – original vector
DestVector – scaled vector
ScaleValue – Scale Value
Operation dstV[n] = sclVal * srcV[n], with n in {0, 1,… , numElems-1}
Note AccuA used, not restored.
CORCON saved, used, restored.

Vector_Negate

Prototype void Vector_Negate(unsigned *srcVector, unsigned *DestVector, unsigned numElems);
Description This procedure does negation of vector.
srcVector – Original vector
destVector – Result vector
numElems – Number of Elements
Operation dstV[n] = (-1)*srcV1[n] + 0, 0 <= n < numElems
Note Negate of 0x8000 is 0x7FFF.
AccuA used, not restored.
CORCON saved, used, restored.

Vector_Multiply

Prototype void Vector_Multiply(unsigned *v1, unsigned *v2, unsigned *dest, unsigned numElems);
Description This procedure does multiplication of two vectors.
numElems must be less or equal to minimum size of two vectors.
v1 – First Vector
v2 – Second Vector
dest – Result Vector
Operation dstV[n] = srcV1[n] * srcV2[n]
with n in {0, 1,… , numElems-1}
Note AccuA used, not restored.
CORCON saved, used, restored.

Vector_Min

Prototype unsigned Vector_Min(unsigned *Vector, unsigned numElems, unsigned *MinIndex);
Description This function find min. value in vector.
Vector – Original vector.
numElems – Number of elements
MinIndex – Index of minimum value
Operation minVal = min {srcV[n], n in {0, 1,…numElems-1}
if srcV[i] = srcV[j] = minVal, and i < j, then minIndex = j
Returns minimum value (minVal)

Vector_Max

Prototype unsigned Vector_Max(unsigned *Vector, unsigned numElems, unsigned *MaxIndex);
Description This function find max. value in vector.
Vector – Original vector.
numElems – Number of elements
MaxIndex – Index of maximum value
Operation maxVal = max {srcV[n], n in {0, 1,…numElems-1} }
if srcV[i] = srcV[j] = maxVal, and i < j, then maxIndex = j
Returns maximum value (maxVal)

Vector_Dot

Prototype unsigned Vector_Dot(unsigned *v1, unsigned *v2, unsigned numElems);
Description Procedure calculates vector dot product.
v1 – First vector.
v2 – Second vector
numElems – Number of elements
Operation dotVal = sum (srcV1[n] * srcV2[n]),
with n in {0, 1,… , numElems-1}
Note AccuA used, not restored.
CORCON saved, used, restored.

Vector_Correlate

Prototype void Vector_Correlate(unsigned *v1, unsigned *v2, unsigned *dest, unsigned numElemsV1,unsigned numElemsV2);
Description Procedure calculates Vector correlation (using convolution).
v1 – First vector.
v2 – Second vector
numElemsV1 – Number of first vector elements
numElemsV2 – Number of second vector elements
dest – Result vector
Operation r[n] = sum_(k=0:N-1){x[k]*y[k+n]},
where:
x[n] defined for 0 <= n < N,
y[n] defined for 0 <= n < M, (M <= N),
r[n] defined for 0 <= n < N+M-1.

Vector_Convolve

Prototype void Vector_Convolve(unsigned *v1, unsigned *v2, unsigned *dest, unsigned numElemsV1, unsignednumElemsV2);
Description Procedure calculates Vector using convolution.
v1 – First vector.
v2 – Second vector
numElemsV1 – Number of first vector elements
numElemsV2 – Number of second vector elements
dest – Result vector
Operation y[n] = sum_(k=0:n){x[k]*h[n-k]}, 0 <= n < M
y[n] = sum_(k=n-M+1:n){x[k]*h[n-k]}, M <= n < N
y[n] = sum_(k=n-M+1:N-1){x[k]*h[n-k]}, N <= n < N+M-1
Note AccuA used, not restored.
CORCON saved, used, restored.

Vector_Add

Prototype void Vector_Add(unsigned *dest, unsigned *v1, unsigned *v2, unsigned numElems);
Description Procedure calculates vector addition.
v1 – First vector.
v2 – Second vector
numElems – Number of vector elements
dest – Result vector
Operation dstV[n] = srcV1[n] + srcV2[n],
with n in {0, 1,… , numElems-1}
Note AccuA used, not restored.
CORCON saved, used, restored.

Matrix_Transponse

Prototype void Matrix_Transpose(unsigned * src, unsigned * dest, unsigned num_rows, unsigned num_cols);
Description Procedure does matrix transposition.
src – Original matrix.
dest – Result matrix
numRows – Number of matrix rows
numCols – Number of matrix columns
Operation dstM[i][j] = srcM[j][i]

Matrix_Subtract

Prototype void Matrix_Subtract(unsigned * src1, unsigned * src2, unsigned * dest, unsigned num_rows,unsigned num_cols);
Description Procedure does matrix substraction.
src1 – First matrix.
src2 – Second matrix
dest – Result matrix
num_rows – Number of matrix rows
num_cols – Number of matrix columns
Operation dstM[i][j] = srcM1[i][j] – srcM2[i][j]
Note AccuA used, not restored.
AccuB used, not restored.
CORCON saved, used, restored.

Matrix_Scale

Prototype void Matrix_Scale(unsigned scale_value, unsigned *src1, unsigned *dest, unsigned num_rows,unsigned num_cols);
Description Procedure does matrix scale.
ScaleValue – Scale Value
src1 – Original matrix
dest – Result matrix
num_rows – Number of matrix rows
num_cols – Number of matrix columns
Operation dstM[i][j] = sclVal * srcM[i][j]
Note AccuA used, not restored.
CORCON saved, used, restored.

Matrix_Multiply

Prototype void Matrix_Multiply(unsigned * src1, unsigned * src2, unsigned * dest, unsigned numRows1,unsigned numCols2, unsigned numCols1Rows2);
Description Procedure does matrix multiply.
src1 – First Matrix
src2 – Second Matrix
dest – Result Matrix
numRows1 – Number of first matrix rows
numCols2 – Number of second matrix columns
numCols1Rows2 – Number of first matrix columns and second matrix rows
Operation dstM[i][j] = sum_k(srcM1[i][k]*srcM2[k][j]),
with
i in {0, 1, …, numRows1-1}
j in {0, 1, …, numCols2-1}
k in {0, 1, …, numCols1Rows2-1}
Note AccuA used, not restored.
CORCON saved, used, restored.

Matrix_Add

Prototype void Matrix_Add(unsigned * src1, unsigned * src2, unsigned * dest, unsigned numRows, unsignednumCols);
Description Procedure does matrix addition.
src1 – First Matrix
src2 – Second Matrix
dest – Result Matrix
numRows – Number of first matrix rows
numCols – Number of second matrix columns
Operation dstM[i][j] = srcM1[i][j] + srcM2[i][j]
Note AccuA used, not restored.
CORCON saved, used, restored.