Using fi for modeling precision in hardware model design
2 views (last 30 days)
Show older comments
I'm modeling a hardware block in Simulink. One of the multiplier modules works with unsigned fixed length bit vectors.
output err: 6 bits
input u: 6 bits
input wt: 4 bits with 3 bits for fractional part
On command line, the following code snippet works:
>> err = fi(0,0,6,0); %an unsingned bit vector of 6 bits
>> u = fi(43,0,6,0);
>> wt = fi(0.25,0,4,3);
>> err.Value = u * wt
err =
11
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 6
FractionLength: 0
>> err = u * wt
err =
10.7500
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 10
FractionLength: 3
From a hardware design perspective, I want the first operation.
But if I write the matlab function like:
function err = fcn(u, wt)
err = fi(0,0,6,0); % using this as a variable declaration statement
err.Value = u * wt;
I get the diagnostic error:
Writing properties to 'embedded.fi' is not supported for code generation.
I could re-write the function as below which works. But if I have to assign err multiple times in the function, I need to specify the redundant information (0,6,0) all the time which is inefficient and error prone.
err = fi( (u * wt), 0, 6, 0);
Are there any suggestions to rewrite this code while keeping variable declaration and assignment separated?
Thanks!
0 Comments
Answers (1)
Shivansh
on 10 Sep 2024
Hi Anshul,
You can try to pass a prototype of the desired datatype as an input to the function and then use it to initialize the variable.
You can refer to the following code snippet for a better understanding of the approach.
% Define a prototype fi object with the desired properties
proto = fi([], numerictype(0, 6, 0));
function err = fcn(u, wt, proto)
% Use the prototype to initialize the err variable
err = proto;
% Perform the multiplication and assign the result to err
err(:) = u * wt;
end
You can refer to the following documentation for more information about fi data type: https://www.mathworks.com/help/fixedpoint/ref/embedded.fi.html.
I hope this helps resolve the issue!
0 Comments
See Also
Categories
Find more on Fixed-Point Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!