Use variable with number of bits for entiere and fractional part

조회 수: 8 (최근 30일)
Hello,
I have to create a Simulink model to simulate the functionnalities of an embedded card coded in C langage (32 bits processor). In this code, there is a specific type of variable created to simplify the calculation of floatting number. On a long variable (32 bits), they use 12 bits for the entire part and 20 for the fractional part.
Is there a way to deal with this kind of variable easily ? In my mind I have to convert my float manually to binary to stock this value and reconvert it as float when I need it. Because I have to to mathematial operation (add, substract, multiply, divide) between two variables of this type.
Thank you in advance for your help.
Best regards

채택된 답변

Andy Bartlett
Andy Bartlett 2021년 8월 9일
편집: Andy Bartlett 2021년 8월 9일
Hi Mathieu,
As Walter mentioned, Fixed-Point Designer provides fixed-point support for MATLAB, Simulink, Stateflow, Embedded Coder, HDL Coder, and other MathWorks products.
Walter gave you an excellent link for getting started with fixed-point in MATLAB using fi objects.
Simulink fixed-point intro
In Simulink, fixed-point support is integrated into a wide selection of blocks. The demo fxpdemo_dbl2fix.slx is a nice first step in using fixed-point in base Simulink blocks.
Open the model fxpdemo_dbl2fix. (see image below).
Double click on the Convert block with name "Dbl-to-FixPt".
To the right of the "Output data type" parameter, click " << " to open data type assistant.
Change the word length to 32 bits.
Change the fraction length to 20 bits.
Set signedness to signed or unsigned as needed.
Click Apply button.
Simulate the model by clicking the green Run triangle at the top of the model.
On the signal line coming out of the "Dbl-to-FixPt" convert block, you should see
"sfix32_En20" or "ufix32_En20" depending on whether you picked signed or unsigned.
You've now completed the first step in modelling your 32 bit fixed-point data type with 20 fractional bits!
Regards,
Andy
Fixed-point math handled automatically
Based on the input and output types used, all the details of fixed-point math are automatically handled by fi MATLAB objects and Simulink blocks. Simulation with Simulink and MATLAB, analysis with Simulink Design Verifier, and code generation with Embedded Coder or HDL Coder all handle the needed details and give bit-true agreement with each other.
The following image shows the demo model fxpdemo_feedback.slx doing a collection of math on a mixture of fixed-point types.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2021년 8월 9일
The Fixed Point Toolbox is designed for this sort of operation.
However, you might have difficulty dealing with sign, depending how it is represented.

Mathieu Gauquelin
Mathieu Gauquelin 2021년 8월 10일
Wahou it is fantastic all we can do :D Thank you for all these explanation. I will look at this. I think I have all information I need to continue. Thank you !

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by