File Exchange

image thumbnail

Fixed-point Q Format to Decimal Converter

version 1.0.0.0 (2.34 KB) by Joseph
Convert fixed-point numbers in Qa.b format to decimal

14 Downloads

Updated 20 Feb 2017

View License

This function converts fixed-point numbers in Qa.b format to decimal where "a" is the number of bits to the left of the binary point not including the sign bit, and "b" is the number of bits to the right of the decimal point. The input format is either binary or hexadecimal (hex is the default). The function is called by the command "q2dec(x,a,b,format)" where x is a string representing the input number in Qa.b format, "a" is "a" is the number of bits to the left of the binary point not including the sign bit, "b" is the number of bits to the right of the decimal point, and format is either 'bin' or 'hex' (format is optional, 'hex' is the default).
For example, the following command can convert the hex number 0x4000 in Q0.15 (also called Q15) format to decimal:
>> q2dec('4000',0,15)
ans =
0.5000

The following command can convert the binary number 1100000000000000 in Q0.15 (also called Q15) format to decimal:
>> q2dec('1100000000000000',0,15,'bin')
ans =
-0.5000

Several numbers can be converted in one call by placing each number on a row of the input matrix:
>> x = ['8000'; 'C000'; '0000'; '4000'];
>> q2dec(x,0,15)
ans =
-1.0000
-0.5000
0
0.5000

See also my other submission dec2q that converts decimal numbers to Qa.b format.

Cite As

Joseph (2020). Fixed-point Q Format to Decimal Converter (https://www.mathworks.com/matlabcentral/fileexchange/61670-fixed-point-q-format-to-decimal-converter), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)

Orazio Turboli

Gokul

Gianfranco Talocchino

Eberhard Nussknacker

These examples don't work on my computer:
Undefined function 'q2dec' for input arguments of type 'char' (R2013b)
Undefined function or variable 'q2dec' (R2018a)

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!