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
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

类别

Help CenterFile Exchange 中查找有关 HDL Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by