Configure filter for integer filtering
set2int(h)
set2int(h,coeffwl)
set2int(...,inwl)
g = set2int(...)
This section applies to discretetime (dfilt
)
filters.
set2int(h)
scales the filter
coefficients to integer values and sets the filter coefficient and
input fraction lengths to zero.
set2int(h,coeffwl)
uses
the number of bits specified by coeffwl
as the
word length it uses to represent the filter coefficients.
set2int(...,inwl)
uses
the number of bits specified by coeffwl
as the
word length it uses to represent the filter coefficients and the number
of bits specified by inwl
as the word length to
represent the input data.
g = set2int(...)
returns
the gain g
introduced into the filter by scaling
the filter coefficients to integers. g
is always
calculated to be a power of 2.
Note

The second part of the example depends on the following — after you filter a set of data, the input data and output data cover the same range of values, unless the filter process introduces gain in the output. Converting your filter object to integer form, and then filtering a set of data, does introduce gain into the system. When the examples refer to resetting the output to the same range as the input, the examples are accounting for this added gain feature.
Two parts comprise this example. Part 1 compares the step response of an FIR filter in both the fractional and integer filter modes. Fractional mode filtering is essentially the opposite of integer mode. Integer mode uses a filter which has coefficients represented by integers. Fractional mode filters have coefficients represented in fractional form (nonzero fraction length).
b = rcosdesign(.25,4,25,'sqrt'); hd = dfilt.dffir(b); hd.Arithmetic = 'fixed'; hd.InputFracLength = 0; % Integer inputs. x = ones(100,1); yfrac = filter(hd,x); % Fractional mode output. g = set2int(hd); % Convert to integer coefficients. yint = filter(hd,x); % Integer mode output.
Note that yint
and yfrac
are fi
objects. Later in this example, use the fi
object properties WordLength
and FractionLength
to work with the output data. Now use the gain g
to rescale the output from the integer mode filter operation. Verify that the scaled integer output is equal to the fractional output.
yints = double(yint)/g;
Verify that the scaled integer output is equal to the fractional output.
max(abs(yintsdouble(yfrac)))
ans = 0
In part two, the example reinterprets the output binary data, putting the input and the output on the same scale by weighting the most significant bits in the input and output data equally.
WL = yint.WordLength; FL = yint.FractionLength + log2(g); yints2 = fi(zeros(size(yint)),true,WL,FL); yints2.bin = yint.bin; max(abs(double(yints2)double(yfrac)))
ans = 0