santosh nath wrote:> > Jerry Avins <jya@ieee.org> wrote in message news:<3F43E184.9C8DD216@ieee.org>... > > santosh nath wrote: > > > > > > Jim Thomas <jthomas@bittware.com> wrote in message news:<3F4385C1.F6231ADE@bittware.com>... > > > > News wrote: > > > > > > > > > my question is why need to multiply by 32768, and what's the Q15 format? > > > > > > > > Ah... they are related. Q15 is a way of representing numbers between a > > > > little less than 1.0 and -1.0 with 16 bits. It is also called "1.15". > > > > The ms bit is the sign bit. The binary point (akin to the decimal > > > > point, but since the number is in binary rather than decimal, we can't > > > > call it a decimal point) follows the sign bit. The remaining bits are > > > > weighted by 2^(n-15), where n is the bit number (the ls bit being bit > > > > 0). > > > > > > > > The easiset way to convert a number to Q15 is by multiplying it by > > > > 32768. So to get 0.5 in Q15, we multiply it by 32768 (0x8000): 0.5 * > > > > 0x8000 = 0x4000. Note that the only bit equal to 1 here is bit14, and > > > > 2^(14-15) = 2^(-1) = 0.5 which is where we started. > > > > > > If the "number" is a fractional number (as you also show in an > > > example) then what you say is correct and represents Q15 format. It > > > would be nice if you > > > highlight how to convert a floating point number to a Q format. > > > > > > For example, 7.1345, 18.6789, 154.3565 are three floating point > > > numbers representing samples of a signal how to convert them in Q9 and > > > what is the percentage reduction in precision (relative error) for > > > each of them? > > > > > > Regards, > > > Santosh > > > > Multiply by 512.0 (2^9) and cast to int. To see the error, cast back to > > float, divide by 512.0, and compare. > > > > Your first sentence trancates decimal points of the product floating > numbers to zero. The second sentence gives the error between derived > number and actual number but it does not serve the inner meaning of my > question. Is Q format achieved by your operation? I guess - no. > Secondly I was hinting the problem > of Q format when a number is represented wrongly in Q format and even > if it represented correctly it looses precision. > Regards, > Santosh > > JerryIf I goofed the numbers, then what I wrote is wrong in detail, but the idea remains. (I have no time to check right now.) Representing a number with many bits in a format that has a smaller number necessarily loses precision. Shoehorning the number into a particular format, such as Q9, may well give up more precision. There are too many ways for a number to be wrongly represented to make the general case worth thinking about. As far as I know, Q9 means that there are nine bits available for representing the fractional part of a number and (with 16-bits total), six bits for the integer part. That's what you have to work with. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������