convert one fixed point format to integer

38 Ansichten (letzte 30 Tage)
Gary
Gary am 5 Jun. 2024
Bearbeitet: Andy Bartlett am 5 Jun. 2024
I have a variable that varies from 0 to 1 and its format is 32 bit fixdt(1,32,30). I want to convert this number 0 to 1 as 0-4095. Help is required for converting fixdt(1,32,30) to 12 bits integer value ranging from 0 to 4095.

Akzeptierte Antwort

Andy Bartlett
Andy Bartlett am 5 Jun. 2024
Bearbeitet: Andy Bartlett am 5 Jun. 2024
I assume you have a signal in Simulink using the data type fixdt(1,32,30).
Just feed that signal as input to a data type conversion block and set the output of the that to be
Output: fixdt(0,12,12)
Overflow: Saturate
The real-world-value of the output will be in the closed interval [0, 0.999755859375].
The stored-integer-value of the output will be in the range 0 to 4095 as you desire.
Note if the input has real-world-value 1.0 it will saturate to the maximum representable value of this 12 bit type 0.999755859375.
Usually it's best to feed that fixed-point signal directly to the next blocks in your design. Those blocks will implement their math with knowledge of the signals real world value.
In less common cases, you may need to have stored integer value be on a Simulink signal with all the scaling information removed. To do this, insert a second data type conversion block into your model. Connect the output of the first data type converstion as the input of the second data type conversion, then configure the second conversion block to have
Output: fixdt( 0, 12, 0)
Input and output to have equal: Stored Integer Value (SI)
the output of the second data type conversion will have trivial scaling and just have integer values from 0 to 4095.
With trivial scaling, the real-world-value and stored-integer-value are always identical.

Weitere Antworten (1)

Shivani
Shivani am 5 Jun. 2024
Hi @Gary,
To convert a fixed-point number with a range of 0 to 1 (fixdt(1,32,30)) to a 12-bit integer value ranging from 0 to 4095 in MATLAB, you can follow a simple scaling process.
Since your variable ranges from 0 to 1 and you want to map this to a range of 0 to 4095, you need to scale it by multiplying by 4095. After scaling, use the floor or round function to convert the result to an integer. Finally, cast the integer to a 12-bit representation. MATLAB does not have a built-in 12-bit integer type, so you’ll need to ensure the value fits within the 12-bit range and use a 16-bit integer to store it. You can refer to the following code for an example on how this can be implemented in MATLAB. I am using the intial variable containing the fixed-point number to be var.
scaled_var = var * 4095;
int_var = round(scaled_var);
int_var = mod(int_var, 4096); % Ensure the value fits in a 12-bit range. This step is crucial to avoid overflow if 'var' is not strictly less than 1
int_var_16bit = uint16(int_var);
  1 Kommentar
Gary
Gary am 5 Jun. 2024
I want a simulink solution to the above problem

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by