Documentation |
Configure filter for integer filtering
set2int(h)
set2int(h,coeffwl)
set2int(...,inwl)
g = set2int(...)
These sections apply to both discrete-time (dfilt) and multirate (mfilt) 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.
These examples demonstrate some uses and ideas behind set2int.
The second parts of both examples depend 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, you 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; MaxDiff=max(abs(yints-double(yfrac)));
Verify that the scaled integer output is equal to the fractional output.
max(abs(yints-double(yfrac)))
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; MaxDiff=max(abs(double(yints2)-double(yfrac)));
This two-part example starts by comparing the step response of a multirate filter in both fractional and integer 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 in fractional form with nonzero fraction lengths.
hm = mfilt.firinterp; hm.Arithmetic = 'fixed'; hm.InputFracLength = 0; % Integer inputs. x = ones(100,1); yfrac = filter(hm,x); % Fractional mode output. g = set2int(hm); %Convert to integer coefficients. yint = filter(hm,x); % Integer mode output.
Note that yint and yfrac are fi objects. In part 2 of this example, you 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.
yints = double(yint)/g;
Verify that the scaled integer output is equal to the fractional output.
max(abs(yints-double(yfrac)))
Part 2 demonstrates reinterpreting the output binary data by using the properties of yint to create a scaled version of yint named yints2. This process puts yint and yints2 on the same scale by weighing the most significant bits of each object 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)))