How to Change the data type of MATLAB user defined function block in SIMULINK to a fixed point 8 bit or 16 bit

조회 수: 9 (최근 30일)
Hello all,
I am working in the HDL coder tool in Simulink.
In simulink I have written a matlab code using MATLAB user defined function and simulated it successfully. But the out from the MATLAB function is by default is double, is there any method to change the data type to fixdt point 16 bit. This will be helpful for me while converting the model into HDL code.
Thanking you,
Roy

답변 (2개)

Tim McBrayer
Tim McBrayer 2014년 8월 27일
There are many ways to do this. The most direct will be to define the data type of the MATLAB Function block output using the Model Explorer. It will explicitly allow you to set any desired data type on any port of any block.
A less direct method is to use Simulink's type propagation. The answer above suggests a Data Type Conversion block, placed on the output. This won;t quite do it. You would need the receiving block's input to be specified to your desired data type.
This can also be accomplished inside your MATLAB code, by explicitly setting the type of the data assigned to your function output in the assignment. For example, for the final output assignment, instead of writing:
y = my_computed_data;
Write this instead:
y = fi(my_computed_data, 1, 16, 4);
This specifies the output to be a signed 16 bit value with 4 bits to the right of the binary point. If this is the first (or only) assignment of output y, it will define the data type of y. With no explicit or implicit definition the data type defaults to double.
  댓글 수: 2
Anish Navalgund
Anish Navalgund 2018년 4월 15일
Sir, I have multiple variables in my user-defined matlab function used in simulink. Now, if I'm using "fi" function, it is throwing multiple errors. Can I know where to declare "fi" function? Before function definition or in function body? Please help.
Bharath Venkataraman
Bharath Venkataraman 2023년 3월 1일
편집: Bharath Venkataraman 2023년 3월 1일
fi is used within the function body. Here is the link to the documentation for fi.
If you are defining a variable var to be a fixed-point data type, you can do the following:
var = fi(0, 1, 16, 14); % signed, word length = 16 bits, fraction length = 14 bits.
Here is an example that shows design patterns for use of the MATLAB function block for HDL code generation.

댓글을 달려면 로그인하십시오.


stalin
stalin 2014년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by