سؤال

I don't know C well at all, and I'm trying to edit someone's code, but I'm having issues when trying to convert values from the log to linear domains.

For example, let's say we have an array A that is full of log values equal to -100 dB, i.e.

float A[100];
int i;

for( i=0; i<100; i++ )
    A[i] = -100;

What I want to do is find the average of all the values (which clearly is -100), but by taking the average in the linear and not log domain, i.e.

float tmp_avg = 0.0;
float avg;
int count = 0;

for( i=0; i<100; i++ ) {
    tmp_avg += pow(10.0, A[i]/10.0);
    count++;
}

avg = 10*log10(tmp_avg / count);

However, the result I'm getting is all 0's. Now the code I'm working on is much more complex than this, but I was wondering if there's anything obvious that I'm missing as to why this won't work.

One thought I had is that 10^(-100/10) is a very small value (1e-10), and perhaps too small to be accurately defined as a float. I've tried making it a double instead, but I still get a result of all 0's.

Thanks!

هل كانت مفيدة؟

المحلول

Just figured out what the problem was: I needed to include the math.h library at the top of the program:

#include <math.h>

Without that, I believe that there was no reference for the log10 function, which in turn caused the result to be all 0's. I now include math.h and everything seems to be working fine.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top