Dear all I have sampled some data using a 16bit ADC the data is in short(16-bit int) format, now I want to use the Cortex CMSIS DSP library to do some calculations like calculating rms values.I have used the arm_rms_q15 function, but the input to this function is in q15 format. How can I convert the short and other int types to the data that has been used in this library? Any ideas are welcome.
Suppose I the ADC has filled the buffer data with these values.
I'd rather suppose it didn't. Because it really didn't.
arm_float_to_q15(data, qdata,8);
Aha. So how exactly did you expect a 1.15 fixed point datatype to hold values between 3.0 and 8.0?
This is what really happens.
Some ADC code fills this buffer. Fill_adc_buffer(); Short adc[8];
Now I cast the buffer to float so I can use the float functions.(like arm_float_to_q15)
for(int i=0;i<8;i++) {
data[i]= adc[i]; }
Now I try to cast the data to q15 using this function.
arm_float_to_q15(data, qdata,8); now I should calculate the rms value using arm_rms_q15(qdata,8,& qres); and I should translate the response back to float! arm_q15_to_float(&qres,& res,1);
the answer is in res! You do not have my adc so I just filled the data buffer with arbitrary data. but the answer is wrong!?
What should I do? I can use the arm_rms_f32 and its answer is correct, but I wanted to use q15 version. How should I do that? Do you have any idea?
Thanks in advance.
Now I cast the buffer to float so I can use the float functions.
And by doing that, you demonstrate that you really didn't heed any of the advice you've been given. You also cast significant doubt on whether you even tried to understand what you're doing.
but the answer is wrong!?
No, it's not. The problem is that you didn't ask the question you thought you were.
I wanted to use q15 version.
Then you better learn what q15 is, so you'll have a chance to use it correctly.
Your Q15 number is 16 bits large. 15 of these bits are used to store the fractional part.
There is then only one bit left. That bit must then store both integer part and sign.
Now, how large integers do you feel you can store in a 1-bit signed integer? If you think the answer is -1 ..0, you are on the way of understanding your problem.
Now, how would you be able to store the integer value 8 into a Q15 number, where the integer part must be less than 1, and the the fractional part should be stored in the remaining bits?
What would happen if you first scaled your integer value 8 by making it 16 times smaller? Now, it would be a value 0.5. So the integer part would be less than 1. And the fractional part would be 0.5. Such a valud would fit, and would be 0x04000, since fractional 0.5 is 0x4000, fractional 0.25 is 0x2000, fractional 0.125 is 0x1000, ...
Isn't it time you do spend own time thinking?
Much time have passed - have you still not managed to find this reference? en.wikipedia.org/.../Q_(number_format)
Well after the first post I read the wiki, but I did not get their point on type conversion. You mean I should divide all the adc data by 32768? Would you answer one simple question to me? Suppose I have one short variable(min=-32768 max=32767). Now I want to convert it to q15 and after some calculations convert it back to int again. Would you write a simple demo code.I’m totally confused!
You mean I should divide all the adc data by 32768?
No.
Suppose I have one short variable(min=-32768 max=32767)
I'm reasonably certain your ADC doesn't actually produce values in that range, now does it?
So what's the actual data range? How do you think that should be mapped into the actual range of the q15_t type?
The ADC is AD7606 and it can produce data in that range!
Haven't it hit you yet, that you could see the ADC as a magic device that returns measurements within the range [-1 to 1) but with lots of fractional data?
Then my numbers represent values in q15 format like this 1 equals 1/32768 or 3.051758E-06 2 equals 2/32768 or 6.10352E-06 3 equals 3/32768 or 9.15527E-06 4 equals 4/32768 or 1.2207E-05 5 equals 5/32768 or 1.52588E-05 6 equals 6/32768 or 1.83105E-05 7 equals 7/32768 or 2.13623E-05 8 equals 8/32768 or 2.44141E-05
But why I’m still getting zero from running the rms function? Please do not answer me by another question!? Char temp[100]; q15_t qdata[8]={1,2,3,4,5,6,7,8}; q15_t qres; arm_rms_q15(qdata,8,&qres);
and if I convert the result to float I still get zero
arm_q15_to_float(&qres,&res,1); sprintf(temp,"rms=%d f=%lf",qres,res);
Your input numbers are too few/small. The function arm_rms_q15 underflows when computing the RMS. After having computed the sum of the squares, it divides the result with 32768 before computing the square root.
But why I’m still getting zero from running the rms function?
You're not --- because you did not get zero before.
And why exactly do you think you have to printf() a float (not even a double) with %lf?
I realize this is 10 years later but jesus christ you guys are some insufferable egos who really seem to enjoy flexing your big brains while being completely useless to someone trying to figure out this subject and how to practically approach this problem. Have you ever considered to apply some of your massive intelligence trying to understand what someone is asking even though they may not have to vocabulary or insight to know what questions to ask. And if you don't feel it's worth your energy (which is totally fine btw) no one is forcing you.
/end rant
Hi jobl
I see you have joined this community today to leave this comment. We have community guidelines in place to remind people of our expectations when interacting in the community. If you see something in the community that you feel is not acceptable then please report the content under the Actions menu.
Thanks
Oli from the Community team
Hi Oliver. Thanks, should probably have read that. Admittedly I got pretty triggered by the condescending atmosphere in this thread (and I think rightfully so) but I understand that you want to avoid flame wars in your community, apologies.
No worries, thanks for replying :). I think we have pretty supportive community today compared to this conversation from 10+ years, but we do have measures in place to respond to inappropriate interactions. Thanks again for the reply, I appreciate it.