MATLAB: How to specify the length of a product of two fixed-point numbers with Fixed-Point Toolbox

I have x and y with word length 4 and fractional number of bits 3:
% set fixed point variable display behavior
p = fipref;
p.NumericTypeDisplay = 'short';
p.FimathDisplay = 'none';
p.LoggingMode = 'on';
format compact
% Set integer and fractional number of bits
DataWordLength = 4
DataFracLength = 3
MultWordLength = 8
MultFracLength = 7
AccWordLength = 10
AccFracLength = 8
% Apply bit widths to named types
Tdata = numerictype('WordLength', DataWordLength, 'FractionLength', DataFracLength)
Tmult = numerictype('WordLength', MultWordLength, 'FractionLength', MultFracLength)
Taccum = numerictype('WordLength', AccWordLength, 'FractionLength', AccFracLength)
% Accumulator sum/mult characteristics
F = fimath('ProductMode','SpecifyPrecision', ...
'ProductWordLength',AccWordLength, 'ProductFractionLength',AccFracLength, ...
'SumMode','SpecifyPrecision', 'SumWordLength', AccWordLength, ...
'SumFractionLength',AccFracLength)
% experiment
Fdata = fimath('ProductMode','SpecifyPrecision', ...
'ProductWordLength',DataWordLength, 'ProductFractionLength',DataFracLength, ...
'SumMode','SpecifyPrecision', 'SumWordLength', DataWordLength, ...
'SumFractionLength',DataFracLength)
% set data values
x = fi(-1, Tdata, F)
y = fi(-1, Tdata, F)
z = fi(-1, Tdata, Fdata)
acc = fi(0, Taccum, F)
mult = fi(0, Tmult, F)
% multiply two numbers
mult = x * y
acc = x * y
and the output is
mult =
1
s10,8
acc =
1
s10,8
How can I make sure that the result of the multiplication has specified properties (word length and fraction length)?

Best Answer

To cast a variable into another, you should use subscripted assignment. The rounding and overflow characteristics are taken from the left-hand-side variable's fimath settings.
% set fixed point variable display behavior
p = fipref;
p.NumericTypeDisplay = 'short';
p.FimathDisplay = 'none';
p.LoggingMode = 'on';
format compact
% Set integer and fractional number of bits
DataWordLength = 4
DataFracLength = 3
MultWordLength = 8
MultFracLength = 7
AccWordLength = 10
AccFracLength = 8
% Apply bit widths to named types
Tdata = numerictype('WordLength', DataWordLength, 'FractionLength', DataFracLength)
Tmult = numerictype('WordLength', MultWordLength, 'FractionLength', MultFracLength)
Taccum = numerictype('WordLength', AccWordLength, 'FractionLength', AccFracLength)
% Accumulator sum/mult characteristics
F = fimath('ProductMode','SpecifyPrecision', ...
'ProductWordLength',AccWordLength, 'ProductFractionLength',AccFracLength, ...
'SumMode','SpecifyPrecision', 'SumWordLength', AccWordLength, ...
'SumFractionLength',AccFracLength)
% experiment
Fdata = fimath('ProductMode','SpecifyPrecision', ...
'ProductWordLength',DataWordLength, 'ProductFractionLength',DataFracLength, ...
'SumMode','SpecifyPrecision', 'SumWordLength', DataWordLength, ...
'SumFractionLength',DataFracLength)
% set data values
x = fi(-1, Tdata, F)
y = fi(-1, Tdata, F)
z = fi(-1, Tdata, Fdata)
acc = fi(0, Taccum, F)
mult = fi(0, Tmult, F)
% multiply two numbers
mult(:) = x * y
acc(:) = x * y
% Now try to move the data from "acc" into a variable that has the same
% size as x or y.
z(:) = acc