Data type conversion in simulink
Show older comments
Hi, One of the output of simulink model is complex number represented in fixd point 18 word lenght and 14 fractional length which is then converted into 18 word length and 15 fractional length . how i can convert the real part and imaginary part of complex number into hex number ? in simulink
Accepted Answer
More Answers (1)
Andy Bartlett
on 14 Sep 2024
Edited: Andy Bartlett
on 14 Sep 2024
Just Viewing Value in Hex Format
If you just want to view the value of fixed-point signal in hex format, please refer to this post (show-the-hex-or-binary-representation-for-an-integer-or-fixed-point-signal-in-simulink).
Putting Hex-String in a Variable for String Processing
If your goal is to create a string variable and have that string variable hold hex-string corresponding to fixed-point variable, then Jatin's answer is good. One clarification on Jatin's steps is when converting from fixed-point to integer make sure you configure the conversion so the output has a Stored Integer Value equal to the Stored Integer Value of the input. This type of conversion can be done using
- fi objects stripscaling method
- Simulink's Data Type Scaling Strip Block
- Simulink's Data Type Conversion Block with parameter setting "Input and output to have equal:" Stored Integer Value
Here's an example of the importance of doing the stripscaling step.
% Create the fixed-point value
val = fi(0.4588623046875,1,16,15)
% just view the stored integer value of the original value in hex format
val.hex
% Strip off the scaling to leave just the raw stored integer value
valSI = stripscaling(val)
% just view the variable holding the raw stored integer value in hex format
valSI.hex
% If desired convert the raw stored integer variable from fi object to
% equivalent base MATLAB integer
valSI2 = castFiToInt(valSI)
% create a string variable holding the hex representation of the raw stored
% integer value
var_SI_Hex_String = dec2hex(valSI2)
If you cast the fi variable directly to int16, it will be a Real World Value cast. The output value will be 0, which has hex format '0' instead of '3ABC'.
% Direct cast to int16 is a Real World Value cast
% Input real world value 0.4588623046875 will cast to the nearest integer
% value which is 0
valDirectCastToInt16 = int16(val)
% Creating the hex-string and putting it in a variable will still be '0'
var_DirectCastToInt16_Hex_String = dec2hex(valDirectCastToInt16)
Hopefully this example clarifies why the "strip scaling" step is critical
Categories
Find more on Sources 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!