This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem with casting _IQtoQ15 to int16

Expert 1570 points


Hallo,

I'm trying to cast an _IQ value (my global is _IQ24) to int16.

My cast for an example  :

_iq a=_IQ(1.0)

int16_t b=0;

b = (int16_t)_IQtoQ15(a);

However, for value of 1 and -1 the cast return the same value...

Am I doing something wrong ?

Thanks

  • Hi,

    Maybe you can use _IQ15int(long) to get integer part and after cast in int16_t
    b = (int16_t) _IQ15int(_IQtoQ15(a));

    If the integer part is between 127 and -127, it's not necessary to cast in _IQ15.


    Edit : For 32 bit

  • Mojo,

    You cannot represent the value "1" in Q15 format. The largest positive Q15 value is 1-2^-15 (which is represented by 0x7FFF).

    - David
  • I would like to send an IQ value over UART.
    So I'm casting to int16, then splitting to two bytes.
    Then, in the PC, I assemble the bytes, cast to float and divide by 32767.

    Is there another "fast" way of doing this ?
    I could always do _IQtoF then do everything in float, cast to int etc..

    Thanks
  • An IQ value is 32 bits. If you send only 16 bits, you could miss information. With an IQ24 format, you have 8 integer bits and 24 decimal bits. Are you saying you're satisfied with only sending 16 of these bits? If so, just send those 16 bits. For example something like this:

    b = (int16)( ( (int32) (a) ) >> N);

    where N is the shift value that isolates the bits you want in the lower 16 bits of the 32-bit value of a. The typcast to int16 will then assign b to the lower 16 bits of the shifted 32-bit value.

    I haven't tested this, but I think it would work. Note that I think you will get a compiler warning about the typecast causing possible loss of resolution (something like that).

    - David