14.1 Useful functions and procedures

MikroElektronika

At the beginning of this chapter the functions and procedures used in this chapter will be described together with some other useful functions and procedures. Table 14-1 presents a list of the functions and procedures 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 sub function FIR_Radix(dim FilterOrder as Word, dim ptrCoeffs as LongInt, dim BuffLength as Word, dim ptrInput as Word, dim Index as Word) as Word
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 sub function IIR_Radix(dim BScale as Integer, dim AScale as Integer, dim ptrB as Word, dimptrA as Word, dim FilterOrder as Word, dim ptrInput as Word, dim Input_Len as Word, dimptrOutput as Word, dim Index as Word) as Word
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 sub procedure FFT(dim log2N as word, dim TwiddleFactorsAddress as LongInt, dim byref Samplesas word[1024])
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 – TwiddleFactors

The 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.

IFFT

Prototype sub procedure IFFT(dim log2N as word, dim TwiddleFactorsAddress as LongInt, dim byrefFftSamples as word[1024])
Description Function applies IFFT 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 IFFT samples is placed in the Samples parameter.
Operation 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 – TwiddleFactors
Note

Complex array of IFFT samples is placed in Samples parameter. Input Samples are arranged in manner Re,Im,Re,Im… (where Im is always zero).

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 IFFT. Input is expected in bit reverse ordering, while output is produced in natural ordering.

BitReverseComplex

Prototype sub procedure BitReverseComplex(dim log2N as word, dim byref ReIm as word[1024])
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 sub procedure Vector_Set(dim byref input as word[1024], dim size, value as word)
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 sub function VectorPower(dim N as word, dim byref Vector as word[1024]) as word
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 sub procedure Vector_Subtract(dim byref dest, v1, v2 as word[1024], dim numElems as word)
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 sub procedure VectorScale(dim N as word, dim ScaleValue as integer, dim byref SrcVector, DestVector as word[1024])
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 sub procedure Vector_Negate(dim byref srcVector, DestVector as word[1024], dim numElems as word)
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 sub procedure Vector_Multiply(dim byref v1, v2, dest as word[1024], dim numElems as word)
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 sub function Vector_Min(dim byref Vector as word[1024], dim numElems as word, dim byrefMinIndex as word) as word
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 sub function Vector_Max(dim byref Vector as word[1024], dim numElems as word, dim byrefMaxIndex as word) as word
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 sub function Vector_Dot(dim byref v1, v2 as word[1024], dim numElems as word) as word
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 sub procedure Vector_Correlate(dim byref v1, v2, dest as word[1024], dim numElemsV1, numElemsV2 as word)
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 sub procedure Vector_Convolve(dim byref v1, v2, dest as word[1024], dim numElemsV1, numElemsV2as word)
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 sub procedure Vector_Add(dim byref dest, v1, v2 as word[256], dim numElems as word)
Description Procedure calculates vector addition.
v1 – First vector.
v2 – Second vector
numElemsV1 – 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 sub procedure Matrix_Transponse(dim byref src, dest as word[1024], dim numRows, numCols as word)
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 sub procedure Matrix_Subtract(dim byref src1, src2, dest as word[1024], dim numRows, numColsas word)
Description Procedure does matrix substraction.
src1 – First matrix.
src2 – Second matrix
dest – Result matrix
numRows – Number of matrix rows
numCols – 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 sub procedure Matrix_Scale(dim ScaleValue as word, dim byref src1, dest as word[1024], dimnumRows, numCols as word)
Description Procedure does matrix scale.
ScaleValue – Scale Value
src1 – Original matrix
dest – Result matrix
numRows – Number of matrix rows
numCols – Number of matrix columns
Operation dstM[i][j] = sclVal * srcM[i][j]
Note AccuA used, not restored.
CORCON saved, used, restored.

Matrix_Multiply

Prototype sub procedure Matrix_Multiply(dim byref src1, src2, dest as word[256], dim numRows1, numCols2, numCols1Rows2 as word)
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 sub procedure Matrix_Add(dim byref src1, src2, dest as word[1024], dim numRows, numCols as word)
Description Procedure does matrix addition.
src1 – First Matrix
src2 – Second Matrix
dest – Result Matrix
numRows1 – Number of first matrix rows
numCols2 – Number of second matrix columns
Operation dstM[i][j] = srcM1[i][j] + srcM2[i][j]
Note AccuA used, not restored.
CORCON saved, used, restored.