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.
Hello,
I am struggling with a IQ math calculation.
I got an ADC result ranging from 0000h - 7FF8h, which is a int16 (Q16)number.
Then, my calculation is as follows:
result = ADC * 3/ 0x7FF8.
m = 2.03/result;
For instance, if my ADC result is 0x39D7, I expect to have m = 1.50.
So I wrote the code as following
//=================================================
int16 ADC; //Q15
_iq m; //Global_Q is 24, so _iq is _IQ24
ADC = _IQ15div(ADC,32760); //line1
ADC = _IQ15mpy(ADC, IQ15(3)); //line2
m = _IQdiv(_IQ(2.03), _IQ15toIQ(ADC)); //line3
//=================================================
Fromt the CCS watch window, only line 1 can get the result that I expect.
From line2 till line 3, the results are weird.
Can you please point out what is wrong in my code when using the IQ math.
Thanks a lot!
Hi,
IQ15 is a 32 bit integer, 17 bit integer part, 15 bit fraction so IQ15(3) is actually0b11 << 15 or 17 significant bits. You doa IQ15 multiply which again gives you a 32-bit output but then assign to ADC, a 16-bit variable so im thinking either the result is getting right shifted and truncated oryou just see the least significant word - either case its thr wrong answer.
line 1 works because the answer is <1 so truncation still preserves the fraction part.
You can make ADC 32-bits or you could calculate like this
m = 2.03/3 *0x7FF8 /ADC;
m = _IQmpy(_IQ(0.67), _IQdiv(0x7FF8 ,_IQ15toIQ(ADC)));
I think that should work
Vishal,
I think I know why.
I changed the expression to m = _IQmpy(_IQ(0.67), _IQdiv(0x7FF8 ,ADC)), instead of , _IQdiv(0x7FF8 ,IQ15toIQ(ADC)).
It works now.
Thanks!