- Use "Complex to Real-Imag" block to convert complex number in fixed point to real and imaginary part in the same data type.
- Now use "Data Type Coverter" block to convert the real and imaginary part from 'fixed point' to 'integer'.
- 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.
Data type conversion in simulink
9 views (last 30 days)
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
0 Comments
Accepted Answer
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:
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:
'Complex to Real-Imag': https://www.mathworks.com/help/simulink/slref/complextorealimag.html
'Data Type Converter': https://www.mathworks.com/help/simulink/slref/datatypeconversion.html
Hope this helps!
0 Comments
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
0 Comments
See Also
Categories
Find more on Data Types 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!