File Exchange

image thumbnail

Decimal to Fixed-point Q Format Converter

version 1.0.0.0 (2.41 KB) by Joseph
Convert decimal (base 10) numbers to fixed-point Qa.b format

16 Downloads

Updated 20 Feb 2017

View License

This function converts decimal (base 10) numbers to fixed-point Qa.b format 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 output format is either binary or hexadecimal (hex is the default). The function is called using the command "dec2q(x,a,b,format)" where x is the decimal input (which can either be a scalar or a vector), "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, and format is either 'bin' or 'hex' (format is optional, 'hex' is the default).
For example, the following command converts the decimal number 0.5 to Q0.15 format (which is also called Q15) with the output expressed as a hex number:
>> dec2q(0.5,0,15)
ans =
4000
The following command converts the decimal number -0.5 to Q0.15 with the output expressed as a binary number:
>> dec2q(-0.5,0,15,'bin')
ans =
1100000000000000

The function can also convert a vector of decimal numbers to Q0.15 format:
>> dec2q([-1 -0.5 0 0.5],0,15)
ans =
8000
C000
0000
4000

See also my other file submission q2dec which converts from Qa.b format to decimal.

Cite As

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

Comments and Ratings (4)

David Fardis

Hello Cool things... Check out online hex to decimal converter https://hextodec.org/.

Gianfranco Talocchino

Dipesh Mudatkar

At line number 58: Code is x(k) = -2^a;
whereas it should be x(i)=-2^a;

vaishnavi Mane

Im getting an error saying " input argument "x" is undefined" at line: for i= 1:length(x)

Updates

1.0.0.0

Changed the title to be more descriptive.

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