convert one fixed point format to integer
Show older comments
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.
Accepted Answer
More Answers (1)
Shivani
on 5 Jun 2024
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);
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!