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.

IQ math question

What's wrong with the following command:

    PhaseDiscrete = _IQ15mpy(_IQ15(PhaseDiscrete),_IQ15(Speed));

Both arguments are _iq15, so as PhaseDiscrete.

Adding this statement in the program prevents it from running, although no errors occur during the Build procedure.

What's wrong?

  • If the variable PhaseDiscrete is _IQ type of different base value, convert it to IQ15 by IQtoIQ15, not _IQ(

    _IQ15(x) does a multiplication of x with 2^15 (left shift by 15 times) which will cause overflow/ lose of many ms bits.

    Assume you are using GLOBAL_Q = 24, value of speed set to = 0.22

    actual value of speed = _IQ(0.22) = 0.22 * 2^24 = 3690987 = 0x3851EB

    _IQ15(_IQ(0.22)) = 0x3851EB * 2^15 = 1C28F5C28F, cannot hold in 32 bits!!!

    Hope it helps

    Joy

  • Thank You, Joy!

    First of all, once more, both variables, PhaseDiscrete and Speed are of the same _iq15 type.

    I'd like Speed to change it's value from 0.1 to 10. I'm doing it with:

    Speed = _IQ15(0.1);

    or

    Speed = _IQ15(2.8);

    or

    Speed = _IQ15(3.6);

    But it does not work.

    How can I repair those sentences for them to work correctly?

  • if everything is in _IQ15, set GLOBAL_Q = 15

    and equation would be

    PhaseDiscrete =  _IQmpy(PhaseDiscrete, Speed);

    //initial value/ assignment samples

    PhaseDiscrete = _IQ(0.2);

    Speed = _IQ(1.3);

    Speed += IQ(0.01); etc etc

    Joy

  • Thanks, Joy!

    Yes, setting the GLOBAL_Q eases things a lot.