Ben S.
Ben S.

Reputation: 107

How much information is lost when I average integers?

Using C++, as this takes place on a microcontroller. My inputs are six int16_t. I cast them to int32_t so I can add them together, and then divide by 6, then store the result in an int16_t for transmission. Something like the following, and assume someRandomInt is exactly that, a random 16-bit integer each line:

int16_t one = someRandomInt;
int16_t two = someRandomInt;
int16_t three = someRandomInt;
int16_t four = someRandomInt;
int16_t five = someRandomInt;
int16_t six = someRandomInt;

int32_t sum = (int32_t)one + (int32_t)two + (int32_t)three + (int32_t)four + (int32_t)five + (int32_t)six;

int16_t result = (int16_t)(sum / 6);

I do this 16384 times per second and then perform I/Q Demodulation on the result array.

Playing around with it in Python, I've calculated that across the entire range of possibilities for the sum (-196601 to 196601) that when I divide by 6 and take the decimal parts of everything, the average decimal part is 0.417±0.285. My intuition is that that's what I'm losing when I do the math above. Is that correct? I'm concerned about when sensor values are close to zero.

Upvotes: 0

Views: 174

Answers (1)

Jimbo
Jimbo

Reputation: 85

I may not understand the question.

There is no information lost when you calculate "sum", correct?

Therefore, any information lost comes in when you divide the integer by six.

So it doesn't matter how many sensors go into the sum or how many times you sample them.

It just matters that you are dividing a number by six. When you divide an integer by an integer you get truncation and in this case that will happen 5 out of the 6 times.

5 divided by 6 is simply .8333. Half of that is .417. So your experimental results appear correct.

Upvotes: 1

Related Questions