4
$\begingroup$

I am trying to convert from a floating point number to a Q-notation signed 16-bit fixed point number (e.g. Q15). How can this be done in C or Java? I searched online but could not find any available code.

$\endgroup$

1 Answer 1

3
$\begingroup$

If your floating point number is X, then

X_Q15 = 32768 * X

if (X_Q15 > 32767)
X_Q15 = 32767;

if (X_Q15 < -32768)
X_Q15 = -32768;

$\endgroup$
2
  • 1
    $\begingroup$ You also have to round your result to the nearest integer after multiplication. $\endgroup$
    – Matt L.
    Commented Apr 16, 2013 at 19:11
  • 3
    $\begingroup$ Note that in @james3849's example, X_Q15 should be wider than 16 bits so that the comparisons will work correctly. Only after it passes the overflow and underflow checks should you assign to a 16 bit variable. $\endgroup$ Commented Apr 17, 2013 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.