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.
1 Answer
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;
-
1$\begingroup$ You also have to round your result to the nearest integer after multiplication. $\endgroup$– Matt L.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$ Apr 17, 2013 at 0:40