Q-format conversion (fixed-point) to decimal (floating-point)
111 ビュー (過去 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 で Manual Fixed-Point Conversion in MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!