14.1 Useful functions and procedures
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 evenual restrictions on the paremeter 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 |
function FIR_Radix(FilterOrder: Word; ptrCoeffs: LongInt; BuffLength: Word; ptrInput: Word; Index: Word): 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 |
function IIR_Radix(BScale: Integer; AScale: Integer; ptrB: Word; ptrA: Word; FilterOrder: Word; ptrInput: Word; Input_Len: Word; ptrOutput: Word; Index: Word) : 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 |
procedure FFT(log2N: word; TwiddleFactorsAddress: LongInt; var Samples: array[1024] of word); |
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. |
IFFT
Prototype |
procedure IFFT(log2N: word; TwiddleFactorsAddress: LongInt; var Samples: array[1024] of word); |
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 |
procedure BitReverseComplex(log2N: word; var ReIm: array[1024] of word); |
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 |
procedure Vector_Set(var input: array[1024] of word; size, value: 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 |
function VectorPower(N: word; var Vector: array[1024] of word): 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 |
procedure Vector_Subtract(var dest, v1, v2: array[1024] of word; numElems: 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 |
procedure VectorScale(N: word; ScaleValue: integer; var SrcVector, DestVector: array[1024] ofword); |
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 |
procedure Vector_Negate(var srcVector, destVector: array[1024] of word; numElems: 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 |
procedure Vector_Multiply(var v1, v2, dest: array[1024] of word; numElems: 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 |
function Vector_Min(var Vector: array[1024] of word; numElems: word; var MinIndex: word): 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 |
function Vector_Max(var Vector: array[1024] of word; numElems: word; var MaxIndex: word): 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 |
function Vector_Dot(var v1, v2: array[1024] of word; numElems: word): 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 |
procedure Vector_Correlate(var v1, v2, dest: array[1024] of word; numElemsV1, numElemsV2: 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 |
procedure Vector_Convolve(var v1, v2, dest: array[1024] of word; numElemsV1, numElemsV2: 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 |
procedure Vector_Add(var dest, v1, v2: array[256] of word; numElems: word); |
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 |
procedure Matrix_Transponse(var src, dest: array[1024] of word; numRows, numCols: 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 |
procedure Matrix_Subtract(var src1, src2, dest: array[1024] of word; numRows, numCols: 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 |
procedure Matrix_Scale(ScaleValue: word; var src1, dest: array[1024] of word; numRows, numCols: 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 |
procedure Matrix_Multiply(var src1, src2, dest: array[256] of word; numRows1, numCols2, numCols1Rows2: 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 |
procedure Matrix_Add(var src1, src2, dest: array[1024] of word; numRows, numCols: word); |
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. |