Why do NUM2BIN and HEX2BIN functions in the Filter Design Toolbox 2.5 (R13SP1) output the incorrect number of bits for my quantized filter?

2 views (last 30 days)
Why do NUM2BIN and HEX2BIN functions in the Filter Design Toolbox 2.5 (R13SP1) output the incorrect number of bits for my quantized filter?
For example, I have the following quantized filter:
Hq = qfilt('df2t',{{[1 -2 1],[1 -2 1]},{[1 -2 1],[1 -2 1]}},...
'CoefficientFormat',unitquantizer('fixed', 'round','saturate', [20 18]),...
'InputFormat',quantizer('fixed', 'floor', 'saturate', [20 19]),...
'OutputFormat',quantizer('fixed', 'floor', 'saturate', [20 19]),...
'MultiplicandFormat',quantizer('fixed', 'floor', 'saturate', [32 31]),...
'ProductFormat',quantizer('fixed', 'floor', 'saturate', [32 31]),...
'SumFormat',quantizer('fixed', 'floor', 'saturate', [32 31]))
When I tried to convert the filter coefficients to binary numbers, I get 16 bits instead of the specified 20 bits.
num2bin(Hq)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This bug has been fixed for Release 14 (R14). For previous releases, please read below for any possible workarounds:
This has been verified as a bug in Filter Design Toolbox 2.5 (R13SP1) in the way that the unitquantizer objects are passed into the NUM2BIN and NUM2HEX functions.
Currently, to work around this issue, try converting the unitquantizer coefficient format into a regular quantizer and then use the NUM2BIN or NUM2HEX function.
For example,
Hq = qfilt('df2t',{{[1 -2 1],[1 -2 1]},{[1 -2 1],[1 -2 1]}},...
'CoefficientFormat',unitquantizer('fixed', 'round','saturate', [20 18]),...
'InputFormat',quantizer('fixed', 'floor', 'saturate', [20 19]),...
'OutputFormat',quantizer('fixed', 'floor', 'saturate', [20 19]),...
'MultiplicandFormat',quantizer('fixed', 'floor', 'saturate', [32 31]),...
'ProductFormat',quantizer('fixed', 'floor', 'saturate', [32 31]),...
'SumFormat',quantizer('fixed', 'floor', 'saturate', [32 31]))
To get NUM2BIN to output the correct bit resolution, use the following series of MATLAB commands:
% Workaround: Convert the unitquantizer coefficient format into a regular quantizer.
u = Hq.Coefficientformat;
q = quantizer(u.mode,u.roundmode,u.overflowmode,u.format);
% Note: Do not do
% q = quantizer(u)
% because the attributes are copied over wrong. This is the root cause of the bug.
% Then call NUM2BIN using the regular quantizer.
coeff = num2bin(q, Hq.ReferenceCoefficients);
% Display the contents of the coeff cell array
celldisp(coeff)
To get NUM2HEX to output the correct bit resolution, replace NUM2BIN with NUM2HEX in the above code.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R13SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!