Digital Signal Processing Reference
In-Depth Information
#define N 16
X _Fract coeff[N] = { 0.7r, ... };
_Fract inner_product(Y _Fract
*
inp)
{
int i;
_Accum sum = 0.0k;
for (i = 0; i < N; i++)
{
sum += coeff[i]
*
(_Accum)
*
(inp++);
}
return (_Sat _Fract)sum;
}
The above example sums the products in an
Accum
typed variable
sum
.Itis
assumed that this summation will not overflow, because
N
is smaller or equal than
the integral part of the
Accum
. Only the final result is saturated back to a
Fract
.
The cast to
Accum
inside the loop makes sure that the multiplication is done
in the
Accum
type. Without this cast the multiplication would have been done in
Fract
type and if both the coefficient and the input would have the exact value
−
0 which is out of range for a regular
Fract
,
resulting in an overflow. In a normal FIR filter there will not however be a
1
.
0, then the result would be 1
.
0in
the coefficients. So in that case the
Accum
cast could be safely removed and the
multiplication would be performed within the
Fract
type.
The example also uses the memory spaces
X
and
Y
from where respectively the
inp
and the
coeff
constants are retrieved. Assuming the target architecture looks
in one cycle.
−
1
.
5.2
Sine Function in Fixed Point
Another example of converting a floating point algorithm into fixed point is the
computation of the sine function.
#define F 5
static _Accum sinfract(_Accum x)
{
int f;
_Accum result = 1.0k;
for (f = 2
*
F+1; f > 1; f -= 2)
{
result = 1.0k - result
*
x
*
x/(f
*
(f-1));
}