Q-format conversion (fixed-point) to decimal (floating-point)
73 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2022-10-20
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?
回答(1 个)
Eswaramoorthy
2023-6-8
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Create Fixed-Point Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!