Data type conversion in simulink

9 views (last 30 days)
shailee
shailee on 12 Sep 2024
Edited: Andy Bartlett on 14 Sep 2024
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

Jatin
Jatin on 12 Sep 2024
To convert the real and imaginary parts of a complex number into their respective hexadecimal representations in Simulink, you can use a combination of Simulink blocks and a MATLAB function block. Here are the steps to do this:
  1. Use "Complex to Real-Imag" block to convert complex number in fixed point to real and imaginary part in the same data type.
  2. Now use "Data Type Coverter" block to convert the real and imaginary part from 'fixed point' to 'integer'.
  3. At last add a MATLAB function block, double click and open the block and use the below function which takes two inputs (real part, imaginary part) and uses 'dec2hex' to convert respective part to hexadecimal type.
function hexReal, hexImag = convertToHex(realPart, imagPart)
% Convert the real and imaginary parts to hexadecimal strings
% Convert to hexadecimal
hexReal = dec2hex(intReal);
hexImag = dec2hex(intImag);
end
You can refer the below documentation for more information on these blocks and function:
Hope this helps!

More Answers (1)

Andy Bartlett
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)
val =
0.4589 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 15
% just view the stored integer value of the original value in hex format
val.hex
ans = '3abc'
% Strip off the scaling to leave just the raw stored integer value
valSI = stripscaling(val)
valSI =
15036 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 0
% just view the variable holding the raw stored integer value in hex format
valSI.hex
ans = '3abc'
% If desired convert the raw stored integer variable from fi object to
% equivalent base MATLAB integer
valSI2 = castFiToInt(valSI)
valSI2 = int16 15036
% create a string variable holding the hex representation of the raw stored
% integer value
var_SI_Hex_String = dec2hex(valSI2)
var_SI_Hex_String = '3ABC'
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)
valDirectCastToInt16 = int16 0
% Creating the hex-string and putting it in a variable will still be '0'
var_DirectCastToInt16_Hex_String = dec2hex(valDirectCastToInt16)
var_DirectCastToInt16_Hex_String = '0'
Hopefully this example clarifies why the "strip scaling" step is critical

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!