Q-format conversion (fixed-point) to decimal (floating-point)

112 vues (au cours des 30 derniers jours)
Peter Balazovic
Peter Balazovic le 20 Oct 2022
Dears,
I would like to convert the numbers (back and forth) between Qa.b format (fixed-point) and real-world numbers (floating-point): [-pi, pi] <-> [-2^17, 2^17 -1] as signed integers.
Is there any already exsisting Matlab function for it - if so, how to use?
Thank you!
  1 commentaire
James Tursa
James Tursa le 20 Oct 2022
Assuming you forgo the value of pi being represented (because top integer is 2^17-1 instead of 2^17), isn't this just multiply or divide by 2^17/pi? What am I missing here?

Connectez-vous pour commenter.

Réponses (1)

Eswaramoorthy
Eswaramoorthy le 8 Juin 2023
In MATLAB, you can use the 'fi' (fixed-point) data type along with its associated functions to convert between fixed-point and floating-point representations. However, there is no built-in function specifically designed to convert between the Q format (e.g., Qa.b) and real-world numbers. You would need to perform the conversion manually.
Here's an example of how you can do the conversion in MATLAB:
% Convert real-world number to fixed-point representation
realNumber = pi; % Input real-world number
Qa = 17; % Number of integer bits
Qb = 0; % Number of fractional bits
fixedPointNumber = fi(realNumber, true, Qa, Qb);
% Access the fixed-point value and scale it to the desired range
fixedPointValue = fixedPointNumber.data;
fixedPointRange = [-2^(Qa+Qb), 2^(Qa+Qb) - 1];
scaledValue = (fixedPointValue - fixedPointRange(1)) * (pi - (-pi)) / (fixedPointRange(2) - fixedPointRange(1)) + (-pi);
% Convert fixed-point representation back to real-world number
recoveredValue = scaledValue;
% Display the results
fprintf('Real-world number: %f\n', realNumber);
fprintf('Fixed-point value: %d\n', fixedPointValue);
fprintf('Scaled value: %f\n', scaledValue);
fprintf('Recovered real-world number: %f\n', recoveredValue);
In this code, we start by converting a real-world number (realNumber) to fixed-point representation using the fi function. The Qa and Qb parameters specify the number of integer and fractional bits, respectively.
Next, we access the fixed-point value using the '.data' property of the fixedPointNumber object. Then, we scale the fixed-point value to the desired range ([-pi, pi]) by mapping it from the range of the fixed-point representation ('[-2^(Qa+Qb), 2^(Qa+Qb) - 1]') to the real-world range.
To convert the fixed-point representation back to a real-world number, we simply assign the scaled value to recoveredValue.
You can modify the Qa and Qb parameters according to your fixed-point format requirements. The code provides the real-world number, fixed-point value, scaled value, and recovered real-world number for demonstration purposes.
Remember that fixed-point representation introduces quantization and may introduce rounding errors.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by